How to make Turing sense that you have moved something to a certain position?
Author |
Message |
Destro
|
Posted: Mon Jan 13, 2014 8:19 pm Post subject: How to make Turing sense that you have moved something to a certain position? |
|
|
What is it you are trying to achieve?
When I move the ball to a certain position, I want to to 'sense' that and draw another line.
What is the problem you are having?
I have absolutely no idea what to use to achieve this.
Describe what you have tried to solve this problem
I tried using the if command.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
This is the section with the code, keep in mind that all variables have been previously stated. It is just a long program. If you need the whole thing, let me know.
Turing: |
if chars ('g') then
cls
Draw.Line (100, 100, 500, 100, black)
x := 100
y := 100
Draw.FillOval (x, y, 7, 7, black)
View.Update
if x = 500 and y = 100 then
Draw.Line (500, 100, 500, 500, black)
View.Update
end if
end if
|
Please specify what version of Turing you are using
4.1.1
Thanks guys! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: Mon Jan 13, 2014 9:08 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
You can have two variables. One keeps track of its old position, one keeps track of its new position. If the new position is different from the old one, then do something. |
|
|
|
|
|
Destro
|
Posted: Tue Jan 14, 2014 11:19 am Post subject: Re: How to make Turing sense that you have moved something to a certain position? |
|
|
Could you explain that further? I am new to Turing so I have no idea.
I want it so that when I move the ball to the end of the line, it draws a new line.
This is my full code:
Turing: |
setscreen ("graphics:800;600,offscreenonly,nobuttonbar")
var x, y : int := 550
%Setting the parameters such as the x and y increments
x := 100
%Setting initial coordinate for x
y := 100
%Sets initial corodinate for y
%sets the screen and declares the variables
var chars : array char of boolean
%To set keys on keyboard to input a on/off signal
var h := false
Draw.FillBox (0, 0, 6000, 6000, 55)
Draw.FillBox (30, 30, 770, 570, 54)
Draw.Box (30, 30, 770, 570, black)
var mypic : int := Pic.FileNew ("turings.jpg")
var intfont : int
intfont := Font.New ("Copperplate Gothic Bold:54")
Font.Draw ("Turing", 265, 470, intfont, 42)
Font.Draw ("Etch-a-Sketch", 120, 400, intfont, black)
Draw.FillBox (270, 130, 537, 353, 55)
Pic.Draw (mypic, 280, 140, picCopy)
var intfont2 : int
intfont2 := Font.New ("Impact:18")
Font.Draw ("Use the arrow keys to move and draw. Ball CAN move diagonally", 92, 100, intfont2, black)
Font.Draw ("Use the 'C' key to clear the screen. Press any of the arrow keys to begin!", 58, 70, intfont2, black)
Font.Draw ("Use 'R' to repeat the instructions any time. Warning: Will Clear Screen!", 62, 40, intfont2, black)
%put "Welcome to the Ball Game. Move the Ball around to draw things"
%put "Press any of the arrow keys to start and press C to clear the screen."
%beginning instructions
delay (1500)
cls
%clears screen
loop
Input.KeyDown (chars )
%States the keyboard variable
if chars (KEY_UP_ARROW) then
%When you press the UP key on the keyboard it carries out the following action
y := y + 3
%It increases the previously used y variable by the allocated amount (3) for every press or hold of the Up key
Draw.FillOval (x, y, 7, 7, 54)
%Turns the circle blue for when it goes up to coincide with the rules
delay (8)
%Delays before updating the movement
View.Update
%Updates the position of the ball while removing the previous location
Draw.FillOval (x, y, 7, 7, black)
%Leaves the black trail behind the ball allowing for drawing
end if
if chars (KEY_DOWN_ARROW) then
%When you press the DOWN key, it carries out the following action(s)
y := y - 3
%Decreases the previously used y variable by the allocated number (3) for every press of the key
Draw.FillOval (x, y, 7, 7, 34)
%Makes the circle purple to coincide with the rules set out on the handout
delay (8)
%Delays the action before updating the movement so you can see the ball's movement
View.Update
%Updates the location of the ball while removing the 'previous' ball
Draw.FillOval (x, y, 7, 7, black)
%Leaves the black trail behind the ball allowing for drawing.
end if
if chars (KEY_RIGHT_ARROW) then
x := x + 3
%Makes position of ball move by 3 pixels on the x axis for every time the key is pressed
Draw.FillOval (x, y, 7, 7, 48)
%Changes colours to appropriate green
delay (8)
%Delays the movement a little bit
View.Update
%Updates the position of the ball
Draw.FillOval (x, y, 7, 7, black)
%Leaves the black trail, while getting rid of the green ball
end if
%Makes the ball go left
if chars (KEY_LEFT_ARROW) then
x := x - 3
%Decreases the position of the ball on the x axis resulting in it going left
Draw.FillOval (x, y, 7, 7, 40)
%Draws the original ball
delay (8)
%delays the movement
View.Update
%Updates the position of the ball
Draw.FillOval (x, y, 7, 7, black)
%Leaves a black trail, while getting rid of the red ball
end if
%Prevents the ball from going outside the box
if chars ('c') then
%When the c button is pressed, the following actions are carried out
cls
%It clears the screen
View.Update
%It updates the screen with the clearing of the screen
Draw.FillOval (x, y, 7, 7, black)
%Redraws the circle since the cls gets rid of the circle as well
View.Update
%Updates with the redrawn circle
end if
if chars ('r') then
var intfont3 : int
intfont3 := Font.New ("Ariel:16")
Font.Draw ("Use the arrow keys to move and draw. Use the 'C' key to clear the screen", 60, 560, intfont3, black)
Font.Draw ("Use 'R' to repeat the instructions any time during the game", 110, 530, intfont3, black)
Font.Draw ("Press any of the arrow keys to begin! Have fun!", 160, 500, intfont3, black)
View.Update
delay (3500)
cls
Draw.FillOval (x, y, 7, 7, black)
View.Update
end if
if chars ('g') then
cls
Draw.DashedLine (100, 100, 100, 500, drawDash, black)
x := 100
y := 100
Draw.FillOval (x, y, 7, 7, black)
View.Update
if y >= 500 then
Draw.DashedLine (100, 100, 500, 500, drawDash, black)
View.Update
end if
View.Update
end if
if (x >= maxx - 7) then
x := x - 3
%Sets maximum parameters for x value
end if
if (x = 7) then
x := x + 3
%Sets minimum parameters for x value
end if
if (y >= maxy - 7) then
y := y - 3
%Sets maximum parameters for y value
end if
if (y = 7) then
y := y + 3
%Sets minimum parameters for y value
end if
end loop
%Ends the entire loop
|
|
|
|
|
|
|
Raknarg
|
Posted: Tue Jan 14, 2014 11:33 am Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Just a quick note, you can comment on everything if you want, but you don't need to comment on things that are self explanatory (or if your teacher really wants you to do it)
Well let's look at your code. You have a main loop here. You can consider one iteration through the loop a frame. Each frame, you detect the user's key presses. If they press the right key, they move. You also have an x variable that keeps track of what the x position of the ball is.
Let's think about two frames. What could happen between each frame? Sometimes the player will chose to move, sometimes not. Let'salso say you had another pair of x and y variables that remembered what the position of the ball was last time. We can compare what the position is now with the old ones, and do a little analysis. What happens if they're different? That means they moves. What happens if they're he same? it means they didn't move. You can do something different in each case. For instance, here's a quick program to show you how this might work:
Turing: |
setscreen ("offscreenonly")
var oldx, oldy, x, y, b : int
Mouse.Where (x, y, b )
oldx := x
oldy := y
loop
Mouse.Where (x, y, b )
if x not= oldx or y not= oldy then
Draw.Line (oldx, oldy, x, y, black)
else
Draw.FillOval (x, y, 2, 2, black)
end if
oldx := x
oldy := y
View.Update
cls
delay (15)
end loop
|
see if you can analyze how this code works. If you need explanation, just ask. |
|
|
|
|
|
Raknarg
|
Posted: Tue Jan 14, 2014 1:56 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Sorry, I realize I likely misread your intention.
Are you saying that if the ball moves to, let's say, position 300,400 you want to do something at that moment? |
|
|
|
|
|
Destro
|
Posted: Tue Jan 14, 2014 8:12 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Yeah, that's exactly what I want. |
|
|
|
|
|
Nathan4102
|
Posted: Tue Jan 14, 2014 8:26 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Somewhere in your main loop, assuming one loop is one frame, you need to have:
Turing: | Mouse.Where(x, y, b )
if (x = 300) and (y = 400) then
.. .
end if |
Replace the ... with your code you want happening when x is 300 and y is 400. |
|
|
|
|
|
Destro
|
Posted: Tue Jan 14, 2014 8:27 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Do I need the Mouse.Where? My program has nothing to do with the mouse :( |
|
|
|
|
|
Sponsor Sponsor
|
|
|
evildaddy911
|
Posted: Tue Jan 14, 2014 8:36 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
no, the x and y coordinates should be replaced with whatever you are checking the location of |
|
|
|
|
|
Destro
|
Posted: Tue Jan 14, 2014 8:44 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Alright, I made it work, by moving it out of the "if chars ('g') then" command. Thanks guys!
However, I want it only to draw another line when you activate the game by pressing 'g'. By moving it out, it draws the line all the time. |
|
|
|
|
|
Nathan4102
|
Posted: Tue Jan 14, 2014 9:56 pm Post subject: RE:How to make Turing sense that you have moved something to a certain position? |
|
|
Apologies, I was going off Raknargs post. Can you show us your new main loop? |
|
|
|
|
|
|
|