Page 1 of 2

Pong

Posted: Sun Jul 10, 2016 12:56 pm
by Raspberrypi

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

Re: Pong

Posted: Sun Jul 10, 2016 10:55 pm
by rbytes
This should be fun. But it won't run without the graphic images - the paddle and the white box. I hope you will post them. :D

Re: Pong

Posted: Tue Aug 09, 2016 1:44 pm
by Dutchman
I think that the code is copied from https://books.google.nl/books?id=o2X0ui ... ng&f=false
But where are the images :?: :D

Re: Pong

Posted: Tue Aug 09, 2016 3:22 pm
by rbytes
It has been almost a month since I asked Raspberrypi to post them. I think we will have to supply them ourselves.

You are good with game graphics. We just need a paddle and a ball :lol:

Re: Pong

Posted: Tue Aug 09, 2016 7:38 pm
by Dutchman
I have stolen my graphics from internet, that's why they look so good. :lol:
I will try to make 'Pong' complete. It remembers me of long ago.

Re: Pong

Posted: Wed Aug 10, 2016 12:13 am
by rbytes
Same here. I started with a Commodore Vic 20, moved to a C64, then an Atari ST and then a couple of Amiga computers. Even today I marvel at what the professional Amiga artists could create with fewer, larger pixels and a fraction of the colors we have now.

Here is a self-portrait by Jim Sachs - one of the best of that elite group.

Pong brought to life

Posted: Wed Aug 10, 2016 11:41 am
by Dutchman
I have brought Pong to life: ;)
https://www.dropbox.com/sh/ntucli2k8nql ... Ev_2a?dl=0
With 'large' ball it was easy to win, with smaller ball it will need some training. :lol:
Who will add sound? :D
won with large ball.PNG
won with large ball.PNG (84.39 KiB) Viewed 3746 times
tie played with small ball.PNG
tie played with small ball.PNG (82.67 KiB) Viewed 3746 times

Re: Pong, fit for launcher

Posted: Wed Aug 10, 2016 2:02 pm
by Dutchman
I have improved the structure of pong and adapted for use with 'Launcher'
See viewtopic.php?f=20&t=1499
There is still no sound. Who cares? :D
new Pong during run.PNG
new Pong during run.PNG (16.51 KiB) Viewed 3741 times

Re: Pong

Posted: Wed Aug 10, 2016 2:42 pm
by rbytes
Looks like fun! I will see if I can find some good sound effects.

Pong with conditional launcher

Posted: Wed Aug 10, 2016 4:28 pm
by Dutchman
I corrected an error if the program was started with the device in vertical position. :oops:
Furthermore the launcher will be called conditionally.
The variable 'Launcher' at the beginning of the program sets the condition.