;controls are 8 on num pad for up
;2 on num pad for down
; sets the graphics mode
Graphics 640,480
; lets windows know the name of the program
AppTitle "Pong"
;does everything in the background ready to show when "flip" is used
SetBuffer BackBuffer()
;loads the pictures we'll need to use
Ball = LoadImage("ball.bmp")
Player=LoadImage ("bat.bmp")
Opp=LoadImage("bat.bmp")
;sets up the settings for the start:
;player location
Player_x=20
Player_y=420
;opponant location
Opp_x=600
Opp_y=420
;starting speed
SpeedX = 5
SpeedY = 5
;ball location
Ball_x = 5
Ball_y = 5
;players score
p_score=0
;opponants score
o_score=0
;while esc. key isn't pressed
While Not KeyHit(1)
;if the 8(up) key on num. pad is pressed
If KeyDown (12)
;Moves 4 up the y axis
Player_y=Player_y-4
;If y axis is less than zero then change to zero (stops it going
;off screen)
If Player_y<0 Then Player_y=0
;closes the if command
EndIf
;if the 2(down) key on num. pad is pressed
If KeyDown (13)
;Moves 4 Down the y axis
Player_y=Player_y+4
;If y axis is more than 400 then change to 400 (stops it going
;off screen)
If Player_y>400 Then Player_y=400
;closes the if command
EndIf
; If the player's bat hits the ball then make the ball go a little faster
If ImagesCollide(Player,Player_x,Player_y,0,Ball,Ball_x,Ball_y,0) Then SpeedX= SpeedX-(2*SpeedX)
; If the opponent's bat hits the ball then make the ball go a little faster
If ImagesCollide(Ball,Ball_x,Ball_y,0,Opp,Opp_x,Opp_y,0) Then SpeedX= SpeedX-(2*SpeedX)
; If the ball goes behind the opponent's bat then add 10 to player's
; score
If Ball_x<1 Then p_score=p_score+10
; If the ball goes behind the player's bat then add 10 to player's
; score
If Ball_x>600 Then o_score=o_score+10
; Clear The Screen
Cls
; Stops the bat going off the screen
If Opp_y>400 Then Opp_y=400
; Stops the bat going off the screen
If Opp_y<0 Then Opp_y=0
;sets a random command to generate a number between 1 and 3
random = Rand(1,3)
;if the ball touches the opponant
If Ball_y>Opp_y Then
;is the random number one if so make the bat stay where it is
If random = 1 Then Opp_y=Opp_y
;otherwise the bat is moved down by five
Else Opp_y=Opp_y-5
;Closes the if command
End If
;if the ball touches the opponant
If Ball_y>Opp_y Then
;is the random number one if so make the bat stay where it is
;otherwise the bat is moved down by five
If random = 1 Then Opp_y = Opp_y Else Opp_y=Opp_y+5
;Closes the if command
End If
;Draw the ball
DrawImage Ball,Ball_x,Ball_y
;If the ball goes off the screen either side move it back to starting
;speed and position
If Ball_x<1 Or Ball_x>GraphicsWidth()-40 Then Ball_x=5 Ball_y=5 SpeedX=5 SpeedY=5
If Ball_y<1 Or Ball_y>GraphicsHeight()-40 Then SpeedY =- SpeedY
;Ball location is current location plus the speed it is travelling at
Ball_x = Ball_x + SpeedX
Ball_y = Ball_y + SpeedY
;draws the bat of the Player
DrawImage Player,Player_x,Player_y
;draws the bat of the Opponent
DrawImage Opp,Opp_x,Opp_y
;prints the opponents's score
Text 300,0,o_score,1
;prints the players score
Text 340,0,p_score,1
;displays the BackBuffer
Flip
;closes the while command
Wend
;end of code
End |