0

hi guys..
im new to this forum and im certain its here that all my problems will be solved.

In fact I have a traffic simulation project in python. Im stuck at the very start since ive never programmed in python. I would like to know how to make an object move from one place to another. that is its should display the object (e.g rectangle) moving along the say x-axis. Just let me know if you need some clarification.

I thank you all foryour consideration hoping to get a positive response from your side.

4
Contributors
7
Replies
30
Views
9 Years
Discussion Span
Last Post by pyTony
0

Here is an example using the Tkinter GUI toolkit ...

# Tkinter animate via canvas.move(obj, xAmount, yAmount)

import Tkinter as tk
import time

root = tk.Tk()

canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()

# canvas.create_rectangle(x0, y0, x1, y1, option, ... )
# x0, y0, x1, y1 are corner coordinates of ulc to lrc diagonal
rc1 = canvas.create_rectangle(20, 260, 120, 360, outline='white', fill='blue')
rc2 = canvas.create_rectangle(20, 10, 120, 110, outline='white', fill='red')

for x in range(50):
    y = x = 5
    time.sleep(0.025)
    canvas.move(rc1, x, -y)
    canvas.move(rc2, x, y)
    canvas.update()

root.mainloop()

Sorry, for some odd reason the loop code got screwed up, make this
for k in range(50)

Edited by vegaseat: loop

0

thanks loads.. this is exactly what i needed..
besides can it be done using other modules like pygame or livewires? can you please provide codes for the other modules? thanks..

0

If you search this forum for pygame you will find tons of examples of games that our forums members have creating using said module.

Either that or google.

0

Here is an example using the Tkinter GUI toolkit ...

# Tkinter animate via canvas.move(obj, xAmount, yAmount)

import Tkinter as tk
import time

root = tk.Tk()

canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()

# canvas.create_rectangle(x0, y0, x1, y1, option, ... )
# x0, y0, x1, y1 are corner coordinates of ulc to lrc diagonal
rc1 = canvas.create_rectangle(20, 260, 120, 360, outline='white', fill='blue')
rc2 = canvas.create_rectangle(20, 10, 120, 110, outline='white', fill='red')

for x in range(50):
    y = x = 5
    time.sleep(0.025)
    canvas.move(rc1, x, -y)
    canvas.move(rc2, x, y)
    canvas.update()

root.mainloop()

can you please show the rotation of the rectangle 90 degrees anticlockwise please? thanks..
im stuck on this rotation.

2

The loop counter in Vegaseat's code should not be x and the assignment should be moved out of the loop

# Tkinter animate via canvas.move(obj, xAmount, yAmount)

import Tkinter as tk
import time

root = tk.Tk()

canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()

# canvas.create_rectangle(x0, y0, x1, y1, option, ... )
# x0, y0, x1, y1 are corner coordinates of ulc to lrc diagonal
rc1 = canvas.create_rectangle(20, 260, 120, 360, outline='white', fill='blue')
rc2 = canvas.create_rectangle(20, 10, 120, 110, outline='white', fill='red')
y = x = 5

for loops in range(50):
    time.sleep(0.025)
    canvas.move(rc1, x, -y)
    canvas.move(rc2, x, y)
    canvas.update()

root.mainloop()
0

Sorry, I dodn't know what happened there 5 years ago, but the original Python25 code for the loop was like this ...

# increments for move
y = x = 5
for k in range(50):
    time.sleep(0.025)
    # move rectangle by increments x, y
    canvas.move(rc1, x, -y)
    canvas.move(rc2, x, y)
    canvas.update()

Sharp eye there PyTony!

Edited by vegaseat

1

I think you are forgiven vegaseat, considering the crime and the time passed :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.