hi! i want to make a program, that moves an object in vertical line 'till the program ends,but when it reaches the upper border, automatically appear in the lower border.


i have this

from utalcanvas import *
import time

show()
window_size(500,500)
window_style( "Mi Ventana", "black")
window_coordinates(0,0,1000,1000)

circulo = create_filled_circle(500,20,50,"green")

for x in range(100):
   y = 10 
   x = 0
   time.sleep(0.0150)
   move(circulo, -x, y)
   window_update()

"utalcanvas" is an updated version of pythong and practically identical to Tkinter, so the code is the same. i've thinking in a "for i in range(something)" for the loop of vertical movement, but i dont know how to implement it.

I've been fighting with this, hope any one can help me.

Recommended Answers

All 5 Replies

you want some variation on code like this

gracefulStart() # create circle etc
while True:
    # display the circle
    # do some work; pause; etc
    if readyToStop(): 
        break
    # adjust the location of the circle
gracefulEnd()

you want some variation on code like this

gracefulStart() # create circle etc
while True:
    # display the circle
    # do some work; pause; etc
    if readyToStop(): 
        break
    # adjust the location of the circle
gracefulEnd()

Also possibly create a second moving circle to simulate reapearance at the other side of the canvas. Why don't you use Tkinter ? Nobody has utalcanvas.

This Tkinter example moves a ball (circle) in a horizontal line, but you can change that to a vertical line:

# a Tkinter moving ball that wraps around

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title("Tkinter Moving Ball")

w = 420
h = 300
cv = tk.Canvas(root, width=w, height=h, bg='black')
cv.pack()

# create 50x50 square box for the circle boundries
box = (0, 120, 50, 170)
# create the ball object
# give it a tag name for reference
cv.create_oval(box, fill="red", tag='red_ball')

# endless animation loop till window corner x is clicked
x = 0
# set x, y increments
dx = 1
dy = 0
while True:
    # move the ball by given increments
    cv.move('red_ball', dx, dy)
    # 15 millisecond delay
    # higher value --> slower animation
    cv.after(15)
    cv.update()
    # when left side of ball hits wall reset
    x += dx
    if x >= w:
        print(x) # test
        cv.move('red_ball', -x, dy)
        x = 0


root.mainloop()

Should give you enough of a hint.

This Tkinter example moves a ball (circle) in a horizontal line, but you can change that to a vertical line:

# a Tkinter moving ball that wraps around

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title("Tkinter Moving Ball")

w = 420
h = 300
cv = tk.Canvas(root, width=w, height=h, bg='black')
cv.pack()

# create 50x50 square box for the circle boundries
box = (0, 120, 50, 170)
# create the ball object
# give it a tag name for reference
cv.create_oval(box, fill="red", tag='red_ball')

# endless animation loop till window corner x is clicked
x = 0
# set x, y increments
dx = 1
dy = 0
while True:
    # move the ball by given increments
    cv.move('red_ball', dx, dy)
    # 15 millisecond delay
    # higher value --> slower animation
    cv.after(15)
    cv.update()
    # when left side of ball hits wall reset
    x += dx
    if x >= w:
        print(x) # test
        cv.move('red_ball', -x, dy)
        x = 0


root.mainloop()

Should give you enough of a hint.

omg! excellent! this was very helpful!.there's anyway to input a random position to the ball when begins to move again?

from random import *
rlist = []
for num in range(1,100):
    rlist.append(randrange(1,1000))

for item in rlist:
    print item
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.