Im adding a blockout/bustout.. watever its called game, to my pong game. so i have 156 blocks that draw on the screen each with a random colour.. and to do that i made a record for the x,y and an array of it. So wat i need to do now is make the block dissapear when the ball hits it. But i dont know how to go about doing that. Heres the blockout part that i have so far, with bricks and ball and paddle:
There is also a few other problems with it:
1. When the paddle is moving and the ball hits it, the ball sometimes stays on the paddle and slides with it.
2. Sometimes the ball gets stuck inbetween the bricks and doesnt come out.
3. The ball sometimes slides along the bricks.
Also if there is a better way of doing this than the way i did.
If anyone can help I'd appreciate it!
code:
type block :
record
x : int
y : int
end record
var xvalue : int := -15
var yvalue : int := 0
var blocks : array 1 .. 156 of block
var colours : array 1 .. 10 of int := init (40, 50, 60, 70, 80, 90, 100, 110, 120, 130)
var blockcol : array 1 .. 156 of int
var chars : array char of boolean
var ballx := maxx div 2
var bally := 15
var pos := maxx div 2
var xmod := 1
var ymod := 1
var tries := 3
var score := 0
var font3 := Font.New ("Courier New:10:bold") %All other font
var keyPressed : string (1)
setscreen ("graphics:640;400,title:Mikes X-Treme Pong Game,nocursor")
loop
cls
for i : 1 .. 156
randint (blockcol (i), 1, 10)
end for
setscreen ("offscreenonly")
loop
cls
for i : 1 .. 156
if (i - 1) rem 12 = 0 then
xvalue += 45
yvalue := 230
end if
blocks (i).x := xvalue
blocks (i).y := yvalue
drawfillbox (blocks (i).x, blocks (i).y + 2, blocks (i).x + 40, blocks (i).y + 10, colours (blockcol (i)))
yvalue += 12
end for
drawfillbox (pos - 25, 0, pos + 25, 5, 1) %user paddle
drawfilloval (ballx, bally, 10, 10, 12) %ball
Font.Draw ("Tries Remaining: " + intstr (tries), 2, maxy - 12, font3, blue)
Font.Draw ("Score: " + intstr (score), maxx - 83, maxy - 12, font3, blue)
drawline (0, maxy - 20, maxx, maxy - 20, RGB.AddColor (0.1, 0.1, 0.1)) %top boundary line
Input.KeyDown (chars)
if chars (KEY_LEFT_ARROW) and pos - 25 > 0 then %Moves paddle left
pos -= 1
ballx -= 1
elsif chars (KEY_RIGHT_ARROW) and pos + 30 < maxx then %Moves paddle right
pos += 1
ballx += 1
elsif chars (KEY_UP_ARROW) then
exit
end if
xvalue := -15
yvalue := 0
View.Update
end loop
loop
ballx += xmod
bally += ymod
if ballx >= maxx then
xmod := -xmod %ball goes in opposite x-direction
sound (500, 10)
elsif ballx <= 0 then
xmod := -xmod %ball goes in opposite x-direction
sound (500, 10)
end if
if bally >= maxy - 30 then
ymod := -ymod %ball goes in opposite y-direction
sound (500, 10)
elsif bally <= 5 then
ymod := -ymod
sound (500, 10)
end if
for i : 1 .. 156
if (i - 1) rem 12 = 0 then
xvalue += 45
yvalue := 230
end if
blocks (i).x := xvalue
blocks (i).y := yvalue
drawfillbox (blocks (i).x, blocks (i).y + 2, blocks (i).x + 40, blocks (i).y + 10, colours (blockcol (i)))
yvalue += 12
end for
drawfillbox (pos - 25, 0, pos + 25, 5, 1) %user paddle
drawfilloval (ballx, bally, 10, 10, 12) %ball
Font.Draw ("Tries Remaining: " + intstr (tries), 2, maxy - 12, font3, blue)
Font.Draw ("Score: " + intstr (score), maxx - 83, maxy - 12, font3, blue)
drawline (0, maxy - 20, maxx, maxy - 20, RGB.AddColor (0.1, 0.1, 0.1)) %top boundary line
Input.KeyDown (chars)
if chars (KEY_LEFT_ARROW) and pos - 25 > 0 then %Moves paddle left
pos -= 2
elsif chars (KEY_RIGHT_ARROW) and pos + 25 < maxx then %Moves paddle right
pos += 2
end if
if chars ('x') then
exit
elsif chars ('p') then %Pauses game
setscreen ("nooffscreenonly")
Font.Draw ("GAME PAUSED", maxx div 2 - 20, maxy - 12, font3, blue)
Font.Draw ("*Press ENTER To Resume*", maxx div 2 - Font.Width ("*Press ENTER To Resume*", font3)
div 2, 100, font3, blue)
loop
getch (keyPressed)
exit when keyPressed = KEY_ENTER
end loop
setscreen ("offscreenonly")
end if
xvalue := -15
yvalue := 0
case whatdotcolour (ballx + 11, bally) or whatdotcolour (ballx - 11, bally) of %if ball hits anything green (2)
label 40, 50, 60, 70, 80, 90, 100, 110, 120, 130 : %it will go in opposite x-direction
xmod := -xmod
label blue, 12, yellow, black, 5, 2 :
xmod := -xmod
label :
ballx += xmod
end case
case whatdotcolour (ballx, bally + 11) or whatdotcolour (ballx, bally - 11) of %if ball hits anything green (2)
label 40, 50, 60, 70, 80, 90, 100, 110, 120, 130 : %it will go in opposite y-direction
ymod := -ymod
label blue, 12, yellow, black, 5, 2 :
ymod := -ymod
label :
bally += ymod
end case
/*
if whatdotcolour (ballx + 11, bally) = padcol or whatdotcolour (ballx - 11, bally) = padcol or
whatdotcolour (ballx + 11, bally) = blockcol () or whatdotcolour (ballx - 11, bally) = padcol then
xmod := -xmod
end if
if whatdotcolour (ballx, bally + 11) = padcol or whatdotcolour (ballx, bally - 11) = padcol then
ymod := -ymod
end if
*/
View.Update
delay (2)
cls
end loop
if chars ('x') then
exit
end if
end loop
jamonathin
Posted: Fri Apr 15, 2005 7:59 am Post subject: (No subject)
What I suggest doing is making a "hit" variable (int) in your "type". Assign all 156 "hit"s to 0, making all of them not hit. When the ball does hit that block, then make "hit" 1. When you go to redraw all of the blocks, use an if statement like this
code:
for i : 1 .. 156
if blocks(i).hit = 0 then
drawfillbox (whatever)
end if
end for
Now what I suggest for determining if the blocks are beign hit is a for loop such as this:
code:
for i : 1 .. upper (colours)
if whatdotcolour (ballx + 11, bally) = colours (i) or whatdotcolour (ballx - 11, bally) = colours (i) then
xmod *= -1 % same as xmod := - xmod
end if
if whatdotcolour (ballx, bally + 11) = colours (i) or whatdotcolour (ballx, bally - 11) = colours (i) then
ymod *= -1
end if
end for
The reason they're in two 'if' statements is because if the ball hits a corner, it will bounce off both sides (top and side).
mike200015
Posted: Fri Apr 15, 2005 1:42 pm Post subject: (No subject)
so when the ball hits the block, and the hit is set to 1, then do i redraw the block in the bakground colour.. or do i somehow delete it from the array or something??
jamonathin
Posted: Fri Apr 15, 2005 1:48 pm Post subject: (No subject)
use a for loop in when you draw all the boxes. And check every hit, and if the hit on any of the blocks = 1 then dont draw the box.
Sponsor Sponsor
mike200015
Posted: Fri Apr 15, 2005 3:24 pm Post subject: (No subject)
how do i check which block the ball hit, im using a case for the ball to bounce off:
code:
case whatdotcolour (ballx + 11, bally) or whatdotcolour (ballx - 11, bally) of %if ball hits anything green (2)
label 40, 50, 60, 70, 80, 90, 100, 110, 120, 130 : %it will go in opposite x-direction
xmod *= -1
label blue, 12, yellow, black, 5, 2 :
xmod *= -1
label :
xmod *= 1
end case
case whatdotcolour (ballx, bally + 11) or whatdotcolour (ballx, bally - 11) of %if ball hits anything green (2)
label 40, 50, 60, 70, 80, 90, 100, 110, 120, 130 : %it will go in opposite y-direction
ymod *= -1
label blue, 12, yellow, black, 5, 2 :
ymod *= -1
label :
ymod *= 1
end case
i made the hits record and set them all to 0, now i need to set to 1 when the ball hits the block, but how do i check which block the ball hit?
jamonathin
Posted: Fri Apr 15, 2005 3:40 pm Post subject: (No subject)
well, what you can do is check coordinates. Since the ball hits at the tip, you can use the x-value of the ball to determine which block is it. Now for the y-value, you need to check the radius of the ball and the location of the block.
Basically if the y-value of the ball + the radius of the ball is equal to the y-value of the block then. . . But, if that doesn't work, then check if the difference between the 2 co-ordinates is less than, 4 or so then.
So . . .
code:
for i: 1 .. blocks
if x >= blockx (i) and x <= blockx (i) and y + radius>= blocky (i) then
end if
%OR
if x >= blockx (i) and x <= blockx (i) and blocky (i) - (y + radius) <=4 then
end if
end for
I've never tried this method however, and I'm not on turing so i dont know if it will work. It sounds like it will work, but if it doesn't, juss lemme know.
mike200015
Posted: Fri Apr 15, 2005 4:30 pm Post subject: (No subject)
ok.. so.. jamonathin your code worked sort of, not totaly, i had to add sum stuff to it, but it still doesnt work right heres the code: add it in either before or after the case section in my game ^:
i changed the case a little bit, so heres the new case part, switch it for the case thats in the code/my game up there ^^:
code:
case whatdotcolour (ballx + 11, bally) or whatdotcolour (ballx - 11, bally) of %if ball hits anything green (2)
label 40, 50, 60, 70, 80, 90, 100, 110, 120, 130 : %it will go in opposite x-direction
xmod *= -1
label blue, 12, yellow, black, 5, 2 :
xmod *= -1
label :
xmod *= 1
end case
case whatdotcolour (ballx, bally + 11) or whatdotcolour (ballx, bally - 11) of %if ball hits anything green (2)
label 40, 50, 60, 70, 80, 90, 100, 110, 120, 130 : %it will go in opposite y-direction
ymod *= -1
label blue, 12, yellow, black, 5, 2 :
ymod *= -1
label :
ymod *= 1
end case
now add this part either after or before the case in the game:
code:
for i : 1 .. upper (blocks)
if ballx - 10 >= blocks (i).x and ballx - 10 <= blocks (i).x + 40 and bally + 10 >= blocks (i).y and bally + 10 <= blocks (i).y + 10 or
ballx + 10 >= blocks (i).x and ballx + 10 <= blocks (i).x + 40 and bally - 10 >= blocks (i).y and bally - 10 <= blocks (i).y + 10 then
blocks (i).hits := 1
%elsif ballx + 10 >= blocks (i).x and ballx + 10 <= blocks (i).x + 40 and bally - 10 >= blocks (i).y and bally - 10 <= blocks (i).y + 10 then
%blocks (i).hits :=1
end if
%OR
%if x >= blockx (i) and x <= blockx (i) and blocky (i) - (y + radius) <= 4 then
%end if
end for
Problems with the ball + block hit detection (code right above^):
1. When the ball hits the blocks on the side, they dont dissapear.
2. When the ball hits some blocks sort of straight up, it keeps going up and erases all of them, it doesnt really deflect off.
Cervantes
Posted: Fri Apr 15, 2005 7:28 pm Post subject: (No subject)
You know, guys, you could use a boolean variable instead of an integer. It just goes better with these situations.
Also, I recommend using coordinate collision detection here. Avoid whatdotcolour!
mike200015
Posted: Fri Apr 15, 2005 7:50 pm Post subject: (No subject)
for the whole blockout part of my game?
Also, can u help wit the question in my last post ^.
mike200015
Posted: Fri Apr 15, 2005 10:04 pm Post subject: (No subject)
i tried putting in the coordinate detection instead of color.. but it doesnt work well, im prolly doing it wrong, this is wat i put in:
code:
for i : 1 .. upper (blocks)
if ballx - 10 >= blocks (i).x and ballx - 10 <= blocks (i).x + 40 and bally + 10 >= blocks (i).y and bally + 10 <= blocks (i).y + 10 or
ballx + 10 >= blocks (i).x and ballx + 10 <= blocks (i).x + 40 and bally - 10 >= blocks (i).y and bally - 10 <= blocks (i).y + 10 then
blocks (i).hits := 1
xmod*=-1
ymod *=-1
end if
end for
Cervantes
Posted: Sat Apr 16, 2005 6:59 am Post subject: (No subject)
check here: http://www.compsci.ca/v2/viewtopic.php?t=75
The difference is that you want to detect collisions between a circle and a rectangle. It's easy enough: just change x2 - s to block (i).x and change x2 + s to block(i).x + blockWidth. Same kind of thing for the y's.
If you can't get it, ask again. But give it a shot first.