Computer Science Canada

Need Help Making Graphics Move By Themselves In Turing

Author:  The Lone Ranger [ Thu Dec 10, 2009 6:05 pm ]
Post subject:  Need Help Making Graphics Move By Themselves In Turing

I am currently making a game in Turing, and I need to know how to make graphics or pictures fall from the top of the screen by themselves. They also need to fall randomly at any point in the program.

Author:  Tony [ Thu Dec 10, 2009 8:15 pm ]
Post subject:  RE:Need Help Making Graphics Move By Themselves In Turing

When you say "by themselves" I assume you mean "have many pictures move" and the way you do that is that you move one image, then another, then another... Use of loops is helpful.

Author:  The Lone Ranger [ Sat Dec 12, 2009 1:06 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

no, the problem is I dont know how to make my picture move. What I want to have is a picture of an asteroid falling down from the top of the screen. I don't know how to do that. Please help me if you can.

Author:  TheGuardian001 [ Sat Dec 12, 2009 1:47 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

When you draw a picture using Pic.Draw, two of the parameters you pass to it are the X and Y coordinates of the picture.

If you change these each time you draw, the picture will move.

Author:  The Lone Ranger [ Sat Dec 12, 2009 1:49 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Could you please show me an example program, please. It would really help me out.

Author:  Tony [ Sat Dec 12, 2009 3:01 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

TheGuardian001 @ Sat Dec 12, 2009 1:47 pm wrote:
If you change [position] each time you draw, the picture will move.

The Lone Ranger @ Sat Dec 12, 2009 1:49 pm wrote:
Could you please show me an example program, please. It would really help me out.

Turing:

put "Movement is a change in position"
delay(1000)
put "You draw image here "..
delay(1000)
Draw.FillOval(100, maxy-100, 50, 50, red)
delay(500)
put "... and as the time passes ... "..
for x:1..250 by 10
    Draw.Line(150 + x, maxy-100, 150 + x+5, maxy-100, black)
    delay(100)
end for
put "you end up here"
delay(500)
Draw.FillOval(450, maxy-100, 50, 50, blue)
locate(11,1)
put "Movement!"

Author:  The Lone Ranger [ Sat Dec 12, 2009 3:15 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Thanks alot for the sample program. But I feel like I should show the real program that I started just for some more help, if you don't mind. I need to get the asteroid to keep falling from the top of the screen, so that who ever is playing can dodge the asteroids. Please help.


View.Set ("graphics:640,427")

var name : string
var chars : array char of boolean
var pic : int := Pic.FileNew ("F:\\images2.jpg")
var backGround : int := Pic.FileNew ("F:\\back.jpg")
var asteroid : int := Pic.FileNew ("F:\\Asteroid.jpg")
var x, y : int
var x1, y1 : int
x := 10
y := 10


randint (x1, 0, 255)
randint (y1, 0, maxx)

for decreasing i : maxy .. 0 by 15

put "Please enter your name player one: " ..
put skip
get name

loop
Pic.Draw (backGround, 0, 0, picCopy)

if backGround = 0 then
put "Unable to load JPEG: ", Error.LastMsg
end if

Pic.Draw (asteroid, x1, y1, picCopy)
if asteroid = 0 then
put "Unable to load BMP: ", Error.LastMsg
end if

loop

Input.KeyDown (chars)

if chars (KEY_RIGHT_ARROW) then
x := x + 5
elsif chars (KEY_LEFT_ARROW) then
x := x - 5
elsif chars (KEY_UP_ARROW) then
y := y + 5
elsif chars (KEY_DOWN_ARROW) then
y := y - 5
end if

if pic = 0 then
put "Unable to load JPEG: ", Error.LastMsg
end if

Pic.Draw (pic, x, y, picCopy)

delay (10)

end loop


end loop

end for

drawfilloval (200, 400, 300, 100, green)
[/quote]

Author:  Tony [ Sat Dec 12, 2009 3:29 pm ]
Post subject:  RE:Need Help Making Graphics Move By Themselves In Turing

You are drawing the asteroid outside of the loop, always at the same location. You want to draw it together with the player
code:

Pic.Draw (pic, x, y, picCopy)
Pic.Draw (asteroid, x1, y1, picCopy)

And you would obviously have to change the x1/y1 in the same loop as you change x/y.

P.S. there is no need to do "Unable to load JPEG:" checks inside the loop. The check is for loading the image, not drawing it.

Author:  The Lone Ranger [ Sat Dec 12, 2009 3:35 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Sorry to be a bother, but could you please put the,
Pic.Draw (pic, x, y, picCopy)
Pic.Draw (asteroid, x1, y1, picCopy)

into my program, I'm not exactly sure where you want me to put. I hope you wont mind.

Author:  Tony [ Sat Dec 12, 2009 3:43 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

The Lone Ranger @ Sat Dec 12, 2009 3:35 pm wrote:
could you please put the [ code ] into my program

No. Your assignment is to figure this out.

Author:  The Lone Ranger [ Sat Dec 12, 2009 3:45 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Well then could you please clarify what you want me to do once more please?

Author:  Tony [ Sat Dec 12, 2009 3:48 pm ]
Post subject:  RE:Need Help Making Graphics Move By Themselves In Turing

You seem to know how to move and draw the player image, so apply the same knowledge to add in the asteroid as well.

Author:  The Lone Ranger [ Sat Dec 12, 2009 4:12 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Just so I'm clear, what is the desired outcome you want me to achieve?

Author:  Tony [ Sat Dec 12, 2009 4:37 pm ]
Post subject:  RE:Need Help Making Graphics Move By Themselves In Turing

I'm just giving you hints, so that you can achieve whatever goals you set for yourself. If you are finding things to be too difficult, step back and trying something less ambitious first. If you'd like to challenge yourself, we'll be here to guide you as far as you'd like to go.

Author:  The Lone Ranger [ Sat Dec 12, 2009 4:42 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

I actually have gotten somewhere. But I did not get my desired result yet. Instead of the asteroid falling down fomr the top of the screen, I just get the picture of the asteroid appearing in different parts of the screen, like an array. But i would like to thank you for all of your help so far.

Author:  The Lone Ranger [ Sun Dec 13, 2009 2:14 pm ]
Post subject:  Re: RE:Need Help Making Graphics Move By Themselves In Turing

Tony @ Sat Dec 12, 2009 3:29 pm wrote:
You are drawing the asteroid outside of the loop, always at the same location. You want to draw it together with the player
code:

Pic.Draw (pic, x, y, picCopy)
Pic.Draw (asteroid, x1, y1, picCopy)

And you would obviously have to change the x1/y1 in the same loop as you change x/y.

P.S. there is no need to do "Unable to load JPEG:" checks inside the loop. The check is for loading the image, not drawing it.


Sorry for the double post, but I just put the asteroid inside the loop , and I did change the x1/y1 in the same loop as the x/y, but my asteroids still don't fall down. The picture of the asteroid just keeps poping up every where on the screen. I dont know if it was something I did, but could you please tell me if this was my fault, or something that you told me to do. I would really appreciate that,

Here's what I changed the program to,
View.Set ("graphics:640,427")

var name : string
var chars : array char of boolean
var pic : int := Pic.FileNew ("F:\\images2.jpg")
var backGround : int := Pic.FileNew ("F:\\back.jpg")
var asteroid : int := Pic.FileNew ("F:\\Asteroid.jpg")
var x, y : int
var x1, y1 : int
x := 10
y := 10

put "Please enter your name player one: " ..
put skip
get name

loop
Pic.Draw (backGround, 0, 0, picCopy)

if backGround = 0 then
put "Unable to load JPEG: ", Error.LastMsg
end if

if asteroid = 0 then
put "Unable to load BMP: ", Error.LastMsg
end if

loop

Input.KeyDown (chars)

if chars (KEY_RIGHT_ARROW) then
x := x + 5
elsif chars (KEY_LEFT_ARROW) then
x := x - 5
elsif chars (KEY_UP_ARROW) then
y := y + 5
elsif chars (KEY_DOWN_ARROW) then
y := y - 5
end if
randint (x1, 0, maxx)
randint (y1, 0, maxy)
if pic = 0 then
put "Unable to load JPEG: ", Error.LastMsg
end if
Pic.Draw (pic, x, y, picCopy)
Pic.Draw (asteroid, x1, y1, picCopy)
delay (10)


end loop


end loop

Author:  Tony [ Sun Dec 13, 2009 2:39 pm ]
Post subject:  RE:Need Help Making Graphics Move By Themselves In Turing

could you explain those two lines that you've added?
code:

randint (x1, 0, maxx)
randint (y1, 0, maxy)

Author:  The Lone Ranger [ Sun Dec 13, 2009 2:42 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

The zeroes make it so that asteroids pop up anywhere, I will change it so that they are alawys at the top later on, once I figure out how to make the asteroids fall.

Author:  TheGuardian001 [ Sun Dec 13, 2009 3:17 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

I think what tony meant is, Is there a reason you put those in the loop? Is randomly generating coordinates something you want to repeat?

Author:  The Lone Ranger [ Sun Dec 13, 2009 3:20 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

I thought that randomly generating coordinates would make the asteroids appear anywhere, which they do, but the picture of the asteroid still doesn't fall down from the top of the screen.

Author:  TheGuardian001 [ Sun Dec 13, 2009 3:45 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

That's because you aren't moving them. There is no gravity in programming. If you want something to move, you have to do it yourself.

In your loop, you randomly generate a new coordinate each time, then draw the picture at that new coordinate. This will produce asteroids popping up randomly around the screen.

If you want the asteroid to move down, what should you be doing to the coordinates in the loop, instead of randomizing them?

Author:  The Lone Ranger [ Sun Dec 13, 2009 3:49 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

I don't know. Maye take the coordinates out of the loop?

Author:  Tony [ Sun Dec 13, 2009 4:06 pm ]
Post subject:  RE:Need Help Making Graphics Move By Themselves In Turing

You want to have something like
code:

set-up/initialize your variables
loop
   move player
   move asteroid
end loop

The random initial position does the "initialization" type of work, something you want to happen just once, so it's a good idea to put that outside of the loop.

You would still need to add more code to describe how the asteroid moves while inside the loop; and I've hinted that you should take a look at how the player moves.

Author:  TheGuardian001 [ Sun Dec 13, 2009 4:09 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Take the randomization out? Yes. But that won't move the thing down.

Draw a dot on a piece of graph paper, then draw another dot down 3 units. What have you done to the coordinates to get it to move down? Do this in your program, and the asteroid will fall.

Author:  The Lone Ranger [ Sun Dec 13, 2009 4:12 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

I just made some changes to the program, I'vegot the asteroid to fall down, but a new problem has come up. The asteroid only falls in the same place and I can't move my character anymore. Can anyone help me out?

Here's the new program,

View.Set ("graphics:640,427")

var name : string
var chars : array char of boolean
var pic : int := Pic.FileNew ("F:\\images2.jpg")
var backGround : int := Pic.FileNew ("F:\\back.jpg")
var asteroid : int := Pic.FileNew ("F:\\Asteroid.jpg")
var x, y : int
var x1, y1 : int
x := 10
y := 10
y1 := 427

put "Please enter your name player one: " ..
put skip
get name

procedure game
randint (x1, 100, 599)
Pic.Draw (pic, x, y, picCopy)
Pic.Draw (asteroid, x1, y1, picCopy)
loop
Pic.Draw (asteroid, x1, y1, picCopy)
y1 := y1 - 10
delay (10)
if y1 < 0 then
y1 := 427
Pic.Draw (asteroid, x1, y1, picCopy)
y1 := y1-10
end if
end loop

end game


loop
Pic.Draw (backGround, 0, 0, picCopy)

if backGround = 0 then
put "Unable to load JPEG: ", Error.LastMsg
end if

if asteroid = 0 then
put "Unable to load BMP: ", Error.LastMsg
end if

loop

Input.KeyDown (chars)

if chars (KEY_RIGHT_ARROW) then
x := x + 5
elsif chars (KEY_LEFT_ARROW) then
x := x - 5
elsif chars (KEY_UP_ARROW) then
y := y + 5
elsif chars (KEY_DOWN_ARROW) then
y := y - 5
end if

if pic = 0 then
put "Unable to load JPEG: ", Error.LastMsg
end if
randint (x1, 100, maxx)
randint (y1, 280, maxy)
game
delay (10)


end loop


end loop

Author:  TheGuardian001 [ Sun Dec 13, 2009 4:59 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

The reason you can no longer move your character is that you suddenly added a new procedure called game, and are now running in a loop inside that procedure. When you have a loop inside of a procedure, that procedure becomes blocking. Since there is a loop inside the procedure, that procedure will never end, and the rest of the code will be ignored.

Instead of having loops inside of a procedure, you should loop the call to the procedure. For example this:
code:

proc game
    loop
        x -= 1
        Pic.Draw(img,x,20,0)
        %Never exits, nothing else will run.
    end loop
end game
loop
    %stuff
    %more stuff
    game
end loop


Is wrong. Game will never exit and the rest of your main program will never run.
This:
code:

proc game
    x -= 1
    Pic.Draw(img,x,20,0)
end game
loop
    %stuff
    %more stuff
    game
end loop

Is right. Since there is no loop inside of game, the program will continue once the end of game is reached.

Author:  The Lone Ranger [ Sun Dec 13, 2009 5:44 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

OK, but what about the fact that the asteroid only falls in one place during the program.

Also I took the loop out of the procedure, and I was back to where I started, the asteroids don't fall anymore, they just keep apperaring again, and again.

Author:  TheGuardian001 [ Sun Dec 13, 2009 7:01 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

That's because inside of your loop, you still have these two lines:
code:

randint (x1, 100, maxx)
randint (y1, 280, maxy)

As you know, these lines will randomize the asteroids position. If you have these in your loop, the position will randomize every single time. This is not what you want.

You also have this line:
code:

randint (x1, 100, 599)

Inside of your game procedure. This means that every time you run game to move the asteroid down, you will instead send it to a random location. If you don't want it to randomly jump around, don't randomize the position.

As for it only falling in one spot, you know when the asteroid hits the bottom, so once it does, just randomize where it will fall from. You already have code to move it back up to the top, you just need to combine that with code that will move it left or right.

Author:  The Lone Ranger [ Sun Dec 13, 2009 7:25 pm ]
Post subject:  Re: Need Help Making Graphics Move By Themselves In Turing

Ok, thank you, you guys have been so, much help. All I have to do is make the asteroid not fall in the same place and I'll be done.
Thanks again.


: