TGC Codebase Backup



"Classic" Pong by Anonymous Coder

31st May 2006 17:51
Summary

The "classic" pong game. Found the base for the game on the forums and decided to edit it to turn it into the classic version. Thanks to the guy who originally did the co



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart

DarkBasic Classic Pong Game

Created by CuCuMBeR 2002

Edited by Fallenshad0w 2006

Found this code on the forums and decided to edit it to the "classic" pong game.

Controls are as follows....

P = Pause
R = Resume
Keyboard Controls Player (Up and Down arrows)

remend

`________________________________________________________________________________________

sync on : sync rate 0

set display mode 800,600,32
backdrop on : color backdrop rgb(0,0,0)
hide mouse



`position variables
boardcenterx=395 : boardtopy=205 : boardbottomy=370

`player 1 image variables
left=0 : top=0 : right=5 : bottom=30
player1image=1

`computer player image variables
`use same as player, just define new image for computer player
computerplayerimage=4

`bounding box variables
`top of screen and bottom
boundleft=0 : boundtop=0 : boundright=400 : boundbottom=5
boundtopbottom=2

`left and right side of screen
sideleft=0 : sidetop=0 : sideright=5 : sidebottom=200
boundleftright=3

`middle line for court division
midleft=0 : midtop=0 : midright=5 : midbottom=5
midline=10

`pong ball variables for size
ballleft=0 : balltop=0 : ballright=5 : ballbottom=5
ballimage=5

`create player image
ink rgb(255,255,255),1
box left, top, right, bottom
get image player1image, left, top, right, bottom
ink rgb(255,255,255),1

`create the computer opponent image
ink rgb(255,255,255),1
box left, top, right, bottom
get image computerplayerimage, left, top, right, bottom
ink rgb(255,255,255),1

`create dotted court division line
ink rgb(255,255,255),1
box midleft, midtop, midright, midbottom
get image midline, midleft, midtop, midright, midbottom
ink rgb(255,255,255),1

`create bounding boxes for the play area (top and bottom)
box boundleft, boundtop, boundright, boundbottom
get image boundtopbottom, boundleft, boundtop, boundright, boundbottom

`create bounding boxes for the play area (left and right)
box sideleft, sidetop, sideright, sidebottom
get image boundleftright, sideleft, sidetop, sideright, sidebottom

`create ball for playing
box ballleft, balltop, ballright, ballbottom
get image ballimage, ballleft, balltop, ballright, ballbottom

`position the bounding boxes for the players area (top and bottom)
sprite 2, 200, 200, boundtopbottom
sprite 3, 200, 400, boundtopbottom

`position the bounding boxes for the players area (left and right)
sprite 4, 200, 200, boundleftright
sprite 6, 595, 200, boundleftright

`position the dotted lines down the center
for dots=10 to 205 step 10
     sprite dots, boardcenterx, 195+dots, midline
next dots

`set player 1 initial starting point in the play area
playerx#=256 : playery#=206

`set computer player initial starting point in the play area
computerplayerx#=539 : computerplayery#=206

`set ball initial starting point
ballpositionx#=390 : ballpositiony#=290

`ball variables for x and y coordinate speeds to give angled rebounds
ballmovespeedx#=1.5
ballmovespeedy#=1.5

`set directions for balls x and y movements. A 0 will move ball left.A 1 will move ball right.
balldirectionx=0
balldirectiony=0

`this section will randomize the x and y direction for each time the ball hits
ballrnddirectionx=rnd(1)
ballrnddirectiony=rnd(1)

`this section will start the ball off in a random direction each time you start
balldirectionx=ballrnddirectionx
balldirectiony=ballrnddirectiony

`this section will establish player scores using an array
dim score(2)

`________________________________________________________________________________________

do

`this section will calculate the new position of the player based on keyboard input
if upkey()=(1) then dec playery#
if downkey()=(1) then inc playery# 


`this section attempts to create an AI system for the computer to try and get the ball
gosub _computer_AI

`this section defines the x and y movements of the ball
if balldirectionx=0 then dec ballpositionx#,ballmovespeedx#
if balldirectionx=1 then inc ballpositionx#,ballmovespeedx#
if balldirectiony=0 then dec ballpositiony#,ballmovespeedy#
if balldirectiony=1 then inc ballpositiony#,ballmovespeedy#

`this section will keep the player 1 sprite within boundaries of own section
if playerx#=<205 then playerx#=205
if playerx#=>205 then playerx#=205
if playery#=<205 then playery#=205
if playery#=>370 then playery#=370

`this section will keep the computer player sprite within boundaries of own section
if computerplayerx#=<590 then computerplayerx#=590
if computerplayerx#=>590 then computerplayerx#=590
if computerplayery#=<205 then computerplayery#=205
if computerplayery#=>370 then computerplayery#=370

`this section will display text messages reporting the x and y position of player.
`if you find the desire to do so, unremark these lines for different stats.
`set cursor 0,0 : print "Player X Coordinate - ", playerx#
`set cursor 0,12 : print "Player Y Coordinate - ", playery#
`set cursor 0,24 : print "Ball X Coordinate - ", ballpositionx#
`set cursor 0,36 : print "Ball Y Coordinate - ", ballpositiony#
`set cursor 0,48 : print "Ball Speed X Axis - ", ballmovespeedx#
`set cursor 0,60 : print "Ball Speed Y Axis - ", ballmovespeedy#
`set cursor 0,72 : print "Ball Direction X - ", balldirectionx
`set cursor 0,84 : print "Ball Direction Y - ", balldirectiony

`this will display the new updated player scores
center text 300,180,"Player 1 : " + str$(score(1))
center text 500,180,"Player 2 : " + str$(score(2))

`this section will update the final new position of the player and computer player
sprite 1, playerx#, playery#, player1image
sprite 7, computerplayerx#, computerplayery#, computerplayerimage
sprite 8, ballpositionx#, ballpositiony#, ballimage

`this section will determine if ball has collided on wall coordinates
if ballpositionx#=<205 and sprite collision(1,8)=0 then balldirectionx=1 : score(2)=score(2)+1
if ballpositionx#=>590 and sprite collision(7,8)=0 then balldirectionx=0 : score(1)=score(1)+1
if ballpositiony#=<205 then balldirectiony=1
if ballpositiony#=>395 then balldirectiony=0

`this section will check for collision between the ball and both players
`if collision occurs, reverse the X direction to go back the other way. Y will already be random.
if sprite collision(1,8)=1
     balldirectionx=1
endif

if sprite collision(7,8)=1
     balldirectionx=0
endif

`check to see if player has pushed the P button to pause the game. If so jump to pause function.
if inkey$()="p" then gamepause()


sync

loop

`________________________________________________________________________________________

_computer_AI:

oldcomputerplayerx#=computerplayerx# : oldcomputerplayery#=computerplayery#
computerreturnspeed#=150.0

`let's make the computer try and think. if the ball gets closer, put computer into defensive
`mode and back up to make sure it doesn't miss the shot. there are chances it will fail though
`at moving up and down quick enough.

if ballpositiony#=>288 and balldirectiony=1
     computerreturny#=ballpositiony#
endif

if ballpositiony#=<288 and balldirectiony=0
     computerreturny#=ballpositiony#
endif

if ballpositionx#=>390 and balldirectionx=1
     computerreturnx#=600
endif

if ballpositionx#=<495 and balldirectionx=0
     computerreturnx#=450
endif

if ballpositiony#<>computerplayery#
     computerplayery#=curvevalue(ballpositiony#,computerplayery#,ballmovespeedy#+1.0)+0.10
endif

computerplayerx#=curvevalue(computerreturnx#,computerplayerx#,computerreturnspeed#)
computerplayery#=curvevalue(computerreturny#,computerplayery#,computerreturnspeed#)

return

`________________________________________________________________________________________

function gamepause()

`Pausing
do

`just to keep the scores showing
ink rgb(255,255,255),1
center text 300,180,"Player 1 : " + str$(score(1))
center text 500,180,"Player 2 : " + str$(score(2))

`color text red and paste centered on screen for indication game is paused.
ink rgb(255,255,255),1 : center text 400,300,"P A U S E D"

`the resume key
if inkey$()="r"
     ink rgb(255,255,255),1
     exit
endif

`this will disable the mouse during pause so the player will not jump when game is resumed.
playery#=playery#+mousemovey()*-1 : playerx#=playerx#+mousemovex()*-1

sync

loop

`return the white ink to the numbers for when game is resumed.
ink rgb(255,255,255),1

endfunction