Ok, so i haven't written a pong game before and i figured why not, it seems rather easy. but it definetly isn't easy.
my pseudo looks allright, and the code should work in my opinion, but db have other plans.
i've been sitting with this for an hour now, tried different methods like
if ball.y < 5 then ball.direction = wrapvalue( ball.direction *-1 ) or
if ball.y < 5 then ball.direction = wrapvalue( ball.direction -180 ) or
if ball.y < 5 then ball.direction = wrapvalue( ball.direction +180 ) etc.
so if anybody knows what is wrong with the code, please let me know!
REM Project: pong
REM Created: 10.08.2006 11:33:16
REM
REM ***** Main Source File *****
REM
Sync On : Sync Rate 60 : hide mouse
makeMedia()
set text font "system" : set text size 72
type tdata
x as integer
y as integer
height as integer
width as integer
score as integer
ismoving as integer
speed as integer
direction as integer
endtype
global player as tdata
global enemy as tdata
global ball as tdata
ball.x = 320-(ball.width/2)
ball.y = 240-(ball.height/2)
enemy.y = 240-(enemy.height/2)
player.y = 240-(player.height/2)
ball.speed = 2
ball.ismoving = 0
_paused=1
Do
if _paused=0
` player physics
player.x = screen width()-40
inc player.y, (downkey()-upkey())*2
if player.y <= 5 then player.y = 5
if player.y >= (screen height()-1)-55 then player.y = (screen height()-1)-55
` enemy physics
if ball.ismoving = 1
endif
enemy.x = 40
if enemy.y <= 5 then enemy.y = 5
if enemy.y >= (screen height()-1)-55 then enemy.y = (screen height()-1)-55
` ball physics
if ball.ismoving = 0
if spacekey()=1
ball.ismoving = 1
ball.direction = rnd(3)
if ball.direction = 0 then ball.direction = wrapvalue( 45 )
if ball.direction = 1 then ball.direction = wrapvalue( 315 )
if ball.direction = 2 then ball.direction = wrapvalue( 225 )
if ball.direction = 3 then ball.direction = wrapvalue( 135 )
endif
else
if ball.y < 5 then ball.direction = wrapvalue( ball.direction *-1 )
if ball.y > (screen height()-1)-ball.height then ball.direction = wrapvalue( ball.direction *-1 )
ball.x = ball.x + cos( ball.direction ) * ball.speed
ball.y = ball.y + sin( ball.direction ) * ball.speed
endif
` update
Paste Image 4, 0, 0
Paste Image 1, player.x, player.y
Paste Image 2, enemy.x, enemy.y
Paste Image 3, ball.x, ball.y
center text 160, 0, str$( enemy.score )
center text 480, 0, str$( player.score )
text 100, 100, str$(ball.direction)
else
paste image 100, 0, 0
endif
` help
if controlkey() and _latch=0
_latch=1
if _paused=1 then _paused=0 else _paused=1
endif
if controlkey() = 0 then _latch=0
Sync : Loop
Function makeMedia()
rem Paddle
cls 0 : ink rgb( 255, 255, 255 ), 0
box 0, 0, 10, 50
get image 1, 0, 0, 10, 50
get image 2, 0, 0, 10, 50
player.height = 50
enemy.height = 50
rem Background
cls 0
ink rgb( 255, 255, 255 ), 0 : box 0, 0, screen width()-1, screen height()-1
ink rgb( 000, 000, 000 ), 0 : box 5, 5, screen width()-6, screen height()-6
ink rgb( 255, 255, 255 ), 0
for x = 1 to 40
line 320, (x*15)-10, 320, (x*15)
next x
get image 4, 0, 0, 640, 480
rem Ball
cls 0 : ink rgb( 255, 255, 255 ), 0
box 0, 0, 10, 10
get image 3, 0, 0, 10, 10
ball.height = 10
ball.width = 10
rem Help Screen
cls 0
ink rgb( 255, 255, 255 ), 0 : box 100, 100, 540, 380
ink rgb( 000, 000, 000 ), 0 : box 105, 105, 535, 375
ink rgb( 255, 255, 255 ), 0
center text 320, 150, "Pong"
center text 320, 200, "control key - show/hide this message"
center text 320, 215, "up key - move paddle up"
center text 320, 230, "down key - move paddle down"
center text 320, 245, "space key - start game when ball freeze"
center text 320, 300, "programmed by Lukas W (www.lukasw.com)"
center text 320, 315, ""
get image 100, 0, 0, 640, 480
EndFunction
(no media is required)
edit,
it's the collision with the top and bottom walls that i am trying to figure out (line 64 - 67)
edit2,
ok i think i figured it out now. the problem was i had to call makeMedia() AFTER i setup the types and globals. hmm if i only knew that sooner. wasted thread. sorry to interupt your daily browsing.