
-----------------------------------
mike200015
Mon Apr 25, 2005 10:11 pm

Blockout Game Help
-----------------------------------
Ok, so im making a blockout game, and i still cant get the collision detection perfect, i got alot of it to work, but still, when the ball hits the blocks in certain spots, the blocks dont dissapear, can someone please help!! :? 

Heres the code for the whole game: 
type block :
    record
        x : int
        y : int
        hits : 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 blockhit := 0
var font3 := Font.New ("Courier New:10:bold") %All other font
%var font1 := Font.New ("Courier New:20:bold") %Title font
var keyPressed : string (1)
setscreen ("graphics:640;400,title:Mikes X-Treme Pong Game,nocursor")
for i : 1 .. upper (blocks)
    blocks (i).hits := 0
end for

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
            if blocks (i).hits ~= 1 then
                Draw.FillBox (blocks (i).x, blocks (i).y + 2, blocks (i).x + 40, blocks (i).y + 10, colours (blockcol (i)))
            end if
            yvalue += 12
        end for
        Draw.FillBox (pos - 25, 0, pos + 25, 5, 1)     %user paddle
        Draw.FillOval (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)
        Draw.Line (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 = maxy - 30 then
            ymod := -ymod                 %ball goes in opposite y-direction
            sound (500, 10)
        elsif bally  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
        /*
         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
         */
        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
        for i : 1 .. upper (blocks)
            if ballx - 10 >= blocks (i).x and ballx - 10 = blocks (i).y and bally + 10 = blocks (i).x and ballx + 10 = blocks (i).y and bally - 10 = blocks (i).x and ballx - 10 = blocks (i).y and bally - 10 = blocks (i).x and ballx + 10 = blocks (i).y and bally + 10 = blocks (i).x and ballx = blocks (i).y and bally - 10 = blocks (i).x and ballx + 10 = blocks (i).y and bally - 10 = blockx (i) and x  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 = maxy - 30 then
            ymod := -ymod                 %ball goes in opposite y-direction
            sound (500, 10)
        elsif bally  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
        for i : 1 .. upper (blocks)
            if blocks (i).hits not= 1 then
                if ballx - 10 = blocks (i).x + 40 and bally >= blocks (i).y and bally = blocks (i).x and ballx = blocks (i).y and bally = blocks (i).y and bally = blocks (i).x and ballx = blocks (i).x and ballx  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 = maxy - 30 then
            ymod := -ymod     %ball goes in opposite y-direction
            sound (500, 10)
        elsif bally  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
        /*
         if blockhit = 15 then %if all blocks are hit then displays You Win!!!
         setscreen ("nooffscreenonly")
         Font.Draw ("You Win!!!", maxx div 2 - Font.Width ("You Win!!!", font1) div 2, maxy div 2, font1, black)
         delay (700)
         Font.Draw ("*Press ENTER To Resume*", maxx div 2 - Font.Width ("*Press ENTER To Resume*", font3)
         div 2, 120, font3, blue)
         loop
         Input.Flush
         getch (keyPressed)
         exit when keyPressed = KEY_ENTER
         end loop
         exit
         end if
         */
        for i : 1 .. randblocks %checks if money block has been hit, if so, 1 point is added to score
            if blocks (randblocknum (i)).hits = 1 then
                Font.Draw ("$", moneyx (randblocknum (i)), moneyy (randblocknum (i)), font3, blue)
                if moneyx (randblocknum (i)) >= pos - 25 and moneyx (randblocknum (i)) = -6 and moneyy (randblocknum (i)) = 0 and bally = pos - 25 and ballx = 0 and bally = 0 and bally = pos - 25 and ballx = pos - 25 and ballx 