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

Username:   Password: 
 RegisterRegister   
 messed up counter
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
x-ecutioner




PostPosted: Wed Nov 26, 2008 8:45 pm   Post subject: messed up counter

hi im making basic collision detection, you have a paddle (controlled by the users paddle) which shoots another circle at a moving square. The collision aspect behind the script works fine, the issue is the counter goes up by different numbers each time. here is my code.
any ideas?
youll find the actual hits recording located inside the squaretohit process
code:

View.Set ("graphics:500,500")
View.Set ("position:middle,middle")
setscreen ("offscreenonly")
var pervasive inc, v, x, y, xforbullet, c2,collisioncount,font1, collision : int
var pervasive distance, x1, y2 : real
var pervasive strcollision : string
var chars : array char of boolean
x := 250
y := 10
c2 := 75
xforbullet := 0
collisioncount := 0
process paddlemove
    drawfilloval (x, y, 10, 10, 255)
    loop
        Input.KeyDown (chars)
        if chars (KEY_RIGHT_ARROW) then
            if x + 10 < 500 then
                delay (30)
                drawfilloval (x, y, 10, 10, 0)
                View.Update
                x := x + 10
                drawfilloval (x, y, 10, 10, 255)
                View.Update
            end if
        end if
        if chars (KEY_LEFT_ARROW) then
            if x - 10 > 0 then
                delay (30)
                drawfilloval (x, y, 10, 10, 0)
                View.Update
                x := x - 10
                drawfilloval (x, y, 10, 10, 255)
                View.Update
            end if
        end if
    end loop
end paddlemove
collision := 0
process squaretohit
    inc := 50
    v := 1
    loop
        if v = 1 then
            inc += 10
        elsif v = 0 then
            inc -= 10
        end if
        if inc > 500 then
            v := 0
        end if
        if inc < 0 then
            v := 1
        end if
        x1 := (xforbullet - inc) ** 2
        y2 := (c2 - 250) ** 2
        distance := sqrt (x1 + y2)
        if distance > 56 then
            drawfilloval (inc, 250, 50, 50, 50)
            View.Update
            delay (30)
            drawfilloval (inc, 250, 50, 50, 0)
            View.Update
        end if
        if distance < 56 then
            collision := collision + 1
            locatexy(20,20)
            put "Hits:",collision
        end if
    end loop
end squaretohit
process shooting
    loop
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            xforbullet := x
            c2 := 75
            loop
                drawoval (xforbullet, c2, 6, 6, 255)
                View.Update
                delay (30)
                drawoval (xforbullet, c2, 6, 6, 0)
                View.Update
                exit when c2 + 10 >= 500
                c2 := c2 + 10
            end loop
        end if
    end loop
end shooting
font1 := Font.New("arial:12")
assert font1 > 0

fork paddlemove
fork shooting
fork squaretohit

thanks very much.
Sponsor
Sponsor
Sponsor
sponsor
DanielG




PostPosted: Wed Nov 26, 2008 10:10 pm   Post subject: RE:messed up counter

I haven't had much of a chance to look at your actual code, but forks are bad and evil, and may be the cause of the problem.
If they aren't, then you still shouldn't be using them. You can find the explanation somewhere in the turing help section (I think).
darkangel




PostPosted: Thu Nov 27, 2008 10:42 pm   Post subject: Re: messed up counter

DanielG is right. You shouldn't be using processes in Turing. Turing has this tendency to execute them at random times, unlike ANY other language. However processes arn't EVIL like DanielG said, just you should try to stay away from them wherever possible. Especially when your program is as simple as it is, what I suggest is replace all the processes with procedures then replace the "forks" with...
code:

loop
        paddlemove
         shooting
         squaretohit
end loop

This is the link http://compsci.ca/v3/viewtopic.php?t=7842&highlight=process by the way

EDIT: Also the reason why your getting different numbers is because you do collision detection every frame even AFTER the small ball already hit it. So lets say that in frame 3 it hits the big circle, you then get a point, then in frame 4 you add to the 'y' value and check for collision again. And guess what, the small ball is STILL inside the big one, guess we add another point. etc...

After it hits once, you have to get rid of the small ball.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: