Jump to content

Recommended Posts

Posted (edited)

Updated script !

I think its the Execute line that doesnt seem to be correct for calculate contact because sometimes it works, sometimes not !

$contact = Execute($yball - $p_1p);calculate contact
MsgBox(64, '$contact p_1', $contact)

$contact = Execute($yball - $p_2p);calculate contact
MsgBox(64, '$contact p_2', $contact)

Cheers, FireFox.

Edited by FireFox
Posted (edited)

Fixed bug with contact line and now game works fine but... the window and graphics flash and the game is more slow than before :) ....

If someone have better idea for non flashing graphics and for seeing ball moving without see these "shallow", I would appreciate it ! :o

Hotkeys :

ESC = Exit

Z = up p1

S = down p1

Space = release ball p1

Up arrow = up p2

Down arrow = down p2

Left arrow = release ball p2

This is my update code :

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IsPressed_UDF.au3>
#include <GDIPlus.au3>

Local $p_1p = 40, $p_2p = 40, $direction = True, $boundaries = True, $xball = 1, $yball = 1, $gf = 2, $win = 1
Dim $points[2] = [0, 0]

Opt("GuiOnEventMode", 1)

#Region GUI
$GUI = GUICreate("GDI+", 700, 500, -1, -1, $WS_POPUP + $WS_BORDER)
GUISetBkColor(0x000000, $GUI)
GUISetState(@SW_SHOW, $GUI)

$p_1 = GUICtrlCreateGraphic(0, 0, 700, 500) ;draw p_1
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFFFF, 0xFFFFFF)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 15, 216, 10, 60)
;~ GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE)
GUICtrlSetPos($p_1, 10, $p_1p) ;place p_1

$p_2 = GUICtrlCreateGraphic(0, 0, 700, 500) ;draw p_2
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFFFF, 0xFFFFFF)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 500, 216, 10, 60)
;~ GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE)
GUICtrlSetPos($p_2, 165, $p_2p) ;place p_2

$ball = GUICtrlCreateGraphic(0, 0, 700, 500) ;draw ball
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFFFF, 0xFFFFFF)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 300, 100, 15, 15)
GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
;~ GUICtrlSetGraphic(-1, $GUI_GR_CLOSE)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE)

;scores
_Graphicdraw(300, 5, 5, 40) ;draw right line
_Graphicdraw(390, 5, 5, 40) ;draw left line
_Graphicdraw(300, 45, 95, 5) ;draw bottom line
$p_1s = GUICtrlCreateLabel("0",310,7,35,35)
GUICtrlSetColor($p_1s,0xFFFFFF)
GUICtrlSetFont($p_1s,20,400,0,'Comic Sans MS')
$p_2s = GUICtrlCreateLabel("0",355,7,35,35)
GUICtrlSetColor($p_2s,0xFFFFFF)
GUICtrlSetFont($p_2s,20,400,0,'Comic Sans MS')

;outline
_Graphicdraw(345, 0, 5, 500) ;draw center line
_Graphicdraw(0, 0, 5, 500) ;draw left line
_Graphicdraw(0, 0, 700, 5) ;draw top line
_Graphicdraw(695, 0, 5, 500) ;draw right line
_Graphicdraw(0, 495, 700, 5) ;draw bottom line
#EndRegion GUI

HotKeySet("{ESC}", "_Exit")


Func _Graphicdraw($left, $top, $width, $height)
    GUICtrlCreateGraphic(0, 0, 700, 500)
    GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFFFF, 0xFFFFFF)
    GUICtrlSetGraphic(-1, $GUI_GR_RECT, $left, $top, $width, $height)
;~  GUICtrlSetGraphic(-1, $GUI_GR_REFRESH)
    GUICtrlSetGraphic(-1, $GUI_GR_MOVE)
EndFunc   ;==>_Graphicdraw

Func _Exit()
    Exit
EndFunc   ;==>_Exit


While 1
    Sleep(40)
    If ($direction) Then ;move ball to right
        $xball += $gf * 2
        GUICtrlSetPos($ball, $xball, $yball)
        GUICtrlSetGraphic($ball, $GUI_GR_REFRESH)
    Else
        $xball -= $gf * 2 ;move ball to left
        GUICtrlSetPos($ball, $xball, $yball)
        GUICtrlSetGraphic($ball, $GUI_GR_REFRESH)
    EndIf
    If ($boundaries) Then ;move ball to top
        $yball += $gf * 3
        GUICtrlSetPos($ball, $xball, $yball)
        GUICtrlSetGraphic($ball, $GUI_GR_REFRESH)
    Else
        $yball -= $gf * 3 ;move ball to bottom
        GUICtrlSetPos($ball, $xball, $yball)
        GUICtrlSetGraphic($ball, $GUI_GR_REFRESH)
    EndIf
    If ($yball > 350) Then $boundaries = False ;the ball hit the bottom
    If ($yball < -70) Then $boundaries = True ;the ball hit the top
    If (($xball >= 350) And ($direction)) Then ;the ball is in the line of right player
        $contact = $yball - $p_2p ;calculate contact
        ConsoleWrite('$contact p_2 >> $yball : ' & $yball & ' //  $p_2p : ' & $p_2p & ' //  $contact : ' & $contact & @CRLF)
        If (($contact > 88) And ($contact < 180)) Then ;check if there is contact
            $direction = False ;move the ball to left
        Else ;no contact, right player lost
            $points[1] += 1 ;+1 pt for player 2
            GUICtrlSetData($p_2s,$points[1]) ;update score
            $gf = 0 ;stop ball move
            $xball = 345 ;replace xball
            $yball = 130 ;replace yball
            $direction = False ;move the ball to left
            $win = 3 ;active restart ball
            ConsoleWrite('win p_2 >> $points[0] : ' & $points[0] & ' //  $points[1] : ' & $points[1] & @CRLF)
        EndIf
    EndIf
    If (($xball <= -268) And (Not ($direction))) Then ;the ball is in the line of left player
        $contact = $yball - $p_1p ;calculate contact
        ConsoleWrite('$contact p_1 >> $yball : ' & $yball & ' //  $p_1p : ' & $p_1p & ' //  $contact : ' & $contact & @CRLF)
        If (($contact > 100) And ($contact < 180)) Then ;check if there is contact
            $direction = True ;move the ball to right
        Else
            $points[0] += 1 ;+1 pt for player 1
            GUICtrlSetData($p_1s,$points[0]) ;update score
            $gf = 0 ;stop ball move
            $xball = -258 ;replace xball
            $yball = 130 ;replace yball
            $direction = True ;move the ball to right
            $win = 2 ;active restart ball
            ConsoleWrite('win p_1 >> $points[0] : ' & $points[0] & ' //  $points[1] : ' & $points[1] & @CRLF)
        EndIf
    EndIf

    ;Keys for move players
    If _IsAndKeyPressed("26|5A") Then
        If $p_2p >= -190 Then
            $p_2p -= 10
            GUICtrlSetPos($p_2, 165, $p_2p)
        EndIf
        If $p_1p >= -190 Then
            $p_1p -= 10
            GUICtrlSetPos($p_1, 10, $p_1p)
        EndIf
    ElseIf _IsAndKeyPressed("26|53") Then
        If $p_2p >= -190 Then
            $p_2p -= 10
            GUICtrlSetPos($p_2, 165, $p_2p)
        EndIf
        If $p_1p <= 200 Then
            $p_1p += 10
            GUICtrlSetPos($p_1, 10, $p_1p)
        EndIf
    ElseIf _IsAndKeyPressed("28|5A") Then
        If $p_2p <= 200 Then
            $p_2p += 10
            GUICtrlSetPos($p_2, 165, $p_2p)
        EndIf
        If $p_1p >= -190 Then
            $p_1p -= 10
            GUICtrlSetPos($p_1, 10, $p_1p)
        EndIf
    ElseIf _IsAndKeyPressed("28|53") Then
        If $p_2p <= 200 Then
            $p_2p += 10
            GUICtrlSetPos($p_2, 165, $p_2p)
        EndIf
        If $p_1p <= 200 Then
            $p_1p += 10
            GUICtrlSetPos($p_1, 10, $p_1p)
        EndIf
    ElseIf (_IsPressed("26") And $p_2p >= -190) Then
        $p_2p -= 10
        GUICtrlSetPos($p_2, 165, $p_2p)
    ElseIf (_IsPressed("28") And $p_2p <= 200) Then
        $p_2p += 10
        GUICtrlSetPos($p_2, 165, $p_2p)
    ElseIf (_IsPressed("5A") And $p_1p >= -190) Then
        $p_1p -= 10
        GUICtrlSetPos($p_1, 10, $p_1p)
    ElseIf (_IsPressed("53") And $p_1p <= 200) Then
        $p_1p += 10
        GUICtrlSetPos($p_1, 10, $p_1p)
    ElseIf (_IsPressed("25") And $win = 3) Then
        $win = 1
        $gf = 2
    ElseIf (_IsPressed("20") And $win = 2) Then
        $win = 1
        $gf = 2
    EndIf
WEnd


Func _OnAutoItExit()
    Exit
EndFunc   ;==>_OnAutoItExit

Cheers, FireFox.

Edited by FireFox
Posted

@CoolPenguin

Thanks, I will take a look :o What I need is more stability about ctrl refreshing ... :)

Cheers, FireFox.

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
×
×
  • Create New...