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

Username:   Password: 
 RegisterRegister   
 Making a cricle follow the x and y of another
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mobin12




PostPosted: Fri Dec 23, 2011 6:49 pm   Post subject: Making a cricle follow the x and y of another

What is it you are trying to achieve?
trying to make the red circle to follow the x and y coordinates of the blue circle, no matter what (including time when the blue circle does not move)


What is the problem you are having?
Can't get the red circle to follow the blue circle around the screen


Describe what you have tried to solve this problem
<Answer Here>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


setscreen ("graphics: 800;500")
View.Set ("offscreenonly")
var x : int := 400 % Player x
var y : int := 250 % Player y
var x2 : int := 7 % Red enemy x
var y2 : int := 7 % Red enemy y
var chars : array char of boolean
loop
    drawfilloval (x, y, 10, 10, 55)
    drawfilloval (x2, y2, 7, 7, 41)
    Input.KeyDown (chars)
    if chars ('d') and x < 789 then
        x := x + 1
    end if
    if chars ('a') and x > 10 then
        x := x - 1
    end if
    if chars ('w') and y < 489 then
        y := y + 1
    end if
    if chars ('s') and y > 10 then
        y := y - 1
    end if
    View.Update
    delay (11)
    cls
end loop




Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Dec 23, 2011 6:58 pm   Post subject: RE:Making a cricle follow the x and y of another

Looks to me like you haven't even tried yet. I see no code that uses x2 or y2.
mobin12




PostPosted: Sat Dec 24, 2011 8:36 am   Post subject: Re: Making a cricle follow the x and y of another

i am clueless on how i should approach this problem
Insectoid




PostPosted: Sat Dec 24, 2011 9:13 am   Post subject: RE:Making a cricle follow the x and y of another

There are two common ways to approach the problem here.

The easy, ugly way: When do you want the red ball to go up? When do you want it to go down? Left? Right? The answer to that question is literally the code to do it.

The harder, prettier way: Use trig to calculate where the red ball should travel. I suggest you try the easy way first.
ThatGuyYouLove




PostPosted: Sat Dec 24, 2011 9:52 am   Post subject: Re: Making a cricle follow the x and y of another

I think the easiest way to do this is to divide the screen into quarters around your enemy
i.e
Blueball(enemy) starts at 7, 7 or x2, y2
then you compare it to yourself and your position, i.e x, y
therefore when you detect for where to move...
code:
if x2 > x then
x2 += 1
elsif x2 < x then
x2 -= 1
end if

if y2 > y then
y2 += 1
elsif y2 < y then
y2 -= 1
end if


So if they are past the enemy on either scale, the enemy moves towards them, and if they are even than they do nothing on that axis.
Insectoid




PostPosted: Sat Dec 24, 2011 10:40 am   Post subject: RE:Making a cricle follow the x and y of another

You got that backwards bro. That code will make the red ball run away.
mobin12




PostPosted: Sat Dec 24, 2011 11:21 am   Post subject: Re: Making a cricle follow the x and y of another

I WANT THE REDBALL TO MOVE TO THE POSITION OF THE BLUE, EVEN IF THE BLUE BALL IS NOT MOVING.
Insectoid




PostPosted: Sat Dec 24, 2011 12:28 pm   Post subject: RE:Making a cricle follow the x and y of another

You didn't answer the questions I asked.
Quote:
When do you want the red ball to go up? When do you want it to go down? Left? Right?
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Dec 24, 2011 12:45 pm   Post subject: RE:Making a cricle follow the x and y of another

Here, I'll answer one of those for you.

1: When do you want the red ball to go up?
A: When the blue ball is above it.
Therefore: When the blue is above the red ball, then you want the red ball to go up. Fairly simple.
mobin12




PostPosted: Sat Dec 24, 2011 1:44 pm   Post subject: Re: RE:Making a cricle follow the x and y of another

yes this is exactly my answer

Quote:
Here, I'll answer one of those for you.

1: When do you want the red ball to go up?
A: When the blue ball is above it.
Therefore: When the blue is above the red ball, then you want the red ball to go up. Fairly simple.

and when the BLUE is on right of the RED, move the RED right
when the BLUE is on left of the RED, move the RED left
when the BLUE is on below of the RED, move the RED down
same with diagonals.
Insectoid




PostPosted: Sat Dec 24, 2011 2:06 pm   Post subject: RE:Making a cricle follow the x and y of another

I guess now the question is, how do you know when the red ball is above the blue ball?

And diagonals don't matter. They just happen, when you go left and right at the same time. If you do it right, you won't have to write a single line of code for diagonals.
mobin12




PostPosted: Sat Dec 24, 2011 4:00 pm   Post subject: Re: RE:Making a cricle follow the x and y of another

Quote:
I guess now the question is, how do you know when the red ball is above the blue ball?

RIGHT: When x > x2
LEFT: When x < x2
DOWN: When y < y2
UP: When y > y2
mobin12




PostPosted: Sat Dec 24, 2011 4:32 pm   Post subject: Re: Making a cricle follow the x and y of another

This is my attempt
code:

setscreen ("graphics: 800;500")
View.Set ("offscreenonly")
var x : int := 400 % Player x
var y : int := 250 % Player y
var x2 : int := 7 % Red enemy x
var y2 : int := 7 % Red enemy y
var chars : array char of boolean
loop
    drawfilloval (x, y, 10, 10, 55)
    drawfilloval (x2, y2, 7, 7, 41)
    Input.KeyDown (chars)
    if chars ('d') and x < 789 then
        x := x + 2
    end if
    if chars ('a') and x > 10 then
        x := x - 2
    end if
    if chars ('w') and y < 489 then
        y := y + 2
    end if
    if chars ('s') and y > 10 then
        y := y - 2
    end if
    % Attempt on enemy follow
    if x > x2 then
        x2 := x2 + 1
    end if
    if x < x2 then
        x2 := x2 - 1
    end if
    if y > y2 then
        y2 := y2 + 1
    end if
    if y < y2 then
        y2 := y2 - 1
    end if
    View.Update
    delay (15)
    cls
end loop
Insectoid




PostPosted: Sat Dec 24, 2011 5:45 pm   Post subject: RE:Making a cricle follow the x and y of another

That looks like it should work!

Protip:
code:
x2 := x2+1

can be written as
code:
x2 += 1


It's the same thing. The 2nd is just nicer to read.
mobin12




PostPosted: Sat Dec 24, 2011 8:57 pm   Post subject: RE:Making a cricle follow the x and y of another

thanks also can you help me with collision dectection
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 4  [ 51 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: