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.

Recommended Answers

All 7 Replies

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)

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!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.