basicprogramming.org


Welcome, Guest. Please login or register.
Did you miss your activation email?
Forum time; Jul 31. 2010, 03:03
Home Help Search Calendar Login Register
News: Have you got suggestions for BasicProgramming.org? Let's hear them!
Interested in creating your own programming language? Check out the QDepartment group!

+  BASIC programming forum
|-+  Basic Coding
| |-+  Basic Interpreters.
| | |-+  sdlBasic
| | | |-+  Pong
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: 1 [2] Go Down Reply Print
Author Topic: Pong  (Read 361 times)
Arnebcab94
Newbie
*
Offline Offline

Posts: 10


« Reply #15 on: Feb 06. 2010, 08:11 » Reply with quote

//pong the game o.o
common barposition[]
common score[]
barposition[1] =0
barposition[2] =0
score[1]=0
score[2]=0
ballpositionx=200
ballpositiony=150
ballsx=0.85
ballsy=0.85
counter=0
cpu = 1
loadsound("ping.wav",1)
----------------------------------------------------------------------------------------------------------------
function randomizeball()
   //puts ball to the middle line
      ballpositionx=200
   // Y is random
      ballpositiony=rnd(394)
   //keeps the ball from being to high
      if ballpositiony < 6 then : ballposition = 6 : end if
   //beginning speed
      ballsx=0.85
      ballsy=0.85
   // left or right/up or down
      if rnd(2) = 1 then : ballsy = -ballsy : end if
      if rnd(2) = 1 then : ballsx = -ballsx : end if
end function
--------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
//Draws the line wich divides the screen into two parts
function drawline()
   line(200,0,200,30)
   line(200,40,200,70)
   line(200,80,200,110)
   line(200,120,200,150)
   line(200,160,200,190)
   line(200,200,200,230)
   line(200,240,200,270)
   line(200,280,200,300)
end function
-----------------------------------------------------------------------------------------------------------------
function drawball(x,y)
   ink(rgb(0,0,0)) : bar(ballpositionx+5,ballpositiony+5,ballpositionx-5,ballpositiony-5) //Covers the old ball with black
   ink(rgb(255,255,255)) :  fillcircle(x,y,5) //Draws the new one
end function

//This are the two bars the players can controll
function drawbars(y1,y2)
   //Covers the old bars with black
   ink(rgb(0,0,0)) : bar(0,0,5,300) : bar(395,0,400,300) : ink(rgb(255,255,255))
   //These will draw the new bars with the new Coordinates
   //player 1
   bar(0,y1,5,y1+50)
   //player 2
   bar(395,y2,400,y2+50)
end function

function drawscore
   //player1
   text(150,4,50,score[1])
   text(227,4,50,score[2])
end function
------------------------------------------------------------------------------------------------------------------
function update()
   if counter = 300 then : drawscore : counter=0 : end if //This makes the Score only to be drawn every 1.5 secs
   ink(rgb(255,255,255)) //white
   //These will draw the balls and bars
   drawline
   drawball(ballpositionx,ballpositiony)
   drawbars(barposition[1],barposition[2])
   counter=counter+1 //Counter for score actualisation
end function
------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
function initscreen()
   setfont("font.ttf") //it will be used when drawing the score
   setdisplay(400,300,32,1) //400x300, 32bit, fixed window
   ink(rgb(255,255,255)) //white
   drawline : drawscore //draws the field for the first time
end function
--------------------------------------------------------------------------------------------------------------------
function moveball()
   ballpositionx=ballpositionx+ballsx //makes the ball move
   ballpositiony=ballpositiony+ballsy  // - - - -
   // makes the ball change its way when poping again upper and lower wall and plays a sound then
   if ballpositiony > 294 then : ballsy = -ballsy: playsound(1,1)  : end if
   if ballpositiony < 6 then : ballsy = -ballsy:playsound(1,1)  : end if

   //reflects ball froms bar 2
   if barposition[2] < ballpositiony and (barposition[2]+50) > ballpositiony  then
      if ballpositionx > 391 then : ballsx = -ballsx : playsound(1,1) :
      If BallSX > -1.0 then : BallSx = BallSx - 0.01 : end if
      If BallSY > 0 and BallSY < 1.0 then : BallSy = BallSy + 0.01 : end if //make the ball faster
      If BallSY < 0 and BallSY > -1.0 then : BallSy = BallSy - 0.01 : end if //- - - -
   end if   : end if

//reflects the ball froms bar 1
   if barposition[1] < ballpositiony and (barposition[1]+50) > ballpositiony  then
      if ballpositionx < 9 then : ballsx = -ballsx : playsound(1,1) :
      If BallSX < 1.0 then : BallSx = BallSx+ 0.01 : end if
      If BallSY > 0 and BallSY < 1.0 then : BallSy = BallSy + 0.01 : end if
      If BallSY < 0 and BallSY > -1.0 then : BallSy = BallSy - 0.01 : end if
end if : end if
end function
-------------------------------------------------------------------------------------------------------------------
function win
   //left side
   if ballpositionx = -7 then
      ink(rgb(0,0,0))
      bar(201,0,251,54) //deletes old score from screen
      ink(rgb(255,255,255))
      score[2] = score[2]+1
      drawscore
      wait(1000)
      randomizeball //randomizes loacation, speed and way of new ball
      playsound(1,1)
      update
      wait(1000)
      //win
      if score[2]=10 then
         text(235,100,60,"win!")
         wait(3000)
         end
      end if
   end if

//right side
if ballpositionx = 407 then
   ink(rgb(0,0,0))
   bar(100,0,199,54)
   ink(rgb(255,255,255))
   score[1] = score[1]+1
   drawscore
   wait(1000)
   randomizeball
   playsound(1,1)
   update
   wait(1000)

   //win
      if score[1]=10 then
         text(35,100,60,"win!")
         wait(3000)
         end
      end if
   end if
end function
--------------------------------------------------------------------------------------------------------------------
function controlling()
   //makes player 1 be able to move his bar
   if key(k_up) and barposition[1] > 0  then : barposition[1] = barposition[1] - 0.75 : end if
   if key(k_down) and barposition[1] < 250 then : barposition[1] = barposition[1] + 0.75 : end if
//checks if player is alone or playing with a friend
   if cpu = 0 then :
      if key(k_kp_minus) and barposition[2] > 0  then : barposition[2] = barposition[2] - 0.75 : end if
      if key(k_kp_plus) and barposition[2] < 250 then : barposition[2] = barposition[2] + 0.75 : end if
   else
//makes the second bar follow the ball when cpu is enabled
      if ballpositiony < (barposition[2]+25) and ballsx > 0 and barposition[2] > 0  then : barposition[2] = barposition[2] - 0.75 : end if
      if ballpositiony > (barposition[2]+25) and ballsx > 0 and barposition[2] < 250 then : barposition[2] = barposition[2] + 0.75 : end if
   end if
end function
-----------------------------------------------------------------------------------------
//introduction
sub intro
      //intro text on german and english
   prints("Wilkommen bei pong | Welcome at pong")
   prints("Wollen sie alleine oder zu zweit spielen?")
   prints("Do you want to play alone or with a friend?")
   prints("leertaste: alleine | Space: alone")
   prints("eingabetaste: zu zweit | Enter/Return with a friend" )
   while 1
      //decide if CPU is enabled
      if key(k_space) then : cpu = 1 : exit while : end if
      if key(k_return) then : cpu = 0 : exit while : end if
      wait(15)
   wend

   prints("") // new line
   if cpu=1 then : prints("sie werden alleine spielen | You are going to play alone ") : end if
   if cpu=0 then : prints("sie werden zu zweit spielen | You are playing with a friend ") : end if
   prints("steuerung: | Controls:")
   prints("spieler 1: | Player 1:")
   prints("     pfeiltaste oben: auf | arrow up --> up")
   prints("     pfeiltaste unten: ab | arrow down --> down")
   if cpu=0 then :
      prints("spieler 2: | player 2")
      prints("     minus: auf | minus --> up")
      prints("     plus: ab | plus --> down") : end if
   prints("")
   prints("drücken sie die eingabetaste um fortzufahren...")
   prints("Press return to continue...")
   //wait for enter to be released
      while key(k_return) : wait(15) :wend
   //wait for enter to be pressed
      waitkey(k_return)
end sub
//

//main
intro //intro text
initscreen() //draw field for first time
randomizeball //random ball position
update //draw ball
wait(2000)
while 1
   update() //draw new positions
   moveball
   win
   controlling() //controlling of bars
   wait(4) //so the game isnt to fast
wend

wait(5000)
//end main
« Last Edit: Feb 06. 2010, 08:13 by Arnebcab94 » Report to moderator   Logged
Arnebcab94
Newbie
*
Offline Offline

Posts: 10


« Reply #16 on: Feb 06. 2010, 08:19 » Reply with quote

btw you didnt write in sdlbasic, did you? what langauge is it?
Report to moderator   Logged
Pages: 1 [2] Go Up Reply Print 
« previous next »
Jump to:  
Atom RDF RSS 0.91 RSS 2.0


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!