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.

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 2 Years Ago by vegaseat: loop

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..

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.

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.

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()

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 2 Years Ago by vegaseat

This question has already been answered. Start a new discussion instead.