Fractalytic.co.uk - Pong in 25 lines Source Code


Pong Source Code

This is the Blitz Basic source code for single player Pong using just 25 lines without any 'cheating' (each line contains only one command but note that due to screen resolution I have split the lines onto multiple ones here for presentation purposes). The game doesn't include sound but does have a scoring routine, a ball that gets faster as the point progresses, simple AI and real physics (albeit with a random element thrown in so that it isn't too predictable).



Graphics 640,480,0,2

SeedRnd MilliSecs()

SetBuffer BackBuffer()

While Not KeyHit(1)

   Cls

   Rect 10,P1y+220,10,40,True

   Rect 620,P2y+220,10,40,True

   Rect Bx#-5,By#-5,10,10,True

   Text 320,0,"Pong25 1 player " + P1score + " : " + P2score + " by Shane Kennedy", True

   Bx# = Bx# + 3*Cos(Bangle) + (MilliSecs() - start)*Cos(Bangle)/6000

   By# = By# + 3*Sin(Bangle) + (MilliSecs() - start)*Sin(Bangle)/6000

   If (Bx# > 615 And Bx# < 623 And Abs(P2y + 240 - By#) < 20) Or (Bx# <25 And Bx# > 17 And Abs(P1y +  

                             240 -  By#) < 20) Then Bx# = 615 * (Bx# > 615) + 25 * (Bx# < 25)

   If (Bx# = 615 And Abs(P2y + 240 - By#) < 20) Or (Bx# = 25 And Abs(P1y + 240 - By#) < 20) Then Bangle 

                             = (180 - Abs(Bangle) - (Rand(-10,0)*(Abs(Bangle) > 10) - Rand(0,10)*(Abs(Bangle) < 

                             170)))*Sgn(Bangle)

   If By# < 25 Or By# > 455 Then By# = 25 * (By# < 25) + 455 * (By# >v455)

   If By# = 25 Or By# = 455 Then Bangle = -Bangle

   If Bx# < -5 Then P2score = P2score + 1

   If Bx# > 645 Then P1score = P1score + 1

   If Bx# < -5 Or Bx# > 645 Or start = 0 Then start = MilliSecs()

   If Bx# < -5 Or Bx# > 645 Or start = 0 Then Bangle = (Rand(1,60) + 120 * Rand(0,1)) * (-1 + 2*Rand(0,1))

   If Bx# < -5 Or Bx# > 645 Or start = 0 Then By# = 240

   If Bx# < -5 Or Bx# > 645 Or start = 0 Then Bx# = 320

   P1y = P1y - 3 * KeyDown(30) * (P1y > -200) + 3 * KeyDown(44) * (P1y < 200)

   P2y = P2y + 3 * (P2y + 240 - By# < -5) - 3 * (P2y + 240 - By# > 5)

Wend

End



To anybody familiar with Blitz Basic there shouldn't be anything above that causes you too much trouble. No external media is required so the code can be compiled and run as is.

The variables used should be fairly self explanatory but if you are unsure the following may help:

  • P1y and P2y are the y co-ordinates corresponding to the top of the player bats.
  • P1score and P2score are the player scores
  • Bx and By are the x and y coordinates of the ball
  • Bangle is the angle that the ball is travelling in measured from the horizontal (in the range -180 to 180)
  • start is the time in milliseconds that the current point started 



< Back to Top >




000webhost logo