How to make a pogram that remembers where you clicked and makes ball goto it
Author |
Message |
zero44
|
Posted: Sat Sep 19, 2009 9:14 am Post subject: How to make a pogram that remembers where you clicked and makes ball goto it |
|
|
i wanna make a pogram where you click at 2 postions and then it remembers those postions as int variables then makes a ball got to the co-ordinate's in order as saved this is what i tried but failed
Turing: |
var choice : string
var key : string (1) := ""
var chars : array char of boolean
var x, y, b : int
var savex, savex2, savey, savey2 : int := 0
var turn : int := 1
% loop 2----------------------------------------------------------------------------
% make ball follow mouse ---------------------------------------------------------------
var ballx, bally : int := 200
loop
setscreen ("offscreenonly")
View.Update
Draw.FillBox (0, 0, maxx, maxy, blue)
Draw.FillOval (ballx, bally, 20, 20, 12)
mousewhere (x, y, b )
if y > bally then
bally := bally + 2
end if
if y < bally then
bally := bally - 2
end if
if x > ballx then
ballx := ballx + 2
end if
if x < ballx then
ballx := ballx - 2
end if
if b = 1 and turn = 1
then
savex := ballx
savey := bally
delay (5)
turn := turn + 1
end if
if b = 1 and turn = 2
then
savex2 := ballx
savey2 := bally
end if
if turn = 2 then
cls
loop
setscreen ("offscreenonly")
View.Update
Draw.FillBox (0, 0, maxx, maxy, blue)
Draw.FillOval (ballx, bally, 20, 20, 12)
mousewhere (x, y, b )
if savey > bally then
bally := bally + 2
end if
if savey < bally then
bally := bally - 2
end if
if savex > ballx then
ballx := ballx + 2
end if
if savex < ballx then
ballx := ballx - 2
end if
if savey2 > bally then
bally := bally + 2
end if
if savey2 < bally then
bally := bally - 2
end if
if savex2 > ballx then
ballx := ballx + 2
end if
if savex2 < ballx then
ballx := ballx - 2
end if
colorback (255)
color (10)
locate (1, 1)
put "The App" ..
locate (2, 1)
put "The ball is at x= ", x, " y= ", y ..
locate (3, 1)
put "Save position 1 is x= ", savex, " y= ", savey ..
put " Save position 2 is x= ", savex2, " y= ", savey2, " turn = ", turn ..
end loop
end if
% make pogram talk ---------------------------------------------------------------
colorback (255)
color (10)
locate (1, 1)
put "The App" ..
locate (2, 1)
put "The ball is at x= ", x, " y= ", y ..
locate (3, 1)
put "Save position 1 is x= ", savex, " y= ", savey ..
put " Save position 2 is x= ", savex2, " y= ", savey2, " turn = ", turn ..
end loop
|
[/syntax] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
andrew.
|
Posted: Sat Sep 19, 2009 9:45 am Post subject: RE:How to make a pogram that remembers where you clicked and makes ball goto it |
|
|
You need to put a delay or wait for the mouse up before you get the second x,y coordinates. They show up as the same thing because you can't hold the mouse down for as long as only one loop cycle. |
|
|
|
|
|
zero44
|
Posted: Sat Sep 19, 2009 2:06 pm Post subject: Re: How to make a pogram that remembers where you clicked and makes ball goto it |
|
|
i tried a delay and it didnt work |
|
|
|
|
|
Superskull85
|
Posted: Sat Sep 19, 2009 6:02 pm Post subject: Re: How to make a pogram that remembers where you clicked and makes ball goto it |
|
|
A couple of problems.
First, when you are saving the second position you are copying data from the balls position (ballx, bally) instead of the clicked position (x, y). This:
Turing: | if b = 1 and turn = 2
then
savex2 := ballx
savey2 := bally
end if |
As opposed to this:
Turing: | if b = 1 and turn = 2
then
savex2 := x
savey2 := y
end if |
Second, in your second loop you have the following code:
Turing: | if savey > bally then
bally := bally + 2
end if
if savey < bally then
bally := bally - 2
end if
if savex > ballx then
ballx := ballx + 2
end if
if savex < ballx then
ballx := ballx - 2
end if
if savey2 > bally then
bally := bally + 2
end if
if savey2 < bally then
bally := bally - 2
end if
if savex2 > ballx then
ballx := ballx + 2
end if
if savex2 < ballx then
ballx := ballx - 2
end if |
Because your second loop is about moving the ball towards the second point (savex2, savey2) you do not need to deal with the first point (savex, savey) (hint: only one set of if statements are needed).
I think that is what you want, but you may in fact want to move the ball from point one to point two. If that is the case than you would want to first set the balls position (ballx, bally) to point ones position (savex, savey) and increment the balls position using the vector formed from point one to point two (vectorx=savex2-savex, vectory=savey2-savey).
Also, when you want to print the balls position you should use the ball coordinate variables instead of the mouse coordinate variables. So this:
Turing: | put "The ball is at x= ", ballx, " y= ", bally .. |
Instead of this:
Turing: | put "The ball is at x= ", x, " y= ", y .. |
Hope that helped. |
|
|
|
|
|
|
|