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

Username:   Password: 
 RegisterRegister   
 Game help (moving circle)
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Azzy123




PostPosted: Sat May 09, 2009 1:15 pm   Post subject: Game help (moving circle)

What is it you are trying to achieve?
I need to make it so that when my bigger yellow circle passes over the smaller circles, the smaller circles completely disappear- currently only parts of them are erased.


What is the problem you are having?
I have the distance formula, but no matter where I put it in my code, it's never called on/ never works.

Describe what you have tried to solve this problem
I've tried changing the formula a bit. I've also placed it nearly everywhere in my code.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
I've tried using this as my formula:
Turing:

if sqrt ((x - a) ** 2 + (y - b) ** 2) <= (radius + 5) then
drawfilloval (a, b, 5, 5, black)
end if


and then I put it under the " %redcookie" part of my code. I just need to make one circle (cookie) get eaten.

Turing:


var chars : array char of boolean
var x, y, a, b, c, d, e, f, g, h, i, j : int
var radius : int := 10
var step : int := 2
var counter : int := 0
var isredcookie, isgreencookie, isorangecookie, iscyancookie, ismagentacookie : boolean := false

View.Set ("graphics, nobuttonbar,offscreenonly")
colorback (16)
cls

x := maxx div 2
y := maxy div 2

loop
    Input.KeyDown (chars)
    drawfilloval (x, y, radius, radius, 16)
    % UP-----------------------------------------------------
    if chars (KEY_UP_ARROW) and y <= maxy - 14 then

        y := y + step
    end if
    % DOWN---------------------------------------------------
    if chars (KEY_DOWN_ARROW) and y >= (0 + 14) then

        y := y - step
    end if
    % LEFT---------------------------------------------------
    if chars (KEY_LEFT_ARROW) and (x >= 0 + 11) then

        x := x - step
    end if
    % RIGHT--------------------------------------------------
    if chars (KEY_RIGHT_ARROW) and x <= (maxx - 11) then

        x := x + step
    end if
    drawfilloval (x, y, radius, radius, 14)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %redcookie
    if counter >= 500 then
        if isredcookie then
            drawfilloval (a, b, 5, 5, 16)



            isredcookie := false
        else
            randint (a, 5, maxx - 5)
            randint (b, 5, maxy - 5)
            drawfilloval (a, b, 5, 5, brightred)

            isredcookie := true


        end if
        counter := 0
    else
        counter := counter + 1
    end if
    %greencookie
    if counter >= 500 then
        if isgreencookie then
            drawfilloval (c, d, 5, 5, 16)
            isgreencookie := false
        else
            randint (c, 5, maxx - 5)
            randint (d, 5, maxy - 5)
            drawfilloval (c, d, 5, 5, brightgreen)
            isgreencookie := true
        end if
        counter := 0
    else
        counter := counter + 1

    end if

    %orangecookie
    if counter >= 500 then
        if isorangecookie then
            drawfilloval (e, f, 5, 5, 16)
            isorangecookie := false
        else
            randint (e, 5, maxx - 5)
            randint (f, 5, maxy - 5)
            drawfilloval (e, f, 5, 5, 42)
            isorangecookie := true
        end if
        counter := 0
    else

        counter := counter + 1
    end if
    %cyancookie
    if counter >= 500 then
        if iscyancookie then
            drawfilloval (g, h, 5, 5, 16)
            iscyancookie := false
        else
            randint (g, 5, maxx - 5)
            randint (h, 5, maxy - 5)
            drawfilloval (g, h, 5, 5, 52)
            iscyancookie := true
        end if
        counter := 0
    else
        counter := counter + 1
    end if
    %magentacookie
    if counter >= 500 then
        if ismagentacookie then
            drawfilloval (i, j, 5, 5, 16)
            ismagentacookie := false
        else
            randint (i, 5, maxx - 5)
            randint (j, 5, maxy - 5)
            drawfilloval (i, j, 5, 5, 36)
            ismagentacookie := true
        end if
        counter := 0
    else
        counter := counter + 1
    end if

    delay (10)


    View.Update

