
-----------------------------------
s_climax
Sat May 15, 2004 6:44 pm

Help w/ collisions in my breakout/arkanoid game.
-----------------------------------
For some reasons its not really working at all any more.  It seems to be a problem with the collisions of the ball with the bricks.


setscreen ("graphics:400,400;offscreenonly")
var speed, x, y, dir, size, dx, dy, px, py : real
var facing, mx, my, mb, xsize, ysize, bricknumbers : int
bricknumbers := 110
var bricks : array 1 .. bricknumbers of boolean
const midx : int := maxx div 2
const midy : int := maxy div 2
randint (facing, 0, 359)
speed := 1
size := 5
dir := intreal (facing)
dir := 270
x := midx
y := midy div 2
dx := 1
dy := 1
py := 10
px := midx
xsize := 40
ysize := 20
for i : 1 .. bricknumbers
    bricks (i) := true
end for
loop
    delay (1)
    x += cosd (dir) * speed * dx
    y += sind (dir) * speed * dy
    for xx : 0 .. maxx by xsize
        for yy : 200 .. maxy by ysize
            if bricks (round ((((xx) / xsize) + 1) * (((yy) / ysize) - 9))) = true then
                drawfillbox (xx, yy, xx + xsize, ysize + yy, 8)
                drawbox (xx, yy, xx + xsize, ysize + yy, 0)
                if x = xx + xsize then
                    if dx = 1 then
                        dx := -1
                    else
                        dx := 1
                    end if
                    bricks (round ((((xx) / xsize) + 1) * (((yy) / ysize) - 9))) := false
                end if
                if y = yy + ysize then
                    if dy = 1 then
                        dy := -1
                    else
                        dy := 1
                    end if
                end if
            end if
        end for
    end for

    mousewhere (mx, my, mb)


    if x - size < 0 then
        if dx = 1 then
            dx := -1
        else
            dx := 1
        end if
    elsif x + size > maxx then
        if dx = 1 then
            dx := -1
        else
            dx := 1
        end if
    elsif y - size < 10 and y  px - 20 and x < px + 20 then
        if dy = 1 then
            dy := -1
        else
            dy := 1
        end if
        dir += ((x - px) * 2) * dx
    elsif y + size > maxy then
        if dy = 1 then
            dy := -1
        else
            dy := 1
        end if
    end if

    px := intreal (mx)

    drawfilloval (round (x), round (y), 5, 5, 7)
    drawfillbox (round (px - 20), round (py), round (px + 20), round (py - 5), 7)
    View.Update
    cls
    exit when y < 0
end loop


-----------------------------------
SuperGenius
Sat May 15, 2004 9:48 pm


-----------------------------------
when I ran your code the ball just hovers in the middle of the screen, fluctuating within a range of a couple pixels.

-----------------------------------
Dan
Sat May 15, 2004 11:05 pm


-----------------------------------
the problem is with your colsione dection with the bricks and the ball:

1. you need to uses and not or
2. both the y and x ifs have to be the same if not 2 diffrent ones
3. you mixed up the > and < singes.

the if should look somting like this:

if x >= xx and x = yy and y = xx and x = yy and y  maxx then
        if dx = 1 then
            dx := -1
        else
            dx := 1
        end if
    elsif y - size < 10 and y  px - 20 and x < px + 20 then
        if dy = 1 then
            dy := -1
        else
            dy := 1
        end if
        dir += ((x - px) * 2) * dx
    elsif y + size > maxy then
        if dy = 1 then
            dy := -1
        else
            dy := 1
        end if
    end if

    px := intreal (mx)

    drawfilloval (round (x), round (y), 5, 5, 7)
    drawfillbox (round (px - 20), round (py), round (px + 20), round (py - 5), 7)
    View.Update
    cls
    exit when y < 0
end loop


it is not perfitck but it will get you on the right track.

-----------------------------------
s_climax
Sun May 16, 2004 12:03 am


-----------------------------------
Thanks, but how would I get the balls to bounce off the bricks in the right direction?

That is the only thing I can't figure out.  I've managed to add the ability to lose, lives, make room around the edges, and add colors.  I simply cannot figure out how to make the balls bounce off the bricks correctly.  Any help?


setscreen ("graphics:400,400;offscreenonly")
var speed, x, y, dir, size, dx, dy, px, py : real
var facing, mx, my, mb, xsize, ysize, bricknumbers, brickcol, count,offset : int
offset:=180
bricknumbers := 110
xsize := 40
ysize := 20
var bricks : array 1 .. maxx div xsize, 1 .. maxy div ysize of boolean
const midx : int := maxx div 2
const midy : int := maxy div 2
randint (facing, 240, 300)
speed := 2
size := 5
dir := -intreal (facing)
%dir := 270
x := midx
y := midy div 2
dx := 1
dy := -1
py := 10
px := midx
count := 0

for i : 1 .. maxx div xsize
    for ii : 1 .. maxy div ysize
        bricks (i, ii) := false
    end for
end for

for i : 2 .. maxx div xsize
    for ii : 1 .. (maxy div ysize) - 2
        bricks (i, ii) := true
        count += 1
    end for
end for

count -= ((maxx-xsize) div xsize) * ((maxy - offset-(ysize*2)) div ysize)
for lives : 1 .. 3
    loop
        speed += 0.0001
        delay (0)
        x += cosd (dir) * speed * dx
        y += sind (dir) * speed * dy

        for k : xsize .. maxx by xsize
            for kk : ysize .. maxy by ysize

                var xx : int := k - xsize
                var yy : int := (kk - ysize) + offset

                if bricks (k div xsize, kk div ysize) = true then
                    brickcol := RGB.AddColor (1, kk / 201, 1 - kk / 201)

                    drawfillbox (xx, yy, xx + xsize, ysize + yy, brickcol)
                    drawbox (xx, yy, xx + xsize, ysize + yy, 0)


                    if x >= xx and x = yy and y  maxx then
            if dx = 1 then
                dx := -1
            else
                dx := 1
            end if
        elsif y - size < 10 and y  px - 20 and x < px + 20 then
            if dy = 1 then
                dy := -1
            else
                dy := 1
            end if

            if dx = 1 then
                dir += - ((x - px) * 2)
            else
                dir += ((x - px) * 2)
            end if


        elsif y + size > maxy then
            if dy = 1 then
                dy := -1
            else
                dy := 1
            end if
        end if

        px := intreal (mx)

        %put lives
        %put count
        drawfilloval (round (x), round (y), 5, 5, 7)
        drawfillbox (round (px - 20), round (py), round (px + 20), round (py - 5), 7)
        View.Update
        cls
        exit when y < 0 or count = 0
    end loop
    dir := -intreal (facing)
    %dir := 270
    x := midx
    y := midy div 2
    dx := 1
    dy := -1
    py := 10
    px := midx
    delay (1000)
end for


-----------------------------------
s_climax
Mon May 17, 2004 6:21 pm


-----------------------------------

                    if x >= xx and x = yy and y = xx and x = yy and y  px - 20 and x < px + 20 then %If it hit the paddle
            if dy = 1 then
                dy := -1
            else
                dy := 1
            end if
            if m = true then
                Music.Sound (300, 1)
            end if
            if dx = 1 then
                dir += - ((x - px) * 2) %Chenge the balls direction based on where on the paddle it hits
            else
                dir += ((x - px) * 2)
            end if


        elsif y + size > maxxy then %If it hits the top
            if dy = 1 then
                dy := -1
            else
                dy := 1
            end if
            if m = true then
                Music.Sound (480, 1)
            end if
        end if

        if mx >= 20 and mx 