well, i allready wrote it, and its not that easy
i call it pongy, a clone of the famous pong but a bit different.
and here is the sourcecode for pongy. plus download atachment (pongy.exe)
the game is working ok (contains errors), its not finished yet , i thing i got stock with this game, maybe because i did`nt have
any well-strutured working plan, i did have another version of pongy, without any problems.
i did put a timer function to the bat1, thats need to be activa when bat 1 hits tank brick. (then bat1 will have machine guns,he he), but you can`t shoot yet. (i have`nt fix the code properly yet!).
here is the error list
ball sometimes dissaeper when its crrossing the boundaries of the edge of the screen!.
if player 1 or 2 loose and gameover screen is showing, the game resets and exit to windows!!??.
that`t why i did put all the music code in rem lines for now.
note, i did`nt pass any parametre to any functions in the game yet, i will do that later.
my first pong game did work well, no errors, i think i messed the whole thing up, with to much
codelines, i can`t find those errors yet.
allso i`m going to do a little research on physics relatedet to how the ball would eventually move
corectly. (i think i now how to do it, the ball needs to bounce of the bat in x axis and y, more to the left or right, if the ball
is bounceing in the middle more than, lets say 2 times,it need to change angle both x and y in randomized order. (my version is not
realstic enough!.)
i thing i`m gonna jump to the next project, making Galaxaian clone. (using arrays and sprite for the enemy spaceships).
if i`m going to write galaxian, i need a function that could check how many times the enemies hits player1 (i know, there`s only 1 player).
if the enemies hits the spaceships to many times i will get destroyed. (i`m not gonna check collision with every single pixel of a sprite of lets say 160*200 pixels!).
code is really hard.
rem pong
rem henrik 2010
startgame:
t as DWORD
t=Timer()
Elapsed=(Timer()-t)/1000
rem clear screen
cls
rem set display mode 1024,768,32 bit color
set display mode 1024,768,32
rem set forground and background with color rgb.
ink rgb(255,255,0),rgb(0,0,0)
rem set window style !
set window on
rem set windows position to 480,100
set window position 450,100
rem set windows style 0,1,0 non dragable, no minimize or maximize
set window layout 0,1,0
rem show title in windows with text " xxxxx"
rem set text font to verdana
set text font "verdana"
rem set text size to 33
set text size 26
rem set text transparent
rem initiliase variables as global as integer.
rem ini for ball x and y position
rem ini for ball direction left or right or up, down.
type defball
global x as integer
global y as integer
global left as integer
global right as integer
global up as integer
global down as integer
endtype
global ball as defball
rem ini batballhit test variabel as global
global bat1ballhit=0 as integer
global bat2ballhit=0 as integer
global player1points=0 as integer
global player2points=0 as integer
bulletx=60
bullety=0
rem ball start position.
ball.x=20
ball.y=270
rem ini players live to 5
global playerlive1=5
global playerlive2=5
rem start position x,y for bat1 position
global bat1x=0
global bat1y=200
rem start position x,y for bat2 position
global bat2x=1000
global bat2y=230
bulletshoot=0
bat1ballhit=0
bat2ballhit=0
tankbrick=0
brick1x=430
brick1y=370
rem ini for ball lelft, right and up, down direction =0
ball.left=0
ball.right=0
ball.up=0
ball.down=0
global chooseplayer=1
rem ini ball position plus newposi (added each time when bat1 player is selected or player 2.
global newposi=0
rem sync rate 20
rem set sprite number, backsave, transperency
set sprite 3,1,1
set sprite 6,0,1
set sprite 7,0,1
set sprite 8,0,1
rem set sprite 9,0,1
set sprite 10,0,1
set sprite 11,0,1
rem load coresponding image for each sprite
load image "c:\dark basic sprites\ball.bmp",3
load image "c:\dark basic sprites\pongbgr.bmp",4
load image "c:\dark basic sprites\pongmenu.bmp",6
load image "c:\dark basic sprites\gameover.bmp",7
load image "c:\dark basic sprites\pongbrick.bmp",8
rem load image "c:\dark basic sprites\tankanim.bmp",9
load image "c:\dark basic sprites\cannons.bmp",10
load image "c:\dark basic sprites\bullet.bmp",11
rem create animted sprite file path.. bmp, 5 picture or frames,down 1,image number.
create animated sprite 1,"c:\dark basic sprites\bat1anim.bmp",5,1,1
create animated sprite 2,"c:\dark basic sprites\bat2anim.bmp",5,1,2
create animated sprite 9,"c:\dark basic sprites\tankanim.bmp",5,1,9
rem set sprite image for sprite 1 image 1 ect..
set sprite image 1,1
set sprite image 2,2
rem load game music / menu music
load music "c:\dark basic sprites\menumusic.mp3",1
load music "c:\dark basic sprites\gamemusic.mp3",2
hide sprite 11
hide sprite 10
REM MENU
rem do loop
DO
rem paste pong game background image
paste image 6,0,0,1
rem show text x,y position, "xxxxxxxx"
text 310,350,"Press Enter-key to play PONGY"
rem play music 1
rem if returnkey i spressed go to subrutine vaelgspiller
if returnkey()=1 then chooseplayer()
rem end of loop
LOOP
rem vælg spiller
function chooseplayer()
play music 1
rem do loop
do
rem ini for randomize choose player.
rem ini for ball x,y position
ball.x=20
ball.y=270
rem ini for bat1 x,y position
bat1x=0
bat1y=200
rem ini for bat2 x,y
bat2x=1000
bat2y=230
rem ini bat hit1 and bathit2 =0
bat1ballhit=0
bat2ballhit=0
rem brick position
brick1x=430
brick1y=370
rem ball l,r,up,down direction=0
ball.left=0
ball.right=0
ball.up=0
ball.down=0
rem newposi =0 first player selected with ball.x position x=0, ball is located to left side of screen.
newposi=0
rem randomize chose player
chooseplayer=rnd(2)
rem if choseplayer =1 then go to player1 otherwise go to player2
if chooseplayer=1
stop music 1
player1()
else
stop music 1
player2()
endif
rem end of if
loop
endfunction
function player1()
rem show sprites
show sprite 1
show sprite 2
show sprite 3
ball.x=20
ball.y=270
rem ini for bat1 x,y position
bat1x=0
bat1y=200
rem ini for bat2 x,y
bat2x=1000
bat2y=230
rem ini bat hit1 and bathit2 =0
bat1ballhit=0
bat2ballhit=0
rem do loop
do
rem player 1 with ball located to the left of screen.
newposi=5
cls o
rem paste background picture
paste image 4,0,0,1
rem set text position x,y show text
set cursor 145,65:print "player1: liv: :",playerlive2
set cursor 650,65:print "player2 liv :",playerlive1
set cursor 145,90:print "player1: points: :",player1points
set cursor 650,90:print "player2: points: :",player2points
rem if W or S key is pressed then move bat1 up or down (y)
if keystate(17)=1 then dec bat1y,3:dec ball.y,3
if keystate(31)=1 then inc bat1y,3:inc ball.y,3
rem testing if bat1 is crossing any boundaries of the screen, so bat1 wont disaeper from screen.
rem bat2 movement canceled.
if bat1y<1 then bat1y=1
if bat1y>600 then bat1y=600
if bat2y<1 then bat2y=1
if bat2y>600 then bat2y=600
if ball.y<1 then ball.y=1
if ball.y>600 then ball.y=600
if ball.y<1 then ball.y=1
if ball.y>600 then ball.y=600
rem check if spacekey is pressed, if so then release ball, (ball.right=1, bally.down=1) go to gameloop (main loop).
if spacekey()=1
ball.right=1
ball.down=1
ball.left=0
ball.up=0
gosub gameloop
endif
rem show sprite bat1 bat2, ball.
sprite 1,bat1x,bat1y,1
sprite 2,bat2x,bat2y,2
sprite 3,ball.x+newposi,ball.y,3
sprite 10,bat1x,bat1y,10
rem play animations fo bat1 sprite (flashing color!)
play sprite 1,1,5,77
loop
endfunction
rem spiller 2 start
function player2()
rem show sprites
show sprite 1
show sprite 2
show sprite 3
ball.x=20
ball.y=270
rem ini for bat1 x,y position
bat1x=0
bat1y=200
rem ini for bat2 x,y
bat2x=1000
bat2y=230
rem ini bat hit1 and bathit2 =0
bat1ballhit=0
bat2ballhit=0
do
cls o
newposi=920
rem paste background image
paste image 4,0,0,1
rem show text x,y position
set cursor 145,65:print "player1: lives: :",playerlive2
set cursor 650,65:print "player2: lives: :",playerlive1
set cursor 145,90:print "player1: points: :",player1points
set cursor 650,90:print "player2: points: :",player2points
rem testing up down key for each bat, move bat1/bat2, and move ball same direction as bat. (y)
if upkey()=1 then dec bat2y,3:dec ball.y,3
if downkey()=1 then inc bat2y,3:inc ball.y,3
rem checking for screen boundires for bat2.
if bat2y<1 then bat2y=1
if bat2y>600 then bat2y=600
if ball.y<1 then ball.y=1
if ball.y>600 then ball.y=600
if ball.y<1 then ball.y=1
if ball.y>720 then ball.y=600
rem if space hit then release ball with direciton left, up , go to gameloop (main loop)
if spacekey()=1
ball.right=0
ball.down=0
ball.left=1
ball.up=1
gosub gameloop
endif
rem show sprite bat,bat2 ball
sprite 1,bat1x,bat1y,1
sprite 2,bat2x,bat2y,2
sprite 3,ball.x+newposi,ball.y,3
sprite 10,bat1x,bat1y,10
play sprite 2,1,5,77
rem play animation for bat2 (flashing bat color).
loop
endfunction
rem game loop
gameloop:
do
cls o
ball.x=ball.x+ball.right
ball.x=ball.x-ball.left
ball.y=ball.y+ball.down
ball.y=ball.y-ball.up
bulletx=bulletx+bulletshoot
Elapsed=(Timer()-T)/1000
rem show background image
paste image 4,0,0,1
play music 2
rem checking if up down, W , S key is pressed, if so, then move bat1 / bat2 with value 5 (moving fast).
if upkey()=1 then dec bat2y,3
if downkey()=1 then inc bat2y,3
if keystate(17)=1 then dec bat1y,3
if keystate(31)=1 then inc bat1y,3
rem checking if bat1 or 2 is not dissapering from the screen.
if bat2y<1 then bat2y=1
if bat2y>600 then bat2y=600
if bat1y<1 then bat1y=1
if bat1y>600 then bat1y=600
rem her comes the tricky part, hmmm
if ball.y<1 and ball.left=1
ball.down=1*rnd(3)
ball.up=0:ball.left=1
ball.right=0
endif
rem same but ball right direction
if ball.y<1 and ball.right=1
ball.down=1*rnd(3)
ball.up=0:ball.left=0
ball.right=1
endif
rem same but ball y position 770 pixel positioned to the left
if ball.y>770 and ball.left=1
ball.down=0
ball.up=1*rnd(3)
ball.left=1
ball.right=0
endif
rem same but ball y position 770 pixel positioned to the right
if ball.y>770 and ball.right=1
ball.down=0
ball.up=1*rnd(3)
ball.left=0
ball.right=1
endif
rem checking if bat1/2 hit ball then activate ball direction to right=1, ball down =1*rnd(4)
if sprite hit(1,3)=1
ball.left=0
ball.right=1
ball.down=1*rnd(2)
ball.up=0
bat1ballhit=1
bat2ballhit=0
endif
if sprite hit(2,3)=1
ball.left=1
ball.right=0
ball.down=0
ball.up=1*rnd(2)
bat2ballhit=1
bat1ballhit=0
endif
rem player 1 lose 1 live, disable ball direciton to zero (the ball is out of the screen in direction, there`s no
rem need to let the ball continue is path.
rem player x as lost, we are going to choose player.
if bat2ballhit=1 and ball.x+newposi<1
playerlive1=playerlive1-1
ball.left=0
ball.right=0
ball.down=0
ball.up=0
bat1ballhit=0
bat2ballhit=0
player1points=player1points+5
player1()
endif
if bat2ballhit=1 and ball.x+newposi>1100
playerlive1=playerlive1-1
ball.left=0
ball.right=0
ball.down=0
ball.up=0
bat1ballhit=0
bat2ballhit=0
player1points=player1points+5
player1()
endif
rem same her but this only aply to player 2
if bat1ballhit=1 and ball.x+newposi>1100
playerlive2=playerlive2-1
ball.left=0
ball.right=0
ball.down=0
ball.up=0
bat1ballhit=0
bat2ballhit=0
player2points=player2points+5
player2()
endif
rem show sprites
if bat1ballhit=1 and ball.x+newposi<1
playerlive2=playerlive2-1
ball.left=0
ball.right=0
ball.down=0
ball.up=0
bat1ballhit=0
bat2ballhit=0
player2points=player2points+5
player2()
endif
if bat1ballhit=1 and ball.right=1 and sprite hit(3,8)=1
ball.right=0
ball.up=rnd(2)+1
ball.down=rnd(2)+1
ball.left=1
endif
if bat2ballhit=1 and ball.left=1 and sprite hit(3,8)=1
ball.right=1
ball.up=0
ball.down=rnd(2)+1
ball.left=0
endif
rem check tank brick collision code
if bat1ballhit=1 and sprite hit(3,9)=1 and ball.right=1
play sprite 9,1,5,15
show sprite 10
tankbrick=1
endif
if returnkey()=1 and tankbrick=1 then bulletshoot=4
if bulletx>1120 then bulletx=0:bulletshoot=0
sprite 1,bat1x,bat1y,1
sprite 2,bat2x,bat2y,2
sprite 3,ball.x+newposi,ball.y,3
sprite 8,320,300,8
sprite 9,470,530,9
sprite 10,bat1x,bat1y,10
sprite 11,bulletx,bullety,11
rem hide sprite 11
play sprite 1,1,1,1
play sprite 2,1,1,1
rem show text with print, players live
set cursor 145,65:print "player1: lives: :",playerlive2
set cursor 650,65:print "player2 lives :",playerlive1
set cursor 145,90:print "player1: points: :",player1points
set cursor 650,90:print "player2: points: :",player2points
rem checking if player 1 or 2 has used all live, then go to game over.
if playerlive1=1 then gameover()
if playerlive2=1 then gameover()
loop
rem gameover
function gameover()
do
cls 0
hide sprite 1
hide sprite 2
hide sprite 3
rem paste image gameover screen
paste image 7,0,0,1
stop music 2
rem show text.
TEXT 250,500, "PRESS ENTER TO START NEW GAME"
rem if player hit returnkey go to startgame
if returnkey()=1 then gosub startgame
rem end of loop
loop
endfunction