end loop



Please specify what version of Turing you are using
4.1

Edit: Sorry if it's a bit messy / impractical. It's my first attempt and I don't want to change it.
Sponsor
Sponsor
Sponsor
sponsor
Azzy123




PostPosted: Sat May 09, 2009 3:24 pm   Post subject: RE:Game help (moving circle)

Bump.. This is urgent, thanks.
Kharybdis




PostPosted: Sat May 09, 2009 5:22 pm   Post subject: RE:Game help (moving circle)

Your problem is that when your big circle moves over a small one, the trail of the big circle (you're moving it by increasing/decreasing its x and y position) is overlapping the smaller circle.

To fix it, one way to do it (albeit not an impressive way) is when the x and y of the big circle are in the range defined by the small cookie (of x and y), then assign that small cookie to be black instead of its original colour.

You can use the whatdotcolor function to do this, although i don't think it would be that efficient.
Azzy123




PostPosted: Sat May 09, 2009 6:13 pm   Post subject: RE:Game help (moving circle)

Er yeah. As I stated in my original post, that's what I've been trying to do. But it won't work. No idea why.

I've been trying to use
Turing:
if sqrt ((x - a) ** 2 + (y - b) ** 2) <= (radius + 5) then
drawfilloval (a, b, 5, 5, black)
end if
OneTwo




PostPosted: Sat May 09, 2009 6:58 pm   Post subject: Re: Game help (moving circle)

Hey there, I've found a solution for you. Your current formula does not work, I haven't really thought of why it doesnt work but I made a solution for you.

Solution:

Here is a simple formula that will solve your problem, change a, b accordingly:

code:
   
if ((x + y) - (a + b) < 10) and ((x+y) - (a-b) > - 10) then
      drawfilloval (a, b, 5, 5, 16)
      a := 0
      b := 0
end if


Explanation:

code:
if ((x + y) - (a + b) < 10) and ((x+y) - (a-b) > - 10) then


This is a simple formula, it gets the sum of the center coordinates of your yellow oval and the centre co-ordinates of your red dot. It then substracts it from each other. It then checks whether the difference is less than 10 or - 10 (10 is the radius of your yellow oval).

code:
drawfilloval (a, b, 5, 5, 16)
      a := 0
      b := 0


When your condition has been met, it will draw another oval over your red dot making it disappear. A and B is set to 0 so that you don't see a grey circle over your yellow oval. Comment out a := 0 and b := 0 and you'll see what I mean.

Well that is all, hopefully I've helped you. : D
TheGuardian001




PostPosted: Sat May 09, 2009 7:02 pm   Post subject: Re: Game help (moving circle)

but you're only drawing it black if it disappears on its own (after the timer).

what you need to do is check if the big yellow circle has hit the cookie, then set isredcookie (or whatever colour cookie) to false.

in pseudo code
Turing:


if player hit redcookie then
    isredcookie := false
end if

if player hit greencookie then
    isgreencookie := false
end if


and so on.
if you don't know how to do collision detection, I suggest you look
here
for square collisions (less effective in this case, but easier), or
here
for circular collision detection (a bit more complicated, but more accurate)

Edit: aw.... ninja'd by OneTwo
OneTwo




PostPosted: Sat May 09, 2009 7:19 pm   Post subject: Re: Game help (moving circle)

Sorry very small mistake,

code:
if ((x + y) - (a + b) < 10) and ((x+y) - (a-b) > - 10) then
      drawfilloval (a, b, 5, 5, 16)
      a := 0
      b := 0
end if


That should read as

code:
if ((x + y) - (a + b) < 10) and ((x+y) - (a+b) > - 10) then
      drawfilloval (a, b, 5, 5, 16)
      a := 0
      b := 0
end if


Mistake:
((x+y) - (a-b) > - 10
Corrected:
((x+y) - (a+b) > - 10
Azzy123




PostPosted: Sat May 09, 2009 7:49 pm   Post subject: RE:Game help (moving circle)

Hi, sorry, either method still does not work. Where in the code would I need to put it? Thanks a lot for the help so far. Smile
Sponsor
Sponsor
Sponsor
sponsor
OneTwo




PostPosted: Sat May 09, 2009 8:39 pm   Post subject: Re: Game help (moving circle)

Hey, the code works fine as I have just tested it. As for the location of the code, you can put it anywhere so long as it is executed every single time your object (yellow oval) moves.

Example: it can be right after "loop ..." or even right before "end loop"

code:
loop

<CONDITIONAL CODE HERE>

    Input.KeyDown (chars)
    drawfilloval (x, y, radius, radius, 16)
    % UP-----------------------------------------------------
    if chars (KEY_UP_ARROW) and y <= maxy - 14 then

        y := y + step
    end if
    % DOWN---------------------------------------------------
    if chars (KEY_DOWN_ARROW) and y >= (0 + 14) then

        y := y - step
    end if
    % LEFT---------------------------------------------------
    if chars (KEY_LEFT_ARROW) and (x >= 0 + 11) then

        x := x - step
    end if

...


OR


code:
...

  elsif ((x + y) - (g + h) < 20) and ((x+y) - (g+h) > - 20) then
      drawfilloval (g, h, 5, 5, 16)
      g := 0
      h := 0 
  elsif ((x + y) - (i + j) < 20) and ((x+y) - (i + j) > - 20) then
      drawfilloval (i, j, 5, 5, 16)
      i := 0
      j := 0 
    end if


<CONDITIONAL CODE HERE>
   
    delay (10)
    View.Update

end loop


Also note that you must change the variables(a, b) for it to work for the other cookies if you haven't already done so.
Azzy123




PostPosted: Sat May 09, 2009 8:57 pm   Post subject: RE:Game help (moving circle)

Hmmmmmmm. I have no idea why mine isn't working. I've done everything you've said, and I understand your formula.. but it the cookie still isn't being eaten.

:s My brain feels like liquid. Thanks for your patience..
OneTwo




PostPosted: Sat May 09, 2009 9:25 pm   Post subject: Re: Game help (moving circle)

Hey, I added my code in to yours and here's the result:

I just changed the BG to gray, no worries you could change it to whatever you want.


Result:
code:
var chars : array char of boolean
var x, y, a, b, c, d, e, f, g, h, i, j : int := 0
var radius : int := 10
var step : int := 2
var counter : int := 0
var isredcookie, isgreencookie, isorangecookie, iscyancookie, ismagentacookie : boolean := false

View.Set ("graphics, nobuttonbar,offscreenonly")
colorback (24)
cls

x := maxx div 2
y := maxy div 2

loop
    Input.KeyDown (chars)
    drawfilloval (x, y, radius, radius, 24)
    % UP-----------------------------------------------------
    if chars (KEY_UP_ARROW) and y <= maxy - 14 then

        y := y + step
    end if
    % DOWN---------------------------------------------------
    if chars (KEY_DOWN_ARROW) and y >= (0 + 14) then

        y := y - step
    end if
    % LEFT---------------------------------------------------
    if chars (KEY_LEFT_ARROW) and (x >= 0 + 11) then

        x := x - step
    end if
    % RIGHT--------------------------------------------------
    if chars (KEY_RIGHT_ARROW) and x <= (maxx - 11) then

        x := x + step
    end if
    drawfilloval (x, y, radius, radius, 14)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %redcookie
    if counter >= 500 then
        if isredcookie then
            drawfilloval (a, b, 5, 5, 24)



            isredcookie := false
        else
            randint (a, 5, maxx - 5)
            randint (b, 5, maxy - 5)
            drawfilloval (a, b, 5, 5, brightred)

            isredcookie := true


        end if
        counter := 0
    else
        counter := counter + 1
    end if
    %greencookie
    if counter >= 500 then
        if isgreencookie then
            drawfilloval (c, d, 5, 5, 24)
            isgreencookie := false
        else
            randint (c, 5, maxx - 5)
            randint (d, 5, maxy - 5)
            drawfilloval (c, d, 5, 5, brightgreen)
            isgreencookie := true
        end if
        counter := 0
    else
        counter := counter + 1

    end if

    %orangecookie
    if counter >= 500 then
        if isorangecookie then
            drawfilloval (e, f, 5, 5, 24)
            isorangecookie := false
        else
            randint (e, 5, maxx - 5)
            randint (f, 5, maxy - 5)
            drawfilloval (e, f, 5, 5, 42)
            isorangecookie := true
        end if
        counter := 0
    else

        counter := counter + 1
    end if
    %cyancookie
    if counter >= 500 then
        if iscyancookie then
            drawfilloval (g, h, 5, 5, 24)
            iscyancookie := false
        else
            randint (g, 5, maxx - 5)
            randint (h, 5, maxy - 5)
            drawfilloval (g, h, 5, 5, 52)
            iscyancookie := true
        end if
        counter := 0
    else
        counter := counter + 1
    end if
    %magentacookie
    if counter >= 500 then
        if ismagentacookie then
            drawfilloval (i, j, 5, 5, 24)
            ismagentacookie := false
        else
            randint (i, 5, maxx - 5)
            randint (j, 5, maxy - 5)
            drawfilloval (i, j, 5, 5, 36)
            ismagentacookie := true
        end if
        counter := 0
    else
        counter := counter + 1
    end if
Text.Locate (1,1)
put (x+y) - (a+b), ",", (x+y) - (c+d), ",", (x+y) - (e + f), ",", (x+y) - (g + h), ",", (x+y) - (i + j)   
   
if ((x + y) - (a + b) <= 10) and ((x+y) - (a+b) >= - 10) then
      drawfilloval (a, b, 5, 5, 24)
      a := 0
      b := 0
end if
if ((x + y) - (c + d) <= 10) and ((x+y) - (c+d) >= - 10) then
      drawfilloval (c, d, 5, 5, 24)
      c := 0
      d := 0
end if
if((x + y) - (e + f) <= 10) and ((x+y) - (e+f) >= - 10) then
      drawfilloval (e, f, 5, 5, 24)
      e := 0
      f := 0
end if
if ((x + y) - (g + h) <= 10) and ((x+y) - (g+h) >= - 10) then
      drawfilloval (g, h, 5, 5, 24)
      g := 0
      h := 0
end if
if ((x + y) - (i + j) <= 10) and ((x+y) - (i+j) >= -10) then
      drawfilloval (i, j, 5, 5, 24)
      i := 0
      j := 0
end if
   
    delay (10)
    View.Update

end loop


Also note the minor changes to the conditional:

From:

code:
if ((x + y) - (a + b) < 10) and ((x+y) - (a+b) > - 10) then


To:
code:
if ((x + y) - (a + b) <= 10) and ((x+y) - (a+b) >= - 10) then
Azzy123




PostPosted: Sat May 09, 2009 9:36 pm   Post subject: RE:Game help (moving circle)

Hey, thanks a lot, but why is the timing so different? The cookies appear too quickly / slowly. Also, sometimes the red cookie appears at the bottom left of the screen (half appears on the screen and the other bit is cut off). The cookies seem to disappear randomly.

Edit: Yeah, they seem to disappear when I approach from their left sides.
OneTwo




PostPosted: Sat May 09, 2009 10:05 pm   Post subject: RE:Game help (moving circle)

It seems it has something to do with the way your counter works,so try experimenting with different ways to draw your random ovals. It's getting fairly late, i'll help you more tomorrow if that's fine with you.
Azzy123




PostPosted: Sat May 09, 2009 10:07 pm   Post subject: RE:Game help (moving circle)

Yes, I'll keep working on it. Thank you so much.
OneTwo




PostPosted: Sun May 10, 2009 12:13 pm   Post subject: Re: Game help (moving circle)

Hey, it looks like my math was way off yesterday. Your formula is correct here is the more accurate code that should be used:

code:
var chars : array char of boolean
var x, y, a, b, c, d, e, f, g, h, i, j : int := 0
var radius : int := 10
var step : int := 2
var counter : int := 0
var isredcookie, isgreencookie, isorangecookie, iscyancookie, ismagentacookie : boolean := false

View.Set ("graphics, nobuttonbar,offscreenonly")
colorback (24)
cls

x := maxx div 2
y := maxy div 2

loop
    Input.KeyDown (chars)
    drawfilloval (x, y, radius, radius, 24)
    % UP-----------------------------------------------------
    if chars (KEY_UP_ARROW) and y <= maxy - 14 then

        y := y + step
    end if
    % DOWN---------------------------------------------------
    if chars (KEY_DOWN_ARROW) and y >= (0 + 14) then

        y := y - step
    end if
    % LEFT---------------------------------------------------
    if chars (KEY_LEFT_ARROW) and (x >= 0 + 11) then

        x := x - step
    end if
    % RIGHT--------------------------------------------------
    if chars (KEY_RIGHT_ARROW) and x <= (maxx - 11) then

        x := x + step
    end if
    drawfilloval (x, y, radius, radius, 14)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %redcookie
    if counter >= 500 then
        if isredcookie then
            drawfilloval (a, b, 5, 5, 24)



            isredcookie := false
        else
            randint (a, 5, maxx - 5)
            randint (b, 5, maxy - 5)
            drawfilloval (a, b, 5, 5, brightred)

            isredcookie := true


        end if
        counter := 0
    else
        counter := counter + 1
    end if
    %greencookie
    if counter >= 500 then
        if isgreencookie then
            drawfilloval (c, d, 5, 5, 24)
            isgreencookie := false
        else
            randint (c, 5, maxx - 5)
            randint (d, 5, maxy - 5)
            drawfilloval (c, d, 5, 5, brightgreen)
            isgreencookie := true
        end if
        counter := 0
    else
        counter := counter + 1

    end if

    %orangecookie
    if counter >= 500 then
        if isorangecookie then
            drawfilloval (e, f, 5, 5, 24)
            isorangecookie := false
        else
            randint (e, 5, maxx - 5)
            randint (f, 5, maxy - 5)
            drawfilloval (e, f, 5, 5, 42)
            isorangecookie := true
        end if
        counter := 0
    else

        counter := counter + 1
    end if
    %cyancookie
    if counter >= 500 then
        if iscyancookie then
            drawfilloval (g, h, 5, 5, 24)
            iscyancookie := false
        else
            randint (g, 5, maxx - 5)
            randint (h, 5, maxy - 5)
            drawfilloval (g, h, 5, 5, 52)
            iscyancookie := true
        end if
        counter := 0
    else
        counter := counter + 1
    end if
    %magentacookie
    if counter >= 500 then
        if ismagentacookie then
            drawfilloval (i, j, 5, 5, 24)
            ismagentacookie := false
        else
            randint (i, 5, maxx - 5)
            randint (j, 5, maxy - 5)
            drawfilloval (i, j, 5, 5, 36)
            ismagentacookie := true
        end if
        counter := 0
    else
        counter := counter + 1
    end if
   
   
if sqrt ((x - a) ** 2 + (y - b) ** 2) <= (radius + 5) then
        drawfilloval (a, b, 5, 5, 24)
        a := 0
        b := 0
   
end if
if sqrt ((x - c) ** 2 + (y - d) ** 2) <= (radius + 5) then
      drawfilloval (c, d, 5, 5, 24)
      c := 0
      d := 0
end if
if sqrt ((x - e) ** 2 + (y - f) ** 2) <= (radius + 5) then
      drawfilloval (e, f, 5, 5, 24)
      e := 0
      f := 0
end if
if sqrt ((x - g) ** 2 + (y - h) ** 2) <= (radius + 5) then
      drawfilloval (g, h, 5, 5, 24)
      g := 0
      h := 0
end if
if sqrt ((x - i) ** 2 + (y - j) ** 2) <= (radius + 5) then
      drawfilloval (i, j, 5, 5, 24)
      i := 0
      j := 0
end if
   
    delay (10)
    View.Update

end loop


I'm very sorry if I made you confused in any way.
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 2  [ 28 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: