;--------------------------
;- PONG -
;--------------------------
;Graphics
Graphics 640, 480
SetBuffer = BackBuffer()
;The Player types
Type Player
Field x,y
Field score
End Type
Type Ball
Field x,y
Field xv, yv
End Type
;Constants
Const ScreenX=640
Const ScreenY=480
Const BallStarty = 300
Const BallStartx = 300
Const humanspeed = 5
Const computerspeed = 3
Const ESCKEY = 1, UPKEY = 200, DOWNKEY = 208, PAUSEKEY = 25
;Initialization
SeedRnd(MilliSecs())
Global continue = 1
Global pause = 0
Global player1.Player = New Player
Global player2.Player = New Player
Global ball.Ball = New Ball
Global player1image = LoadImage("Paddle.bmp")
Global player2image = LoadImage("Paddle.bmp")
Global ballimage = LoadImage("Ball.bmp")
MaskImage(ballimage, 128, 255, 255)
AutoMidHandle True
ball\x = BallStartx
ball\y = BallStarty
player1\x=50
player2\x=ScreenX-50
player1\y=ScreenY/2
player2\y=ScreenY/2
ball\yv = -4
ball\xv = -4
;Main
Text (ScreenX/2)-100, ScreenY/2-50, "Welcome to PONG by InuTracy"
WaitKey
InitializeLevel()
Function InitializeLevel()
ball\x = BallStartx
ball\y = BallStarty
player1\x=50
player2\x=ScreenX-50
player1\y=ScreenY/2
player2\y=ScreenY/2
ball\yv = -4
ball\xv = -4
While Not KeyDown(1)
If pause=0
Cls
DrawImage (player1image, player1\x, player1\y)
DrawImage (player2image, player2\x, player2\y)
DrawImage (ballimage, ball\x, ball\y)
KeyInput()
TestAI()
TestBall()
DrawHUD()
Flip
Else
Cls
KeyInput()
Text ScreenX/2, ScreenY/2, "PAUSED"
Text (ScreenX/2)-60, (ScreenY/2)+10, "Press P to Unpause the game."
Flip
EndIf
Wend
End Function
;Functions
Function KeyInput()
If KeyDown(UPKEY)
player1\y = player1\y - humanspeed
If player1\y <= 0
player1\y = 0
End If
ElseIf KeyDown(DOWNKEY)
player1\y = player1\y + humanspeed
If player1\y >= ScreenY
player1\y = ScreenY
EndIf
ElseIf KeyHit(PAUSEKEY)
If pause=0
pause=1
Else
pause=0
EndIf
End If
End Function
Function DrawHUD()
Text 295, 10, "P2 y Position: " + player2\y
Text 295, 20, "Ball Y Position: " + ball\y
Text 295, 30, "Score: " + player1\score + "/" + player2\score
End Function
Function TestAI()
If player2\y < ball\y
player2\y = player2\y + computerspeed
ElseIf player2\y > ball\y
player2\y = player2\y - computerspeed
EndIf
End Function
Function TestBall()
If ImagesOverlap(ballimage, ball\x, ball\y, player1image, 50, player1\y)
ball\yv = -ball\yv Rand(-4, 4)
ball\xv = ball\xv Rand(-4, 4)
ElseIf ImagesOverlap(ballimage, ball\x, ball\y, player2image, ScreenX-50, player1\y)
ball\yv = -ball\yv Rand(-4, 4)
ball\xv = ball\xv Rand(-4, 4)
ElseIf ball\y<=0
ball\yv = -ball\yv Rand(-1, 1)
ball\xv = ball\xv Rand(-1, 1)
ElseIf ball\y>=ScreenY
ball\yv = -ball\yv Rand(-1, 1)
ball\xv = ball\xv Rand(-1, 1)
ElseIf ball\x<=0
Cls
player2\score = player2\score + 1
Text (ScreenX/2)-120, ScreenY/2, "Player 2 Scored! Reseting in 5 seconds."
Delay 5000
InitializeLevel()
ElseIf ball\x>=ScreenX
Cls
player1\score = player1\score + 1
Text (ScreenX/2)-120, ScreenY/2, "Player 1 Scored! Reseting in 5 seconds."
Delay 5000
InitializeLevel()
EndIf
ball\x = ball\x + ball\xv
ball\y = ball\y + ball\yv
End Function
ID:186353
![]() Jul 16 2005, 7:34 pm
|
|
I've been programming a Pong game in Blitz basic but I need some help. I made this from scratch and am having a bit of trouble with the actual ball AI...I could use some big help. When it hits the paddle it seems to sort of just hit the paddle and go diagnol in the same direction. If someone could please help me I could use some help with the ball AI or if someone has a new ball AI suggestion...please do. If you need to I can upload the Blitz Basic compiler demo since it's not offered anymore. All of the ball collision code is in TestBall().
|