Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Blockout Game Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mike200015




PostPosted: Mon Apr 25, 2005 10:10 pm   Post subject: 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!! Confused

Heres the code for the whole game:
code:
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 <= 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
            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 -= 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).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 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 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 or
                    ballx >= blocks (i).x and ballx <= blocks (i).x + 40 and bally - 10 >= blocks (i).y and bally - 10 <= blocks (i).y + 10
                    then
                blocks (i).hits := 1
                %blockhit += 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
        /*
        if blockhit = 156 then
            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
        */
          /*
         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
        cls
    end loop
    if chars ('x') then
        exit
    end if
end loop

And this is the particular code that is the collision detection, which makes the blocks dissapear, which is the part i need help with:
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 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 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 or
                    ballx >= blocks (i).x and ballx <= blocks (i).x + 40 and bally - 10 >= blocks (i).y and bally - 10 <= blocks (i).y + 10
                    then
                blocks (i).hits := 1
                %blockhit += 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
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: