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

Username:   Password: 
 RegisterRegister   
 block breaker help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: Thu Oct 26, 2006 3:52 pm   Post subject: block breaker help

code:
setscreen ("offscreenonly")

var padx, pady, padheight, padwidth, life, col : int
var ballx, bally, velx, vely, blocklength, blockheight : int
var dropx, dropy, dropi, dropr, dropc, timer : int
var blockx, blockx2, blockx3, blockyr1, blockyr2, blockyr3 : array 1 .. 19 of int
var blocknum : int
var x, y, button : int
var sdrop, getdrop : boolean := false
var font1, font2, font3, font4 : int

font1 := Font.New ("serif:12")
font2 := Font.New ("sans serif:18:bold")
font3 := Font.New ("mono:9")
font4 := Font.New ("Palatino:24:Bold,Italic")
blocknum := 7
padheight := 5
padwidth := 70
ballx := 200
bally := 300
velx := 2
vely := 2
blocklength := 30
blockheight := 10
life := 1
randint (dropr, 360, 380)
randint (dropc, 0, maxx)
dropx := dropc
dropy := dropr
assert font1 > 0 and font2 > 0 and font3 > 0 and font4 > 0


for decblock : 1 .. 18
    blockx (decblock) := 10
    blockx2 (decblock) := 10
    blockx3 (decblock) := 10
    blockyr1 (decblock) := 380
    blockyr2 (decblock) := 360
    blockyr3 (decblock) := 340
end for

for r1dec : 2 .. 18
    blockx (r1dec) := blockx (r1dec - 1) + 35
    blockx2 (r1dec) := blockx2 (r1dec - 1) + 35
    blockx3 (r1dec) := blockx3 (r1dec - 1) + 35
end for

loop
    View.Update
    delay (5)
    cls
    mousewhere (x, y, button)
    drawfillbox (x, 20, x + padwidth, 20 + padheight, 7)
    drawfilloval (ballx, bally, 5, 5, 7)
    ballx += velx
    bally += vely
    if ballx >= maxx then
        velx *= -1
    elsif ballx <= 0 then
        velx *= -1
    end if
    if bally >= maxy then
        vely *= -1
    elsif bally <= 0 then
        vely *= -1
    end if
    if ballx > x and ballx < x + padwidth - 56 and bally > 20 and bally < 20 + padheight then
        vely *= -1
        velx := 3
    end if
    if ballx > x + 14 and ballx < x + padwidth - 42 and bally > 20 and bally < 20 + padheight then
        vely *= -1
        velx := 2
    end if
    if ballx > x + 28 and ballx < x + padwidth - 28 and bally > 20 and bally < 20 + padheight then
        vely *= -1
        velx := 1
    end if
    if ballx > x + 42 and ballx < x + padwidth - 14 and bally > 20 and bally < 20 + padheight then
        vely *= -1
        velx := -2
    end if
    if ballx > x + 56 and ballx < x + padwidth and bally > 20 and bally < 20 + padheight then
        vely *= -1
        velx := -3
    end if


    for row1 : 1 .. 18
        drawfillbox (blockx (row1), blockyr1 (row1), blockx (row1) + blocklength, blockyr1 (row1) + blockheight, 7)
        drawfillbox (blockx2 (row1), blockyr2 (row1), blockx2 (row1) + blocklength, blockyr2 (row1) + blockheight, 7)
        drawfillbox (blockx3 (row1), blockyr3 (row1), blockx3 (row1) + blocklength, blockyr3 (row1) + blockheight, 7)
    end for

    for colr1 : 1 .. 18
        if ballx > blockx (colr1) and ballx < blockx (colr1) + blocklength and bally > blockyr1 (colr1) and bally < blockyr1 (colr1) + blockheight then
            blockx (colr1) := 5000
            vely *= -1
            bally -= 10
        end if
        if ballx > blockx2 (colr1) and ballx < blockx2 (colr1) + blocklength and bally > blockyr2 (colr1) and bally < blockyr2 (colr1) + blockheight then
            blockx2 (colr1) := 5000
            vely *= -1
            bally -= 10
        end if
        if ballx > blockx3 (colr1) and ballx < blockx3 (colr1) + blocklength and bally > blockyr3 (colr1) and bally < blockyr3 (colr1) + blockheight then
            blockx3 (colr1) := 5000
            vely *= -1
            bally -= 10
        end if
    end for

    if ballx > dropc and ballx < dropc + 30 and bally > dropr and bally < dropr + 10 and sdrop = false then
        sdrop := true
    end if
    if sdrop = true then
        drawfilloval (dropx, dropy, 5, 5, 12)
        dropy -= 1
        if x < dropx and x + padwidth > dropx and 20 > dropy and 20 < dropy + 3 then
            life += 1
            dropx := 5000
            getdrop := true
        end if
    end if
    locate (maxrow - 1, maxcol)
    put life
    if getdrop = false then
        timer := 0
    end if
    if getdrop = true and timer < 500 then
        timer += 1
        randint (col, 1, 14)
        Font.Draw ("EXTRA LIFE", maxx div 2 - 20, maxy div 2, font2, col)
    end if
end loop



ok, play the game and you will notice that sometimes the ball hits like 10 blocks before coming back down. Can anyone tell me why that is please?
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Thu Oct 26, 2006 5:36 pm   Post subject: (No subject)

It Appears To Be An Error In Collision Detection, Or Possibly That Ball Is Going Inbetween.
shoobyman




PostPosted: Sat Oct 28, 2006 2:38 pm   Post subject: (No subject)

yes, that i know, but how do i fix it?
Ultrahex




PostPosted: Sat Oct 28, 2006 3:30 pm   Post subject: (No subject)

The Code is Badly Layed out, and im not sure where your Collision Detection IS, so its hard for me to determine where your collision detection is done, this makes it extremely hard for me to help you.

Ontop of this, i personally rather not spoon feed you the information, there is a tutorial (or many) in the Turing Help forum on Collision Detection on this forum, give it a search.

If You Need More Help Clean Up OR comment your code so we can see how you are currently doing collision detection.
Expirant




PostPosted: Sat Oct 28, 2006 6:42 pm   Post subject: (No subject)

I'm assuming it hits more than one block at a time simply because that's the way the ball is deflecting off the bricks. It's possible your collision detection isn't a hundred percent accurate (but I couldn't tell you where you've gone wrong, as was pointed out, uncommented code is tough to help people with).

Maybe comment your code and someone can help.

(First post for me, I know! I've been reading the forums for some time and this place is really awesome to see good coding in action...for the most part. Hope I can improve my coding and possibly others' from being part of the forum!)
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  [ 5 Posts ]
Jump to:   


Style:  
Search: