Jump to content

Pong Clone


Recommended Posts

Here is a Pong game that I made. Attached is the Autoit Script, the source is below. Suggestions appreciated!

EDIT: P1 controls are up down arrow keys or mouse, P2 controls are W and S.

Edit 11/3/07: Many bugs fixed!

$aimove = 2.5
$moved = 0
$smove = 0
$boost = 0
$btime = TimerInit()
HotKeySet("w", "fc")
HotKeySet("s", "fc")
#include <Misc.au3>
#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Banana Pong", 209, 205, 193, 115)
$Label1 = GUICtrlCreateLabel("Pong Setup", 8, 8, 60, 17)
$Label2 = GUICtrlCreateLabel("Player 1 Controls:", 8, 32, 86, 17)
$Radio1 = GUICtrlCreateRadio("Keys", 8, 56, 97, 17)
$Radio2 = GUICtrlCreateRadio("Mouse", 112, 56, 81, 17)
$Checkbox1 = GUICtrlCreateCheckbox("AI Player", 8, 80, 97, 17)
$Combo1 = GUICtrlCreateInput("", 80, 104, 65, 25)
$Label3 = GUICtrlCreateLabel("Points to Win:", 8, 105, 70, 17)
GUIStartGroup()
$rad1 = GUICtrlCreateRadio("Easy AI", 8, 135)
$rad2 = GUICtrlCreateRadio("Hard AI", 70, 135)
GUIStartGroup()
$Button1 = GUICtrlCreateButton("Start!", 16, 165, 177, 33, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$inittime = TimerInit()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        
    Case $Button1
        If StringIsAlNum(GUICtrlRead($Combo1)) = 1 Then
            If GUICtrlRead($Radio1) = $GUI_CHECKED Then
                $keys = 1
            Else
                $keys = 0
            EndIf
            If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then
                $ai = 1
            Else
                $ai = 0
            EndIf
            If GUICtrlRead($rad2) = $GUI_CHECKED Then
                $aimove = 4
            Else
                $aimove = 3
            EndIf
            $plim = GUICtrlRead($Combo1)
            ExitLoop
        Else
            MsgBox(0, "Pong", "The Points To Win Value needs to be a number.")
        EndIf
    EndSwitch
WEnd

$gui = GUICreate("Pong", 300, 200)
$p1 = GUICtrlCreateLabel("", 5, 85, 10, 30)
$points = GUICtrlCreateLabel("", 10, 10, 50)
$p1t = 85
$p2 = GUICtrlCreateLabel("", 285, 85, 10, 30)
$p2t = 85
$ball = GUICtrlCreateLabel("", 145, 95, 10, 10)
$pl1points = 0
$pl2points = 0
$hit = 0
GUICtrlSetBkColor($p1, 0x000000)
GUICtrlSetBkColor($p2, 0x000000)
GUICtrlSetBkColor($ball, 0x990000)
$bl = 145
$bt = 95
    $movel = -3
    $movet = 3
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    EndSelect
    If TimerDiff($inittime) > 10 Then
    Select
    Case _IsOn(5, $p1t, 10, 30, $bl, $bt, 10, 10) = 1
        If $hit = 0 Then
            If BitAND($bt + 5 < $p1t + 27, $bt + 5 > $p1t + 3) Then
                If _IN($movel) = 1 Then
                    $movel = 3
                Else
                    $movel = -3
                EndIf
                If _IN($movet) = 1 Then
                    $movet = -3
                Else
                    $movet = 3
                EndIf
            Else
                ;If _IN($movel) = 1 Then
                    $movel = 2
                ;Else
                ;   $movel = -2
                ;EndIf

                    If $bt + 5 > $p1t + 15 Then
                        $movet = 4
                    Else
                        $movet = -4
                    EndIf
            EndIf
            $hit = 1
        EndIf
    Case BitAND($bl + 10 > 285, $bt < $p2t + 30, $bt + 10 > $p2t) = 1
        If $hit = 0 Then
            If BitAND($bt + 5 < $p2t + 27, $bt + 5 > $p2t + 3) Then
                If _IN($movel) = 1 Then
                    $movel = 3
                Else
                    $movel = -3
                EndIf
                If _IN($movet) = 1 Then
                    $movet = -3
                Else
                    $movet = 3
                EndIf
            Else
                ;If _IN($movel) = 1 Then
                ;   $movel = 2
                ;Else
                    $movel = -2
                ;EndIf
                    If $bt + 5 > $p2t + 15 Then
                        $movet = 4
                    Else
                        $movet = -4
                    EndIf
                EndIf
            $hit = 1
        EndIf
    Case _IsOn(5, $p1t, 10, 30, $bl, $bt, 10, 10) = 0
        $hit = 0
    Case BitAND($bl + 10 > 285, $bt < $p2t + 30, $bt + 10 > $p2t) = 0
        $hit = 0
EndSelect
    Select
    Case _IsPressed("26")
        $p1t = $p1t - 3
    Case _IsPressed("28")
        $p1t = $p1t + 3
    EndSelect
    If $ai = 0 Then
        Select
        Case _IsPressed("53")
            $p2t = $p2t + 3
        Case _IsPressed("57")
            $p2t = $p2t - 3
        EndSelect
    Else
        If $moved > 17 Then
            If $p2t + 30 > $bt + 5 Then
                $smove = _NT($aimove)
            ElseIf $p2t < $bt + 5 Then
                $smove = $aimove
            EndIf
        $moved = 0
        EndIf
        $p2t = $p2t + $smove
        $moved = $moved + $aimove
    EndIf
    If $pl1points = $plim Then
        MsgBox(0, "Game Over", "Player 1 Won!")
        If @Compiled = 1 Then
            Run(@ScriptFullPath)
        Else
            Run(@AutoItExe & " " & @ScriptName)
        EndIf
        Exit
    ElseIf $pl2points = $plim Then
        MsgBox(0, "Game Over", "Player 2 Won!")
        If @Compiled = 1 Then
            Run(@ScriptFullPath)
        Else
            Run(@AutoItExe & " " & @ScriptName)
        EndIf
        Exit
    EndIf
    $coords = GUIGetCursorInfo()
    If $keys = 0 Then
        $p1t = $coords[1]
    EndIf
    If TimerDiff($inittime) < 20 Then
    GUICtrlSetPos($p1, 5, $p1t)
    GUICtrlSetPos($p2, 285, $p2t)
    EndIf
    If $bl < 0 Then
        If $wall = 0 Then
            $movel = _Rev($movel)
            $pl2points += 1
            $wall = 1
        EndIf
    ElseIf $bl > 290 Then
        If $wall = 0 Then
            $movel = _Rev($movel)
            $pl1points += 1
            $wall = 1
        EndIf
    Else
        $wall = 0
    EndIf
    If $bt < 0 Then
        $movet = _Rev($movet)
    ElseIf $bt > 190 Then
        $movet =_Rev($movet)
    EndIf
    $bl = $movel + $bl
    $bt = $bt + $movet
    GUICtrlSetPos($ball, $bl, $bt)
    GUICtrlSetData($points, $pl1points & " - " & $pl2points)
    $inittime = TimerInit()
    EndIf
WEnd
Func _IsOn ($left1, $top1, $width1, $height1, $left2, $top2, $width2, $height2)
    If StringIsAlNum($left1 & $top1 & $width1 & $height1 & $left2 & $top2 & $width2 & $height2) = 0 Then
        Return -1
    EndIf
    $lewi1 = $left1 + $width1
    $tohi1 = $height1 + $top1
    $lewi2 = $left2 + $width2
    $tohi2 = $height2 + $top2
    If BitAND($left1 < $lewi2, $lewi1 > $left2, $tohi1 > $top2, $top1 < $tohi2) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc
Func _IsBetween ($min, $max, $num)
    If BitAND($num > $min - 1, $num < $max + 1) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc
Func _Around ($numin, $around, $numbase)
    If BitAND($numin < $numbase + $around, $numin > $numbase - $around) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

Func _Rev ($num)
    If $num < 0 Then
        $num = $num + StringReplace(($num * 2), "-", "")
    ElseIf $num > 0 Then
        $num = $num - ($num * 2)
    EndIf
    Return $num
EndFunc

Func _IN ($num)
    If $num < 0 Then
        Return 1
    ElseIf $num > 0 Then
        Return 0
    EndIf
EndFunc

Func _NT($num)
    If $num < 0 Then Return $num
    Return "-" & $num
EndFunc

Func fc ()
EndFunc

pong.au3

Edited by BananaFredSoft
Link to comment
Share on other sites

Keys don't work when AI player is used. Also, I find it quite difficult for pong, maybe it should be a little wider?

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Whoops. Actually, that's just when you have Easy AI. But I'll fix that. Do you mean make the paddles wider, or what?

Keys don't work when AI player is used. Also, I find it quite difficult for pong, maybe it should be a little wider?

Link to comment
Share on other sites

The pong window, i think it's a little fast (maybe I'm just rubbish at pong :rolleyes:)

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

