Jump to content

Pong!


LimeSeed
 Share

Recommended Posts

heres my first attempt at one player pong! it includes a computer with 4 difficulty settings :P does anyone know how to double buffer this cuz it looks like shit? Thanks!

#include <guiconstants.au3>
#include "misc.au3"
GUICreate("Pong", 200, 270)
guisetbkcolor(0x000000)
guisetstate()
$xball = 0
$yball = 0
$direction = true ;if true, going twords paddle, if false, going towards computer player
$boundaries = true ;make ball bounce off walls
$xp1 = 0
$xcomp = 0
$vDll = 'user32.dll'
dim $points[2] = [0, 0]
$pointscore = guictrlcreatelabel("P1: " & $points[0] & " Computer: " & $points[1], 5, -1, 180, 12)
guictrlsetbkcolor($pointscore, 0xFFFFFF)
$p1 = guictrlcreategraphic(0, 0, 70, 5)
guictrlsetgraphic(-1, $GUI_GR_COLOR, 0x0000FF, 0x0000FF)
guictrlsetgraphic(-1, $GUI_GR_RECT, 80, 260, 55, 5)
guictrlsetgraphic(-1, $GUI_GR_REFRESH)
guictrlsetgraphic(-1, $GUI_GR_CLOSE)
$ball = guictrlcreategraphic(0, 0, 50, 50)
guictrlsetgraphic(-1, $GUI_GR_COLOR, 0xFFFF00, 0xFFFF00)
guictrlsetgraphic(-1, $GUI_GR_PIE, 10, 10, 5, 0, 360)
guictrlsetgraphic(-1, $GUI_GR_REFRESH)
guictrlsetgraphic(-1, $GUI_GR_CLOSE)
$computer = guictrlcreategraphic(0, 5, 70, 5)
guictrlsetgraphic(-1, $GUI_GR_COLOR, 0xFF0000, 0xFF0000)
guictrlsetgraphic(-1, $GUI_GR_RECT, 20, 10, 55, 5)
guictrlsetgraphic(-1, $GUI_GR_REFRESH)
guictrlsetgraphic(-1, $GUI_GR_CLOSE)
$easy = guictrlcreatebutton("Easy", 5, 5, 190, 50)
$medium = guictrlcreatebutton("Medium", 5, 55, 190, 50)
$hard = guictrlcreatebutton("Hard", 5, 105, 190, 50)
$impossible = guictrlcreatebutton("Impossible", 5, 155, 190, 50)
$game = 0
Do
    $msg = guigetmsg()
        if ($msg = $easy) Then
            $game = 0
        ElseIf ($msg = $medium) then
            $game = 1
        ElseIf ($msg = $hard) then
            $game = 2
        ElseIf ($msg = $impossible) Then
            $game = 3
        EndIf
Until (($msg = $easy) or ($msg = $medium) or ($msg = $hard) or ($msg = $impossible))
guictrldelete($easy)
guictrldelete($medium)
guictrldelete($hard)
guictrldelete($impossible)

if ($game = 0) Then
    $gf = 1
ElseIf ($game = 1) Then
    $gf = 1.4
ElseIf ($game = 2) Then
    $gf = 2
Else
    $gf = 4
EndIf
$g = $gf
While 1
    $gf += .001
    guictrlsetgraphic($ball, $GUI_GR_REFRESH)
    sleep(20)
    if (_IsPressed(08, $vDll)) Then
    Exit
    EndIf
    if (_IsPressed(25, $vDll) and $xp1 > -75) Then
        $xp1 -= 4
        guictrlsetpos($p1, $xp1, 0)
        ;guictrlsetgraphic($p1, $GUI_GR_REFRESH)
    EndIf
    if (_IsPressed(27, $vDll) and $xp1 < 60) Then
        $xp1 += 4
        guictrlsetpos($p1, $xp1, 0)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
    EndIf
    if ($direction) Then
        $yball += $gf * 2
        guictrlsetpos($ball, $xball, $yball)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
    Else
        $yball -= $gf * 2
        guictrlsetpos($ball, $xball, $yball)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
        $aipos = $xball - 50
        if (($aipos > -15) and ($aipos < 120)) Then
            guictrlsetpos($computer,$aipos , 5)
            ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
        EndIf
    EndIf
    if ($boundaries) Then
        $xball += $gf * 3
        guictrlsetpos($ball, $xball, $yball)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
    Else
        $xball -=  $gf * 3
        guictrlsetpos($ball, $xball, $yball)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
    EndIf
    if ($xball > 180) Then $boundaries = False
    if ($xball < 0) then $boundaries = True
    if (($yball >= 244) and ($direction)) Then
        ;xball = 0, xp1 = -68
        $contact = Execute($xball - $xp1)
        if (($contact > 68) and ($contact < 123)) then 
            $direction = False
        Else
            $points[1] += 1
            $gf = $g
            guictrlsetdata($pointscore, "P1: " & $points[0] & " Computer: " & $points[1])
            $yball = 50
            $direction = False
        EndIf
    EndIf
    if (($yball <= 17) and (not($direction))) Then
        $contact = Execute($xball - $aipos)
        if (($contact < 60) and ($contact > 0)) Then
            $direction = True
        Else
            $points[0] += 1
            $gf = $g
            guictrlsetdata($pointscore, "P1: " & $points[0] & " Computer: " & $points[1])
            $yball = 50
            $direction = true
        EndIf
    EndIf
WEnd
global $warming = true
Link to comment
Share on other sites

Screennnyy!!

what?

i added the line in the middle

$line = guictrlcreategraphic(-1, 150)
guictrlsetgraphic(-1, $GUI_GR_COLOR, 0x00FFFF, 0x00FFFF)
guictrlsetgraphic(-1, $GUI_GR_PIE, -1, -10, 300, 0, 0)
guictrlsetgraphic(-1, $GUI_GR_REFRESH)
guictrlsetgraphic(-1, $GUI_GR_CLOSE)
global $warming = true
Link to comment
Share on other sites

Great job! It looks really good! :P

There is unfortunately not possible to double buffer autoits built in graphics functions. You'll need GDI+ for that.

Look into my GDI+ apps, they all use double buffering (well almost all).

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Great job! It looks really good! :P

There is unfortunately not possible to double buffer autoits built in graphics functions. You'll need GDI+ for that.

Look into my GDI+ apps, they all use double buffering (well almost all).

dammnit! im redoing the whole thing using gdi+
global $warming = true
Link to comment
Share on other sites

yea but his thing didnt work, when i put it in, the gui window didnt show up

Wraithdu has explained why. Also I should have said the "extended style" not "style".
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I remember when I made a Pong game a while ago. I had some pretty good AI because I would set it to a "Random Rubber Band" type scenario. In other words I would have the margin of error in the AI's difficulty swing around a base number.

