Pong
Posted: Sun Jul 10, 2016 12:56 pm
Code: Select all
playerScore = 0 ! comScore = 0 ! startSpeed = 0.1
SPRITE 0 LOAD "Paddle@2x.png" ! SPRITE 0 SHOW
SPRITE 1 LOAD "Paddle@2x.png" ! SPRITE 1 SHOW
SPRITE 2 LOAD "white_box.png" ! SPRITE 2 SHOW
init:
GRAPHICS
REFRESH
GRAPHICS CLEAR 0, 0.4, 0
OPTION SPRITE POS CENTRAL
OPTION ANGLE DEGREES
RANDOMIZE
DRAW FONT NAME "AppleColorEmoji"
DRAW FONT SIZE 50
DRAW TEXT playerScore AT SCREEN_WIDTH()/4, 100
DRAW TEXT comScore AT SCREEN_WIDTH()/1.5, 100
FOR y = 0 TO SCREEN_HEIGHT() STEP 40
FILL RECT SCREEN_WIDTH()/2, y SIZE 2, 5
NEXT y
player_x = 100 ! player_y = SCREEN_HEIGHT()/2
com_x = SCREEN_WIDTH() - 100 ! com_y = SCREEN_HEIGHT()/2
ball_x = SCREEN_WIDTH()/2 ! ball_y = SCREEN_HEIGHT()/2
ballSpeed_x = 0 ! ballSpeed_y = 0 !
IF startSpeed < 0.5 THEN
startSpeed+= 0.05
ENDIF
DIM touches(3) ! game = 0
DEF constrain(v, x, y)
var = v
IF var < x THEN
var = x
ENDIF
IF var > y THEN
var = y
ENDIF
RETURN var
ENDDEF
DEF range(var, x, y)
bool = 0
IF var >= x THEN
IF var < y THEN
bool = 1
ENDIF
ENDIF
RETURN bool
ENDDEF
DEF hitPaddle(x, y)
IF range(.ball_x, x - 15, x + 15) = 1 THEN
IF range(.ball_y, y - 64, y + 64) = 1 THEN
.ballSpeed_x *= -1
.ballSpeed_y = (.ball_y - y) / 300
ENDIF
ENDIF
ENDDEF
1 SPRITE 0 AT player_x, player_y SCALE 2, 0.5 ANGLE 90
SPRITE 1 AT com_x, com_y SCALE 2, 0.5 ANGLE 90
SPRITE 2 AT ball_X, ball_y SCALE 0.5
touches(0) = touches(1)
touches(1) = touches(2)
GET TOUCH 0 AS x, y
touches(2) = y
delta = touches(2) - touches(1)
IF touches(1) > 0 AND touches(2) > 0 THEN
player_y += delta
ENDIF
IF ball_y < com_y - 40 THEN ! com_y -= startSpeed / 3 ! ENDIF
IF ball_y > com_y + 40 THEN ! com_y += startSpeed / 3 ! ENDIF
IF game = 0 THEN
IF y <> -1 THEN
game = 1
IF RND(2) = 0 THEN
ballSpeed_x = -startSpeed
ELSE
ballSpeed_x = startSpeed
ENDIF
ENDIF
ENDIF
player_y = constrain(player_y, 50, SCREEN_HEIGHT() - 50)
com_y = constrain(com_y, 50, SCREEN_HEIGHT() - 50)
IF game = 1 THEN
ball_x += ballSpeed_x
ball_y += ballSpeed_y
ENDIF
CALL hitPaddle(player_x, player_y) ! CALL hitPaddle(com_x, com_y)
IF ball_y < 10 OR ball_y > SCREEN_HEIGHT() - 10 THEN
ballSpeed_y *= -1
ENDIF
IF ball_x < -10 THEN
comScore +=1
GOTO init
ENDIF
IF ball_x > SCREEN_WIDTH() + 10 THEN
playerScore +=1
GOTO init
ENDIF
GOTO 1