The pong window, i think it's a little fast (maybe I'm just rubbish at pong :rolleyes:)

when you are playing with keys and move the mouse the ball travells with light speed! (i think thats the problem) but you shouldnt move the mouse while plaing with keys

anyway the game is GREAT, well done!

Edited by c4nm7
Link to comment
Share on other sites

I think you need to group those radio buttons in pairs with GUIStartGroup(), because as of right now only one of all four (Keys, Mouse, EasyAI, HardAI) can be selected, and I don't think it makes any sense.

"be smart, drink your wine"

Link to comment
Share on other sites

  • 4 months later...

New update, many bugs have been fixed. Suggestions/Comments appreciated. <_<

When running the code, it is saying that "$wall is undeclared", just puting "$wall = 0" at the top of the script should fix it.

When you win or lose, you call one of these line : "Run(@ScriptFullPath)" or "Run(@AutoItExe & " " & @ScriptName)", wich depend if the script is compiled or not. I found out that if it's not compiled, @ScriptName will only return the first word of the script name, so if you named it using two word, it will drop an error. This is just a little bug in the maccro, maybe it is just me, I think someone else should try.

Other than that, I like this script, good job :)

Link to comment
Share on other sites

Very interesting!

I liked it <_<

A sugestion: disable the hotkey when the window is not active.

:)

Só o que posso lhe dizer, bom é quando faz mal!My work:Au3Irrlicht - Irrlicht for AutoItMsAgentLib - An UDF for MSAgentAu3GlPlugin T2 - A 3D plugin for AutoIt...OpenGl Plugin - The old version of Au3GlPlugin.MAC Address Changer - Changes the MAC AddressItCopter - A dragonfly R/C helicopter simulator

VW Bug user

Pinheiral (Pinewood) city:

http://pt.wikipedia.org/wiki/Pinheiral

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...