I really need help with a pong game im making I want to add levels, scoring systems, effects, and items! if you could help that would be great here is my code!
Rem Project: Pong
Rem Created: Sunday, January 25, 2009
Rem ***** Main Source File *****
`Setup the display
flush video memory ` Clear everything from the video cards memory.
sync on : sync rate 60 ` Set the frames per second to 60
set display mode 640,480,32 ` Set the resolution to 1024x768x32
color backdrop rgb(128,128,128) ` Color the background to a dark gray.
`A label that'll take us to the games start.
GameStart:
sync ` Refresh the screen
center text screen width()*.5,screen height()*.5,"Press any key to begin"
sync ` Refresh the screen again to ensure text is displayed.
wait key ` Wait for a keypress to start the program.
`Setting up the grid
rows=6 ` Will setup how many rows(vertical) of blocks we have.
columns=14 ` Sets up how many columns(horizontal) of blocks we have.
totalobjects=(rows*columns)+rows ` We define the total Objects variable, that tells us
` how many objects we have. This will later be used, so
` we'll be able to tell how many blocks need to be removed
` to win the game.
```Creating the objects```
` This for-next loop will create a cube, randmoize the color, and then position it
` using the modify and modifier variable.
modify=0 ` Ensure the variable is 0
modifier=0 ` Ensure the variable is 0
for x = 1 to totalobjects
make object cube x,1
color object x,rgb(rnd(255),0,0)
position object x,modify-(columns*.5),modifier+rows-1.5,8
modify=modify+1
if modify>columns ` If the modify variable is greater than the columns, add 1 to
modify=0 ` the modifier variable so a new row will start.
modifier=modifier-1
endif
next x
```Creating the ball and paddle```
paddle=totalobjects+1 `The paddles object id
ball=totalobjects+2 `The balls object id
`The paddle
make object cube paddle,1 ` Create the paddle
scale object paddle,200,100,100 ` Scale it. Notice the x and z values are increased and
` the y value stays the same, so we have a longer sleaker
` paddle.
position object paddle,0,-6,8 ` Position it
`the sphere
make object sphere ball,0.5 ` Make the sphere
position object ball,0,-4,8 ` position it
```Setting up collision```
`Add collision to all the blocks and the paddle
for x = 1 to paddle
set object collision on x
set object collision to boxes x
next x
`Add collision to the ball
set object collision on ball
set object collision to spheres ball
`The game variables
score=0 ` Set the score to zero.
direction$="down" ` This string defines the direction of the ball. When the ball
` encounters a block or the paddle, this variable will be modified
` to change directions.
angle$="none" ` This works the same way as the direction$ variable, but it defines
` the angle of the ball instead.
ballspeed#=0.120 ` Define the ball speed.
paddlespeed#=0.4 ` Define the paddle speed(this is you).
hide mouse ` Because it's ugly ;)
set text size 30 ` Set the text size
ink rgb(0,0,0),1 ` Set the ink color which will be applied to our text.
Do ` begin our loop
```Display```
`Center our score in the bottom center of our screen.
center text screen width()*.5,screen height()-40,str$(score)
```Paddle Movement```
if leftkey()=1 then move object left paddle,paddlespeed#
if rightkey()=1 then move object right paddle,paddlespeed#
````Ball Movement```
` Based on the direction variable, the ball is moved up our down.
if direction$="down"
move object down ball,ballspeed#
endif
if direction$="up"
move object up ball,ballspeed#
endif
` Move the ball based on the angle.
if angle$="left"
move object left ball,ballspeed#*.4
endif
if angle$="right"
move object right ball,ballspeed#*.4
endif
````collision```
`collision between ball and paddle
if object collision(paddle,ball)=1
if direction$="up" then direction$="down" ` If the ball collides with the paddle, switch
if direction$="down" then direction$="up" ` its direction.
` If the paddles x position is greater than the balls, then angle the ball left.
if object position x(paddle)>object position x(ball)
angle$="left"
endif
` If the paddles x position is less than the balls, then angle the ball right.
if object position x(paddle)<object position x(ball)
angle$="right"
endif
` If the paddles x position is equal to the balls, then the ball's angle is zero.
if object position x(paddle)=object position x(ball)
angle$="none"
endif
endif
`collision between ball and blocks
for x = 1 to totalobjects
if object exist(x) ` If the object exists and collision occurs
if object collision(x,ball)=1 ` delete the object and add to the score.
delete object x
score=score+1
if direction$="down" then direction$="up" ` Switch the balls direction upon collision.
if direction$="up" then direction$="down"
endif
endif
next x
```Ball and paddle boundaries```
` Paddle boundaries
` If the paddle is outside these screen coordinates, move it the other direction.
if object position x(paddle)>7 then move object left paddle , paddlespeed#
if object position x(paddle)<-7 then move object right paddle , paddlespeed#
` If the ball is outside these screen coordinates, then move it the opposite direction.
if object position x(ball)>7 and direction$="up"
angle$="left"
endif
if object position x(ball)<-7 and direction$="up"
angle$="right"
endif
if object position x(ball)>7 and direction$="down"
angle$="left"
endif
if object position x(ball)<-7 and direction$="down"
angle$="right"
endif
if object position y(ball)>5 then direction$="down"
```Winning the game```
` Once the score matches the total number of objects, you win, because there's nothing
` left to hit. From there, every object is deleted if it exists, and then the game restarts.
if score=totalobjects
center text screen width()*.5,screen height()*.5,"You Win!!"
for x = 1 to totalobjects+2
if object exist(x) then delete object x
next x
goto GameStart
endif
```Losing the game```
` If the balls position falls beneath the paddle, then you lose the game and must restart.
` All the objects are deleted, and you're sent to the GameStart label where everything is
` reset.
if object position y(ball)<-7
center text screen width()*.5,screen height()*.5,"GAME OVER!!"
for x = 1 to totalobjects+2
if object exist(x) then delete object x
next x
goto GameStart
endif
```Debug```
` It's always a good idea to have some sort of debugger in your game. This way, you'll be
` able to tell what exactly is going on. It's usually a good idea to hide this from your
` players, as you're the only one who really needs to see it. So set it to a key a player
` normally wouldn't press, or you can implement some kind of console like in
` first person shooters.
if keystate(15)=1 ` The tab key
text 0,00,"-Debug-"
text 0,30,"Paddle: "+str$(object position x(paddle))
text 0,60,"Ball: "+str$(object position x(ball))
text 0,90,"FPS: "+str$(screen fps())
endif
sync ` Refresh the screen
loop ` Go back to our do.
I am new to the coding so could you also explain it?!
JG