UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: Draco9898 on August 24, 2005, 05:06:25 AM

Title: A simple game 1: Pong
Post by: Draco9898 on August 24, 2005, 05:06:25 AM
[pbcode]
`A Very simple pong game

`Lock the screens Frame rate-
`We do this because this will stablise
`our game on different machines (to an extent)
SetFPS 60
`Now set the screen to 640 x 480 pixels
`And 16 bitmode in resolution, Also make it fullscreen
OpenScreen 640,480,16,2


`Make a type for our pong ball,
`This will hold all of the balls information
`such as screen position and its velocities
Type tPongBall
   X#,Y#,XVelo#,YVelo#
EndType
Dim PongBall As TpongBall
`Set the balls position to the very centre of the screen
`Using the screens width and height
PongBall.X#=(GetScreenWidth()/2)
PongBall.Y#=(GetScreenHeight()/2)
`Now give the ball some velocity, so it will move once we start.
`However it would probably be better if there was a delay at first,
`But we'll keep it simple.
`A negative value for velocity will make the ball go left.
PongBall.XVelo#=-4


`Now we'll make a type for the paddles in our game,
`Note that we're dimming this array for two paddles.
Type TPaddles
   X#,Y#,SizeX#,SizeY#
EndType
Dim Paddles(2) As Tpaddles
`Now let's set the position of our human player-
`He or she will be on the left side of the screen
`and about 20 pixels from the edge.
`Notice that the human player will have the
`first paddle in our array.
`We're going to give these paddles size values
`to help us with collision and centering and use a for next loop to set both.
For X=1 To 2
   Paddles(X).SizeX#=10: Paddles(X).SizeY#=32
Next X
`Now give this human paddle a position
Paddles(1).X#=20: Paddles(1).Y#=(GetScreenHeight()/2)-(Paddles(1).SizeY#/2)
`Now give this computer paddle a position
Paddles(2).X#=GetScreenWidth()-40: Paddles(2).Y#=(GetScreenHeight()/2)-(Paddles(1).SizeY#/2)

   
`Now how about an array for player the players score and lives?
Type TPlayerData
   Score, Lives
EndType   
Dim PlayerData As TPlayerData
`set lives to 3
PlayerData.Lives=3

`We need to make a camera so we can see stuff on our screen
`if we choose to use captureToscene.
CreateCamera 1
`******************************* OUR MAIN LOOP **************************************
Do
   `This will begin capturing to the scene buffer,
   `Thus allowing us to draw everything using a handy dandy camera :P
   CaptureToScene: ClsScene
   

   `Put the players score in the top corner of the screen
   `When combining varibles with strings like "Your score",
   `We must use STR$ to turn the varible into a string.
   Text 10,10,"Your Score is "+Str$(PlayerData.Score)
   Text 10,30,"You have "+Str$(PlayerData.Lives)+" Lives"
   
   
   `Handle the paddles***
   `Use temporary values to help us position these paddles and make
   `this code easier to read. We're also using a for next loop
   `to conveniently go through all the paddles and draw them.
   For X=1 To 2
 X#=Paddles(X).X#: Y#=Paddles(X).Y#
 Box X#,Y#,X#+Paddles(X).SizeX#,Y#+Paddles(X).SizeY#,1
   Next X
   `We're going to make the computer unbeatable for
   `this game, just to keep it simple.
   `This computers position will stay with the ball.
   Paddles(2).Y#=PongBall.Y#-15
   
   
   `Handle the pong ball***
   `Move the ball according to its velocites-
   `We will add the velocites to the positions,
   `making sure we add to the right ones...
   PongBall.X#= PongBall.X#+PongBall.Xvelo#
   PongBall.Y#= PongBall.Y#+PongBall.Yvelo#
   `Draw the ball- using its stored positions
   Circle PongBall.X#,PongBall.Y#,10,1
   
   
   `Handle Collision between ball, paddles, and screen edges***
   `We'll use a for Next loop to go through the paddle positions
   `and check for collsion between that and the ball.
   `IF there was a collision, simply reverse the balls velocity.
   `This may look confusing, but all we're doing here is
   `Checking all four sides of the paddles of collision.
   For X=1 To 2
 If (PongBall.Y#+5)>Paddles(X).Y#
    If (PongBall.Y#-5)<Paddles(X).Y#+Paddles(X).SizeY#
   If PongBall.X#<Paddles(X).X#+10
      If PongBall.X#>Paddles(X).X#
     PongBall.XVelo#=PongBall.XVelo#*-1
     `Make the ball bounce off randomly
     PongBall.YVelo#=Rnd(2)+1
     If Rnd(1)=1
        PongBall.YVelo#=-PongBall.YVelo#
     EndIf
     `If the player defended the ball- add score
     If X=1
        PlayerData.Score=PlayerData.Score+10
     EndIf
      EndIf
   EndIf
    EndIf
 EndIf
   Next X  
   `Now we'll check to see if the ball went off screen.
   `IF it does go off screen, reset the ball and set the scores.
   `The pong ball went through the players defence...
   `IF the ball went off the top or bottom of the screen-
   If PongBall.Y#<0
 PongBall.YVelo#=PongBall.YVelo#*-1
   EndIf
   If PongBall.Y#>GetScreenHeight()
 PongBall.YVelo#=PongBall.YVelo#*-1
   EndIf
   `IF the ball went off the players side-
   If PongBall.X#<-10
 `Reset ball positions
 PongBall.X#=(GetScreenWidth()/2): PongBall.Y#=(GetScreenHeight()/2)
 PongBall.YVelo#=0
 `reset player position
 Paddles(1).Y#=(GetScreenHeight()/2)-(Paddles(1).SizeY#/2)
 `Decrease lives and exit if it goes below 1
 PlayerData.Lives=PlayerData.Lives-1
 If PlayerData.Lives<1
    End
 EndIf
   EndIf
   
   
   `Handle Controls
   `This is a fairly simple control sceheme, it should really
   `be based on time, but that's up to you.
   If UpKey()=1
 Paddles(1).Y#=Paddles(1).Y#-4
   EndIf
   If DownKey()=1
 Paddles(1).Y#=Paddles(1).Y#+4
   EndIf
   
   
   `Draw the camera, allowing stuff to be seen on the screen
   DrawCamera 1
Sync
Loop
[/pbcode]