Okey, finally i got it working, thanks to your code, i used it as a blueprint. Here is how it looks, tell me what you think.
Code: Select all
; Pong game v 0.01
Macro EventSink()
ev = WindowEvent():While ev:If ev=#PB_Event_CloseWindow:End:EndIf:ev=WindowEvent():Wend
EndMacro
Structure paddle
x.i
y.i
speed.i
direction.i
EndStructure
Structure ball
x.d
y.d
x_vector.d
y_vector.d
EndStructure
Enumeration ;Windows and gadgets
#window
EndEnumeration
Enumeration ;Sprites
#paddle
#ball
EndEnumeration
Declare DrawingEngine()
Declare MoveEngine()
Declare NewBall()
InitSprite():InitKeyboard()
OpenWindow(#window, 0, 0, 800, 300, "Pong Game", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#window), 0, 0, 800, 300, 1, 0, 0)
; Create the object faces to display
CreateSprite(#paddle,6,50)
CreateSprite(#ball,6,6)
StartDrawing(SpriteOutput(#paddle))
Box(0,0,SpriteWidth(#paddle), SpriteHeight(#paddle), #Yellow)
StopDrawing()
StartDrawing(SpriteOutput(#ball))
Box(0,0,SpriteWidth(#ball), SpriteHeight(#ball), #White)
StopDrawing()
; Create the objects
Global userpaddle.paddle
With userpaddle
\x = 20
\y = 125
\speed = 3
EndWith
Global aipaddle.paddle
With aipaddle
\x = 775
\y = 125
\speed = 2
\direction = 0
EndWith
Global ball.ball
NewBall()
Repeat
EventSink()
MoveEngine()
DrawingEngine()
ForEver ; MoveEngine() or EventSink() will end the program
End
;=========================================
; Procedures
;=========================================
Procedure DrawingEngine()
ClearScreen(0)
DisplaySprite(#paddle,userpaddle\x,userpaddle\y)
DisplaySprite(#paddle,aipaddle\x,aipaddle\y)
DisplaySprite(#ball,ball\x,ball\y)
FlipBuffers()
EndProcedure
Procedure NewBall()
If Random(1)
direction = 90 ; Right
Else
direction = 270 ; Left
EndIf
If Random(1)
angle.d = direction+5+Random(50) ; Up
Else
angle.d = direction-5-Random(50) ; Down
EndIf
With ball
\x=WindowWidth(#window)/2-4 : \y=WindowHeight(#window)/2-4
\x_vector=0.0 + 2 * Sin(angle* #PI / 180.0)
\y_vector=0.0 + 2 * Cos(angle* #PI / 180.0)
EndWith
EndProcedure
Procedure.i MoveEngine()
; User Paddle
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
userpaddle\y-userpaddle\speed
ElseIf KeyboardPushed(#PB_Key_Down)
userpaddle\y+userpaddle\speed
ElseIf KeyboardPushed(#PB_Key_Left)
userpaddle\x-userpaddle\speed
ElseIf KeyboardPushed(#PB_Key_Right)
userpaddle\x+userpaddle\speed
ElseIf KeyboardPushed(#PB_Key_Q)
End
EndIf
; Computer Paddle
With aipaddle
If \direction = 0
\y- \speed : If \y <0 : \y = 0 : \direction = 1 : EndIf
ElseIf \direction = 1
\y+ \speed : If \y >250 : \y = 250 : \direction = 0 : EndIf
EndIf
If Abs(\x - ball\x) < 7: \y = ball\y-20 : ball\x_vector = 0-ball\x_vector:EndIf ; Simple AI to make sure the computer doesn't miss (for now)
EndWith
; Ball
ball\x + ball\x_vector
ball\y + ball\y_vector ; For now we won't worry about angles, just go back and forth
If SpriteCollision(#ball, ball\x, ball\y, #paddle, userpaddle\x, userpaddle\y)
ball\x_vector=0-ball\x_vector
ball\y_vector= ball\y_vector + userpaddle\speed
ElseIf SpriteCollision(#ball, ball\x, ball\y, #paddle, aipaddle\x, aipaddle\y)
ball\x_vector=0-ball\x_vector
ball\y_vector= ball\y_vector + aipaddle\speed
EndIf
; Collision!
If userpaddle\y <= 0
userpaddle\y = 0
ElseIf userpaddle\y >= 250
userpaddle\y = 250
EndIf
If ball\y <= 0
ball\y_vector = -(ball\y_vector)
ElseIf ball\y >= 294
ball\y_vector = -(ball\y_vector)
EndIf
If ball\x <= 0
MessageRequester("Game Over", "You did it wrong...", 0)
End
EndIf
EndProcedure
Actually i see i made a mistake there with the ball's vectors, so the good version looks like this:
Code: Select all
; Ball
ball\x + ball\x_vector
ball\y + ball\y_vector ; For now we won't worry about angles, just go back and forth
If SpriteCollision(#ball, ball\x, ball\y, #paddle, userpaddle\x, userpaddle\y)
ball\x_vector=0-ball\x_vector
ball\y_vector= ball\y_vector + ((userpaddle\speed) * userpaddle\direction)
ElseIf SpriteCollision(#ball, ball\x, ball\y, #paddle, aipaddle\x, aipaddle\y)
ball\x_vector=0-ball\x_vector
ball\y_vector= ball\y_vector + ((aipaddle\speed) * aipaddle\direction)
EndIf