Jump to content

Ping Pong Game with AutoItObject_Internal


Recommended Posts

For comparative testing purposes I tried to translate the script created by @Numeric1 at this link which uses AutoitObject, so as to use AutoItObject_Internal.au3 by @genius257 instead. (The latter does not require additional DLLs.). Maybe it seems a little slower and slightly less responsive (?)

P.S. I created a new thread just to not hijack @Numeric1's original thread.

(any improvements and corrections relating to the translation of the listing are welcome)

; ===============================================================================================================================
; original script created by @Numeric1 at the link below
; https://www.autoitscript.com/forum/topic/211824-ping-pong-game-with-autoitobject/
; ===============================================================================================================================
; Game Overview:
;   - The game consists of a paddle and a ball.
;   - The player controls the paddle using the left and right arrow keys.
;   - The objective is to bounce the ball off the paddle and prevent it from hitting the bottom edge of the window.
;   - If the ball hits the bottom edge, the game ends.
;   - As the game progresses, the speed of the ball increases periodically, making it more challenging.
; Controls:
;   - Left Arrow Key: Move the paddle to the left.
;   - Right Arrow Key: Move the paddle to the right.
;   - S Key: Pause the game.
;   - When the game is paused, press Left or Right arrow key to resume.
; Enjoy playing Ping Pong!
; ===============================================================================================================================

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
; #include "AutoItObject.au3"
#include "AutoItObject_Internal.au3" ; <-- https://www.autoitscript.com/forum/topic/185720-autoitobject-pure-autoit
#include <Misc.au3>

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
_GDIPlus_Startup()
Const $COLOR_RED = 0xFFFF0000
Const $COLOR_GREEN = 0xFF00FF00

Func Ball($x = 0, $y = 0, $size = 5)
    Local $cBall = IDispatch() ; _AutoItObject_Class()

    With $cBall
        .dx = 10
        .dy = -10
        .size = $size
        .X = $x
        .Y = $y
        .__defineGetter("move", _move)
    EndWith

    Return $cBall ; .Object
EndFunc   ;==>Ball

Func Paddle($x = 0, $size = 5)
    Local $cPaddle = IDispatch() ; _AutoItObject_Class()
    With $cPaddle
        .X = $x
        .size = $size
        .dx = 20
        .__defineGetter("moveLeft", _moveLeft)
        .__defineGetter("moveRight", _moveRight)
    EndWith
    Return $cPaddle ; .Object
EndFunc   ;==>Paddle

Func _moveLeft($this)
    $this.parent.X -= $this.parent.dx
    If $this.parent.X < 0 Then $this.parent.X = 0
EndFunc   ;==>_moveLeft

Func _moveRight($this) ; , $maxX)
    Local $maxX = $this.arguments.values[0]
    Local $paddleWidth = $this.parent.size
    If $this.parent.X + $this.parent.dx + $paddleWidth <= $maxX Then
        $this.parent.X += $this.parent.dx
    Else
        $this.parent.X = $maxX - $paddleWidth
    EndIf
EndFunc   ;==>_moveRight

Func GamePanel()
    Local $hGUI = GUICreate("Ping Pong", 400, 300, -1, -1, $WS_SIZEBOX + $WS_SYSMENU)
    GUISetBkColor(0x000000)
    GUISetState()
    Local $aClient = WinGetClientSize($hGUI)
    If @error Then Return SetError(1, 0, 0)
    Local $iWidth = $aClient[0]
    Local $iHeight = $aClient[1]

    Local $aGDIMap[5]

    $aGDIMap[0] = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $aGDIMap[1] = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $aGDIMap[0])
    $aGDIMap[2] = _GDIPlus_ImageGetGraphicsContext($aGDIMap[1])
    $aGDIMap[3] = _GDIPlus_BrushCreateSolid($COLOR_RED)
    $aGDIMap[4] = _GDIPlus_HatchBrushCreate(4, $COLOR_GREEN)

    Local $Ball = Ball(40, 40)
    Local $paddleX = Paddle(150, 100)
    Local $cGamePanel = IDispatch() ; _AutoItObject_Class()

    With $cGamePanel
        .iWidth = $iWidth
        .iHeight = $iHeight
        .ball = $Ball
        .paddle = $paddleX
        .map = $aGDIMap
        .speedLevel = 100
        .__defineGetter("move", _move)
        .__defineGetter("drawStage", _drawStage)
        .__defineGetter("cleanUpResources", _cleanUpResources)
        .__destructor(_cleanUpResources)
        .__defineGetter("runGameLoop", _runGameLoop)
    EndWith
    Return $cGamePanel ; .Object
EndFunc   ;==>GamePanel

Func _move($this)
    Local $x = $this.parent.ball.X
    Local $y = $this.parent.ball.Y
    Local $dx = $this.parent.ball.dx
    Local $dy = $this.parent.ball.dy
    Local $Width = $this.parent.iWidth
    Local $Height = $this.parent.iHeight
    Local $BallSize = $this.parent.ball.size

    If $y + $dy >= ($Height - 40) And $x + $BallSize >= $this.parent.paddle.X And $x <= $this.parent.paddle.X + $this.parent.paddle.size Then
        $dy *= -1
    EndIf

    If $y + $dy <= 0 Then
        $dy = Abs($dy)
    EndIf

    If $y + $dy >= $Height - $BallSize Then
        MsgBox(0, "Game Over", "You missed the ball! Game Over!")
        Exit
    EndIf

    If $x + $dx <= 0 Then
        $dx = Abs($dx)
    EndIf

    If $x + $dx >= $Width - $BallSize Then
        $dx = -Abs($dx)
    EndIf

    $x += $dx
    $y += $dy
    $this.parent.ball.dx = $dx
    $this.parent.ball.dy = $dy
    $this.parent.ball.X = $x
    $this.parent.ball.Y = $y

    $this.parent.drawStage()
EndFunc   ;==>_move

Func _drawStage($this)
    Local $hGraphics = $this.parent.map[0]
    Local $hBitmap = $this.parent.map[1]
    Local $hGraphicsCtxt = $this.parent.map[2]
    Local $iX = $this.parent.ball.X
    Local $iY = $this.parent.ball.Y
    Local $iRadius = $this.parent.ball.size
    Local $padX = $this.parent.paddle.X
    Local $padH = $this.parent.iHeight - 40

    _GDIPlus_GraphicsClear($hGraphicsCtxt, 0xFF000000)
    _GDIPlus_GraphicsFillEllipse($hGraphicsCtxt, $iX - $iRadius, $iY - $iRadius, $iRadius * 2, $iRadius * 2, $this.parent.map[3])
    _GDIPlus_GraphicsFillRect($hGraphicsCtxt, $padX, $padH, $this.parent.paddle.size, 10, $this.parent.map[4])
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $this.parent.iWidth, $this.parent.iHeight)
EndFunc   ;==>_drawStage

Func _cleanUpResources($this)
    ConsoleWrite("clean up ressources...." & @CRLF)
    Local $map = $this.parent.map
    _GDIPlus_GraphicsDispose($map[0])
    _GDIPlus_BitmapDispose($map[1])
    _GDIPlus_GraphicsDispose($map[2])
    _GDIPlus_BrushDispose($map[3])
    $this.parent.map = 0
    _GDIPlus_Shutdown()
EndFunc   ;==>_cleanUpResources

Func _runGameLoop($this)
    Local $speedUpTime = 5000
    Local $lastMoveTime = TimerInit()
    Local $maxX = $this.parent.iWidth

    While 1
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop

        If _IsPressed(25) Then $this.parent.paddle.moveLeft()

        If _IsPressed(27) Then $this.parent.paddle.moveRight($maxX)

        If _IsPressed(53) Then
            While 1
                If _IsPressed(25) Or _IsPressed(27) Then ExitLoop
                Sleep(100)
            WEnd
        EndIf

        If TimerDiff($lastMoveTime) >= $speedUpTime Then
            $this.parent.speedLevel -= 5
            If $this.parent.speedLevel < 0 Then $this.parent.speedLevel = 0
            $lastMoveTime = TimerInit()
        EndIf

        $this.parent.move()

        Sleep($this.parent.speedLevel)
    WEnd
EndFunc   ;==>_runGameLoop

Func _ErrFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc


;================================================
Global $game = GamePanel()
$game.runGameLoop()
ConsoleWrite("------> the end <-------" & @CRLF)
; $game = 0
;=================================================

 

Edited by Gianni

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

The tests previously conducted to assess the performance of the UDFs AutoItObject.au3 and AutoItObject_Internal.au3 under various conditions have highlighted discernible differences, albeit subtle. The analysis of the collected data underscores the slight preference in terms of speed for AutoItObject.au3. While this difference may not be considerable, it remains non-negligible in certain usage contexts.

It is important to note that each UDF has its own advantages and disadvantages, which can influence their selection depending on the specific project requirements. The comparative table provided by @genius257  while informative, may spark debates and additional nuances regarding the evaluation of the performance and features of each UDF.

 

Unfortunately, I no longer have the test code available.

Edited by Numeric1
Link to comment
Share on other sites

Hi @Gianni 🙂,

AutoItObject_Internal is much slower than AutoitObject. An issue regarding speed exists on github: https://github.com/genius257/AutoItObject-Internal/issues/10

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