[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Link to comment
Share on other sites

I remember when I made a Pong game a while ago. I had some pretty good AI because I would set it to a "Random Rubber Band" type scenario. In other words I would have the margin of error in the AI's difficulty swing around a base number.

Yay i made it much better1

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\Desktop\dick.ICO
#AutoIt3Wrapper_Outfile=Pong.exe
#AutoIt3Wrapper_Res_Comment=Made By Isaac Flaum
#AutoIt3Wrapper_Res_Description=Pong Game
#AutoIt3Wrapper_Res_Fileversion=7.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=Isaac Flaum 2008
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;Brought To You By Isaac Flaum

#include "guiconstants.au3"
#include "misc.au3"
#include "GDIPlus.au3"
#include "WinAPI.au3"
#include "windowsconstants.au3"
#include "GUIConstantsEx.au3"

;create double buffered gui
Global Const $WS_EX_COMPOSITED = 0x2000000
$hGUI = GUICreate(ChrW(20184) & ChrW(38647) & " Pong", 200, 270, -1, -1, -1, $WS_EX_COMPOSITED) ;Double Buffering
GUISetBkColor(0x000000)
GUISetState()

;variables necessary for game to work
$xball = 0
$yball = 0
$direction = True ;if true, going twords paddle, if false, going towards computer player
$boundaries = True ;make ball bounce off walls
$xp1 = 0
$xcomp = 0
$game = 0
$aipos = 20
$vDll = 'user32.dll'
Dim $points[2] = [0, 0]
Dim $scoreboard[2]
;create the paddles, scoreboards, half line, and ball
$t = 100

For $i = 0 To 1 Step 1
    $scoreboard[$i] = GUICtrlCreateLabel("0", 190, $t, 30, 30)
    GUICtrlSetFont($scoreboard[$i], 16, 400, 1, "Visitor TT2 BRK")
    GUICtrlSetBkColor($scoreboard[$i], $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor($scoreboard[$i], 0xFFFFFF)
    $t += 70
Next
$line = GUICtrlCreateGraphic(-1, 150)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x00FF44, 0x00FF44)
GUICtrlSetGraphic(-1, $GUI_GR_PIE, -1, -10, 300, 0, 0)
GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
GUICtrlSetGraphic(-1, $GUI_GR_CLOSE)
$p1 = GUICtrlCreateGraphic(0, 0, 70, 5)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x0000FF, 0x0000FF)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 80, 260, 55, 5)
GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
GUICtrlSetGraphic(-1, $GUI_GR_CLOSE)
$ball = GUICtrlCreateGraphic(0, 0, 50, 50)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFF00, 0xFFFF00)
GUICtrlSetGraphic(-1, $GUI_GR_PIE, 10, 10, 5, 0, 360)
GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
GUICtrlSetGraphic(-1, $GUI_GR_CLOSE)
$computer = GUICtrlCreateGraphic($aipos, 5, 70, 5)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFF0000, 0xFF0000)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 20, 10, 55, 5)
GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
GUICtrlSetGraphic(-1, $GUI_GR_CLOSE)

$easy = GUICtrlCreateButton("Easy (Slow)", 5, 5, 190, 50)
GUICtrlSetBkColor($easy, 0x00FFFFF)
$medium = GUICtrlCreateButton("Medium (Moderate)", 5, 55, 190, 50)
GUICtrlSetBkColor($medium, 0x00FFFFF)
$hard = GUICtrlCreateButton("Hard (Fast)", 5, 105, 190, 50)
GUICtrlSetBkColor($hard, 0x00FFFFF)

Do
    $msg = GUIGetMsg()
    If $msg = -3 Then Exit
    If ($msg = $easy) Then
        $game = 0
    ElseIf ($msg = $medium) Then
        $game = 1
    ElseIf ($msg = $hard) Then
        $game = 2
    EndIf
Until (($msg = $easy) Or ($msg = $medium) Or ($msg = $hard))

GUICtrlDelete($easy)
GUICtrlDelete($medium)
GUICtrlDelete($hard)

If ($game = 0) Then
    $gf = 1
ElseIf ($game = 1) Then
    $gf = 1.4
ElseIf ($game = 2) Then
    $gf = 2
EndIf

Global Const $g = $gf

While 1
    $gf += .001 ;gradually increase speed of ball as rally goes on
    GUICtrlSetGraphic($ball, $GUI_GR_REFRESH) ;refresh ball, but also refreshes paddles
    Sleep(10)
    If _IsPressed(08, $vDll) Then Exit

    If (_IsPressed(25, $vDll) And $xp1 > -75) Then
        $xp1 -= 4
        GUICtrlSetPos($p1, $xp1, 0)
    EndIf
    If (_IsPressed(27, $vDll) And $xp1 < 60) Then
        $xp1 += 4
        GUICtrlSetPos($p1, $xp1, 0)
        
    EndIf
    
    If ($direction) Then
        $yball += $gf * 2
        GUICtrlSetPos($ball, $xball, $yball)
    Else
        $yball -= $gf * 2
        GUICtrlSetPos($ball, $xball, $yball)
    EndIf
    
    If ($boundaries) Then
        $xball += $gf * 3
        If $aipos < 115 Then
            $aipos += $gf * 2
            GUICtrlSetPos($computer, $aipos, 5)
        EndIf
        GUICtrlSetPos($ball, $xball, $yball)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
    Else
        $xball -= $gf * 3
        $aipos -= $gf * 2
        GUICtrlSetPos($computer, $aipos, 5)
        GUICtrlSetPos($ball, $xball, $yball)
        ;guictrlsetgraphic(-1, $GUI_GR_REFRESH)
    EndIf

    If ($xball > 180) Then $boundaries = False
    If ($xball < 0) Then $boundaries = True
    If (($yball >= 244) And ($direction)) Then

        $contact = Execute($xball - $xp1)
        If (($contact > 68) And ($contact < 130)) Then
            $direction = False
        Else
            $points[1] += 1
            Sleep(100)
            $gf = $g
            GUICtrlSetData($scoreboard[0], $points[1])
            $yball = 50
            $direction = False
        EndIf
    EndIf
    
    If (($yball <= 17) And (Not ($direction))) Then
        $contact = Execute($xball - $aipos)
        If (($contact < (60 + $gf + 3)) And ($contact > 0)) Then
            $direction = True
        Else
            $points[0] += 1
            Sleep(100)
            $gf = $g
            GUICtrlSetData($scoreboard[1], $points[0])
            $yball = 50
            $direction = True
        EndIf
    EndIf
    
    If $points[0] = 7 Then
        $winner = GUICtrlCreateLabel("P1 Wins", 5, 70, 250, 50)
        GUICtrlSetFont($winner, 16, 400, 1, "Visitor TT2 BRK")
        GUICtrlSetBkColor($winner, $GUI_BKCOLOR_TRANSPARENT)
        GUICtrlSetColor($winner, 0xFFFFFF)
        Sleep(1000)
        $answer = MsgBox(4, ChrW(20184) & ChrW(38647) & " Pong", "Play Again?")
        If $answer = 7 Then
            Exit
        Else
            For $i = 0 To 1 Step 1
                $points[$i] = 0
            Next
            GUICtrlDelete($winner)
            GUICtrlSetData($scoreboard[1], $points[0])
            GUICtrlSetData($scoreboard[0], $points[1])
            Sleep(10)
            $xball = 0
            $yball = 0
            $direction = True ;if true, going twords paddle, if false, going towards computer player
            $boundaries = True ;make ball bounce off walls
            $xp1 = 0
            $xcomp = 0
            $game = 0
            $aipos = 20
        EndIf
    ElseIf $points[1] = 7 Then
        $winner = GUICtrlCreateLabel("Computer Wins", 5, 70, 250, 50)
        GUICtrlSetFont($winner, 16, 400, 1, "Visitor TT2 BRK")
        GUICtrlSetBkColor($winner, $GUI_BKCOLOR_TRANSPARENT)
        GUICtrlSetColor($winner, 0xFFFFFF)
        Sleep(1000)
        $answer = MsgBox(4, ChrW(20184) & ChrW(38647) & " Pong", "Play Again?")
        If $answer = 7 Then
            Exit
        Else
            For $i = 0 To 1 Step 1
                $points[$i] = 0
            Next
            GUICtrlDelete($winner)
            GUICtrlSetData($scoreboard[1], $points[0])
            GUICtrlSetData($scoreboard[0], $points[1])
            Sleep(10)
            $xball = 0
            $yball = 0
            $direction = True ;if true, going twords paddle, if false, going towards computer player
            $boundaries = True ;make ball bounce off walls
            $xp1 = 0
            $xcomp = 0
            $game = 0
            $aipos = 20
        EndIf
    EndIf
WEnd
Edited by LimeSeed
global $warming = true
Link to comment
Share on other sites

Well, the computer can move his paddle much faster than the player ...

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Hmm, why didn't take it my full post?

I wanted to say, it's even difficult to score when playing "easy"

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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...