Hello everybody, I've been trying for the past day or so to create a BrickBreaker clone. The part that's just got me stuck is the ball colliding with the top of the "bricks" or blocks as they're called in the code.
I've tried setting up collision zones, and then just trying to reverse the speed of the ball so it doesn't go downwards anymore, but neither worked. I've commented out the collision code I wrote and the program is currently set up to send the ball directly down the y-axis to the block below it.
Any help would be appreciated.
Here's my full source code:
Rem Project: BrickBreaker
Rem Created: Thursday, April 01, 2010
Rem ***** Main Source File *****
randomize timer()
//Basic setup
sync on : sync rate 60
backdrop on
color backdrop rgb(0,169,0)
//constants
#constant true 1
#constant false 0
//datatype for player
type player_info
x as float
y as float
xspeed as float
lives as byte
//Extras here
endtype
//datatype for blocks
type block_info
x as float
y as float
hp as byte
hp_low as DWORD
hp_med as DWORD
hp_high as DWORD
collided_ball as boolean //0 for bottom. 1 for top. 2 for left. 3 for right
collided_lsr as boolean
collided_gun as boolean
//Extras here
endtype
//datatype for ball
type ball_info
x as float
y as float
xspeed as float
yspeed as float
dam as integer
endtype
//initial values for player
global player as player_info
player.x=320
player.y=460
player.lives=3
//Initial values for ball datatype
global ball as ball_info
ball.x=320
ball.y=100
//temp=rnd(1)
//if temp>=.5 then ball.xspeed=temp+1
//if temp<.5 then ball.xspeed=(temp*-1)-1
ball.xspeed=0
ball.yspeed=.1
ball.dam=85
//Setting up of blocks/"bricks"
global dim blocks(9) as block_info
global tempx=0 : tempx=45
global tempy=30
for x=0 to 9
blocks(x).x=tempx
tempx=tempx+61
blocks(x).y=tempy
blocks(x).hp=255
blocks(x).hp_low=rgb(85,0,0)
blocks(x).hp_med=rgb(170,0,0)
blocks(x).hp_high=rgb(255,0,0)
blocks(x).collided_ball=-1
blocks(x).collided_lsr=-1
blocks(x).collided_gun=-1
next x
//Special test of collision zones
blocks(0).x=320
blocks(0).y=200
//Debug text setup
ink rgb(255,255,255),rgb(0,0,0)
set text opaque
//MAIN LOOP
do
DrawScreen()
DrawPlayer(player.x,player.y)
PlayerControl()
BallControl()
ManageBoxes()
//Debug text
text 10,10,str$(blocks(0).hp)
text 10,30,str$(blocks(1).hp)
text 10,50,str$(blocks(2).hp)
text 10,70,str$(blocks(0).collided_ball)
sync
loop
end
//Draws Boundaries
function DrawScreen()
//Top Boundary
line 8,8,632,8
line 8,9,632,9
line 8,10,632,10
//Right Boundary
line 630,10,630,470
line 631,10,631,470
line 632,10,632,470
//Left Boundary
line 8,10,8,470
line 9,10,9,470
line 10,10,10,470
endfunction
//Draws Player
function DrawPlayer(x as float , y as float )
box x-25,y-10,x+25,y+10
endfunction
//Controls Player, MOUSE CONTROL SCHEME MUST CHANGE LATER
function PlayerControl()
if mousex()<605 and mousex()>35
player.x=mousex()
else
if mousex()>605 then player.x=605
if mousex()<35 then player.x=35
endif
endfunction
//Manages Block health/color and draws the blocks
function ManageBoxes()
//for-next loop to cycle through blocks
for x=0 to 9
if blocks(x).hp>170
box blocks(x).x-30,blocks(x).y-15,blocks(x).x+30,blocks(x).y+15,blocks(x).hp_high,blocks(x).hp_high,blocks(x).hp_high,blocks(x).hp_high
endif
if blocks(x).hp<=170 and blocks(x).hp>85
box blocks(x).x-30,blocks(x).y-15,blocks(x).x+30,blocks(x).y+15,blocks(x).hp_med,blocks(x).hp_med,blocks(x).hp_med,blocks(x).hp_med
endif
if blocks(x).hp<=85 and blocks(x).hp>0
box blocks(x).x-30,blocks(x).y-15,blocks(x).x+30,blocks(x).y+15,blocks(x).hp_low,blocks(x).hp_low,blocks(x).hp_low,blocks(x).hp_low
endif
if blocks(x).hp<=0
blocks(x).hp=-1
blocks(x).x=-200
blocks(x).y=-200
endif
//Collision with ball
if blocks(x).collided_ball=true
blocks(x).hp=blocks(x).hp-ball.dam
blocks(x).collided_ball=false
endif
//Collision with laser
if blocks(x).collided_lsr=true
blocks(x).hp=blocks(x).hp-(ball.dam/2)
blocks(x).collided_lsr=false
endif
//Collision with gun(missile)
if blocks(x).collided_gun=true
blocks(x).hp=blocks(x).hp-blocks(x).hp
blocks(x).collided_gun=false
endif
next x
endfunction
//Controls ball collision and movement
function BallControl()
//movement
ball.x=ball.x+ball.xspeed
ball.y=ball.y+ball.yspeed
//Boundaries
//Top Boundary
if ball.y<15
ball.yspeed=abs(ball.yspeed)+.15
endif
//Left Boundary
if ball.x<15
ball.xspeed=abs(ball.xspeed)+.15
endif
//Right Boundary
if ball.x>625
ball.xspeed=(ball.xspeed*-1)-.15
endif
//Bottom boundary
if ball.y>player.y+15
Reset()
player.lives=player.lives-1
endif
//Player collision
if ball.x>player.x-25 and ball.x<player.x+25 and ball.yspeed>0
if ball.y<player.y+10 and ball.y>player.y-10
ball.yspeed=(ball.yspeed*-1)-.2
endif
endif
//Block collision
for x=0 to 9
if ball.x>blocks(x).x-30 and ball.x<blocks(x).x+30
if ball.y<blocks(x).y+15 and ball.y>blocks(x).y-15
if ball.yspeed>0
ball.yspeed=(ball.yspeed*-1)
blocks(x).collided_ball=true
endif
if ball.yspeed<0
ball.yspeed=abs(ball.yspeed)
blocks(x).collided_ball=true
endif
endif
endif
remstart
//Top collision box
if ball.x>blocks(x).x-30 and ball.x<blocks(x).x+30
if ball.y<blocks(x).y-5 and ball.y>blocks(x).y-15
ball.yspeed=ball.yspeed*-1
blocks(x).collided_ball=true
endif
endif
remend
next x
circle ball.x,ball.y,5
circle ball.x,ball.y,4
circle ball.x,ball.y,3
circle ball.x,ball.y,2
circle ball.x,ball.y,1
endfunction
//Reset and reinitialize ball data
function Reset()
ball.x=320
ball.y=240
temp=rnd(1)
if temp>=.5 then ball.xspeed=temp+1
if temp<.5 then ball.xspeed=(temp*-1)-1
ball.yspeed=-1.5
endfunction
Note: The code related to all ball collision/movement is in the function called BallControl() and block health management and drawing in the function called ManageBlocks()