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

Username:   Password: 
 RegisterRegister   
 Help w/ collisions in my breakout/arkanoid game.
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SuperGenius




PostPosted: Sun May 23, 2004 8:59 pm   Post subject: (No subject)

i suggest cutting down the code inside your main loop. I would eliminate the music because it gets annoying pretty fast. that would help reduce the chopiness whenever there is a collision.
Sponsor
Sponsor
Sponsor
sponsor
s_climax




PostPosted: Sun May 23, 2004 9:36 pm   Post subject: (No subject)

code:

setscreen ("graphics:400,440;offscreenonly")
const maxxy : int := 400 %Allows there to be an area with no gameplay
var speed, x, y, dir, size, dx, dy, px, py : real
var facing, mx, my, mb, xsize, ysize, brickcol, count, offset, scol : int
var start, finished, m : boolean
var chars : array char of boolean
m := true %Music
offset := 180 %Where the bricks start
xsize := 40
ysize := 20
var bricks : array 1 .. maxx div xsize, 1 .. maxxy div ysize of int %Array that holds the number of hits per brick
var cheats : array 1 .. 5 of boolean %Whether a cheat is true or false
var cheatcodes : array 1 .. 5 of string %The codes needed to be entered per cheat
var cheatselect : string
const midx : int := maxx div 2
const midy : int := maxxy div 2
const sx := maxx - ysize %Where the sould logo is
const sy := maxy - ysize %Where the sould logo is
randint (facing, 240, 300) %Direction the ball starts moving in
cheatselect := "Enter a cheat code"
speed := 1
size := 5
dir := -intreal (facing) %Direction the ball starts moving in
%dir := 270
x := midx
y := midy div 2
dx := 1
dy := -1
py := 10
px := midx
count := 0
start := true
finished := false

for i : 1 .. 5 %Make all of the cheats inactive
    cheats (i) := false
end for

cheatcodes (1) := "iwin" %Automatically win
cheatcodes (2) := "invisible" %Invisible paddle
cheatcodes (3) := "nocollisions" %Ball goes through bricks
cheatcodes (4) := "disco" %Bricks flash
cheatcodes (5) := "reversed" %Paddle moves oppositely

for i : 1 .. maxx div xsize %Make all of the bricks dead
    for ii : 1 .. maxxy div ysize
        bricks (i, ii) := 0
    end for
end for

for i : 2 .. maxx div xsize %Make certain bricks alive
    for ii : 1 .. (maxxy - 12 * ysize) div ysize
        bricks (i, ii) := 1
        count += 1
    end for
end for
count -= 4
for i : 2 .. maxx div xsize %The bottom row of bricks that alternate solidity
    for ii : 1 .. (maxxy - 19 * ysize) div ysize
        if i mod 2 = 0 then
            bricks (i, ii) := 3
        else
            bricks (i, ii) := 2
            count += 1
        end if
    end for
end for

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                          Main Loop                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for lives : 1 .. 3 %Self-explanitory
    loop
        if cheats (1) = true then %Auto-win
            count := 0
        end if
        Input.KeyDown (chars)
        if chars (KEY_CTRL) then %Key to press to enter cheats
            color (0)
            locate (1, 10)
            get cheatselect
        end if
        for i : 1 .. 5 %Checks if what you entered cooresponds to a cheat
            if cheatselect = cheatcodes (i) then
                cheats (i) := true
            end if
        end for

        colorback (7)
        drawfillbox (0, maxxy, maxx, 0, 0) %White background for playing field
        if m = true then %Changes the color of the sound icon
            scol := 10
        else
            scol := 12
        end if
        %speed += 0.0001
        delay (0)
        x += cosd (dir) * speed * dx %Makes the ball move relative to its direction and speed
        y += sind (dir) * speed * dy %Makes the ball move relative to its direction and speed
        for k : xsize .. maxx by xsize %Allows for brick collisions and drawing
            for kk : ysize .. maxxy by ysize %Allows for brick collisions and drawing

                var xx : int := k - xsize %Where the bricks are horizontally
                var yy : int := (kk - ysize) + offset %Where the bricks are vertically

                if bricks (k div xsize, kk div ysize) ~= 0 then %Checks if the brick is dead and if not goes on
                    if cheats (4) = false then %Disco bricks
                        brickcol := RGB.AddColor (0, 1 - bricks (k div xsize, kk div ysize) / 6, 1 - bricks (k div xsize, kk div ysize) / 6)
                    else %Brick colour depending on the amount of hits needed
                        brickcol := RGB.AddColor (Rand.Int (1, 100) / 100, 1 - bricks (k div xsize, kk div ysize) / 6, 1 - bricks (k div xsize, kk div ysize) / 6)
                    end if
                    drawfillbox (xx, yy, xx + xsize, ysize + yy, brickcol) %Draws the bricks
                    drawbox (xx, yy, xx + xsize, ysize + yy, 0) %Draws lines between the bricks


                    if x >= xx and x <= xx + xsize and y >= yy and y <= yy + ysize then %if the ball is within the bricks
                        if floor (x) = xx and cheats (3) = false or ceil (x) = xx + xsize and cheats (3) = false then %If it hit the left/right side and the no-collisions cheat was off
                            if dx = 1 then %Change the horizontal Direction
                                dx := -1
                            else
                                dx := 1
                            end if
                        end if
                        if floor (y) = yy and cheats (3) = false or ceil (y) = yy + ysize and cheats (3) = false then %If it hit the top/bottom side and the no-collisions cheat was off
                            if dy = 1 then %Change the vertical Direction
                                dy := -1
                            else
                                dy := 1
                            end if
                        end if
                        bricks (k div xsize, kk div ysize) -= 1 %Give a hit to the cooresponding brick
                        if bricks (k div xsize, kk div ysize) = 0 then %If the brick is dead make the overall bricks - 1
                            count -= 1
                        end if
                        if m = true then %If the music is on play a sound
                            if bricks (k div xsize, kk div ysize) = 0 then
                                Music.Sound (440, 1)
                            elsif bricks (k div xsize, kk div ysize) = 1 then
                                Music.Sound (400, 1)
                            elsif bricks (k div xsize, kk div ysize) = 2 then
                                Music.Sound (360, 1)
                            end if
                        end if
                    end if
                end if

            end for
        end for

        if start = true then % The 3/2/1 thing on startup
            colorback (white)
            color (7)
            for i : 0 .. 3
                locatexy (midx + 5, offset - ysize)
                if i < 3 then
                    put 3 - i
                    if m = true then
                        Music.Play ("16c")
                    end if
                else
                    put "GO!"
                    if m = true then
                        Music.Play ("16>c<")
                    end if
                end if
                View.Update
                delay (1000)
                if i = 3 then
                    start := false
                end if
            end for
        end if

        mousewhere (mx, my, mb) %Checks if you clicked on the sound logo
        if mb = 1 and round (sqrt ((mx - sx + 5) ** 2 + (my - sy + 5) ** 2)) <= 13 then
            if m = false then
                m := true
            else
                m := false
            end if
        end if


        if x - size < 0 then %Hits the left wall
            if dx = 1 then
                dx := -1
            else
                dx := 1
            end if
            if m = true then
                Music.Sound (480, 1)
            end if
        elsif x + size > maxx then %Hits the right wall
            if dx = 1 then
                dx := -1
            else
                dx := 1
            end if
            if m = true then
                Music.Sound (480, 1)
            end if
        elsif y - size < 10 and x > 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 <= maxx - 20 then %Move the paddle to the mouse
            if cheats (5) = false then
                px := intreal (mx)
            else
                px := maxx - (intreal (mx)) %Reverse the paddle if the cheat is on
            end if
        elsif mx <= 20 then %Stop the paddle from going past the edge
            px := 20
        else
            px := maxx - 20
        end if
        color (white)
        put "Lives: ", 4 - lives
        put "Bricks Left:", count
        %put cheats (3)
        %put cheatselect
        drawfilloval (sx, sy, 3, 3, scol) %Drawing the sound logo
        drawline (sx + 2, sy + 2, sx + 2, sy + 12, scol) %Drawing the sound logo
        drawline (sx + 2, sy + 12, sx + 12, sy + 12, scol) %Drawing the sound logo
        drawline (sx + 12, sy + 12, sx + 12, sy + 2, scol) %Drawing the sound logo
        drawfilloval (sx + 10, sy, 3, 3, scol) %Drawing the sound logo
        drawoval (sx + 5, sy + 5, 13, 13, scol) %Drawing the sound logo
        if scol = 12 then
            drawline (sx - 5, sy - 5, sx + 15, sy + 15, scol) %Drawing the sound logo
        end if
        drawfilloval (round (x), round (y), 5, 5, 7) %Drawing the ball
        if cheats (2) = false then
            drawfillbox (round (px - 20), round (py), round (px + 20), round (py - 5), 7) %Draw the paddle
        end if
        drawline (0, maxxy, maxx, maxxy, 7) %Draw a line at the top of the playing area
        View.Update
        cls
        if count = 0 then %If there are no bricks, finish
            finished := true
        end if
        exit when y < 0 or count = 0 %Exit when you miss the ball or you win
    end loop
    randint (facing, 240, 300) %Restart the game with one less live
    dir := -intreal (facing) %Restart the game with one less live
    %dir := 270
    x := midx %Restart the game with one less live
    y := midy div 2 %Restart the game with one less live
    dx := 1 %Restart the game with one less live
    dy := -1 %Restart the game with one less live
    py := 10 %Restart the game with one less live
    px := midx %Restart the game with one less live
    start := true %Restart the game with one less live
    speed := 1 %Restart the game with one less live
    exit when finished = true %Close the main loop when you win
end for

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                          End of Main Loop                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if count = 0 then
    put "You win!"
    if m = true then
        Music.Play ("2ae8e4f+2e4g+1a")
        Music.Play ("2a > 4a < 2d 4g 2d 4e <2a> 2d <4a 2c 1a ")
    end if
else
    put "You Lose!"
    if m = true then
        Music.Play ("<<4gf+f1e16efefefef1e")
    end if
end if
SuperGenius




PostPosted: Sun May 23, 2004 10:10 pm   Post subject: (No subject)

i don't know what you could do to cut it down because i can't understand your code, but in the version that you posted before i was able to turn the music off...
s_climax




PostPosted: Mon May 24, 2004 10:52 am   Post subject: (No subject)

I edited my post to include comments so that maybe you can understand it.
SuperGenius




PostPosted: Mon May 24, 2004 1:27 pm   Post subject: (No subject)

I see the problem. WHen the music is engaged every time there's a collision the program stops for a fraction of a second while the note is playing, and then resumes. If you are bent on having music i supposed you could try to put it into a process but I would just drop it altogether. Also a concern with the gameplay is how the ball just jumps downwards at the start of the game, and you can't predict where it is going to go so sometimes you would lose lives right at the start. As well I noticed that the slope of the ball's path would sometimes be very close to ablolutely vertical or horizontal, which is "wrong", as it does not happen in superbreakout/ araknoid.
s_climax




PostPosted: Mon May 24, 2004 2:07 pm   Post subject: (No subject)

I fixed the problem with the music. I found another function, Music.Sound
this way it doesn't stop the game when it palys. I updated my post with it. I don't think I would be able to solve the other thing you mentioned though.
SuperGenius




PostPosted: Mon May 24, 2004 5:49 pm   Post subject: (No subject)

another thing which you might find interesting is this editor prog i made. It facilitates the creation of levels for this type of game. It won't work for you without tweaking but i thought that you might be interested. To use it you enter the name of the output file (dont worry about extensions because it tacks on ".txt" automatically. Then you put a colour number and click/drag on the stage. When you want to switch colours click in the bottom 100px of the screen, and when you're finished put -1 for the colour. The output file will be in the same directory as the editor prog.
To use the text file just tell your game to draw a box in the normal place, and read the number for what colour to make it from the text file.



editor.t
 Description:
sick editor prog.

Download
 Filename:  editor.t
 Filesize:  1.34 KB
 Downloaded:  117 Time(s)

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 2 of 2  [ 22 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: