Python Forum
How to make tkinter object move endlessly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make tkinter object move endlessly
#1
Hi, so I´m kinda noob in python, I just started learning it. I got this game in mind where u have a ball going from top to bottom without changing x dimension. I´m desperately trying to make the ball loop up and down in a smooth movement however with no results. The loop should end when I press space, which will shoot the ball to pre generated goal on the left side of the screen. Any suggestions on how to do that? I searched the internet however only really sophisticated and difficult algorythms are there...Im desoerate please help.

import tkinter
can=tkinter.Canvas(width=1000,height=700)
from random import*
can.pack()

y=100
def ball():
    can.create_oval(500,y,600,y+100)
ball()

i=0
while i == 0:
    if y<500:
        can.move(ball,0,5)
    else:
        can.move(ball,0,-5)
This is what I created, however it doesnt work...
Reply
#2
This jumped out at me:
while i == 0:
    print(y) ##  <--- add this line.
    if y<500:
        can.move(ball,0,5)
    else:
        can.move(ball,0,-5)
And most examples I see have another parameter here, but I honestly don't know if it's important:
can=tkinter.Canvas(***HERE***, width=1000,height=700)
Reply
#3
Here is a working example from my toolbox. You will have to remove the x axis portion.
import tkinter as tk

class BounceTest():
    def __init__(self):
        self.running=True
        self.window = tk.Tk()
        self.canvas = tk.Canvas(self.window, width = 400, height = 300)
        self.canvas.grid()

        self.x0 = 10
        self.y0 = 50
        self.x1 = 60
        self.y1 = 100
        self.deltax = 2
        self.deltay = 3
        self.which=self.canvas.create_oval(self.x0, self.y0, self.x1, self.y1,
                                      fill="red", tag='red_ball')

        tk.Button(self.window, text="Stop/Start Ball", bg='yellow',
               command=self.stop_it).grid(row=1, column=0)
        tk.Button(self.window, text="Exit", bg='orange',
               command=self.window.quit).grid(row=2, column=0)
        self.window.after(50, self.move_the_ball)

        self.window.mainloop()

    def move_the_ball(self):
        if self.running:
            self.canvas.move(self.which, self.deltax, self.deltay)
            ## check for ball hitting edges & change direction
            if self.x1 >= 400:
               self.deltax = -2
            if self.x0 < 0:
               self.deltax = 2
            if self.y1 > 290:
                self.deltay = -3
            if self.y0 < 0:
                self.deltay = 3

            ## keep track of the coordinates to tell when
            ## the ball moves off canvas
            self.x0 += self.deltax
            self.x1 += self.deltax
            self.y0 += self.deltay

            self.window.after(25, self.move_the_ball)

    def stop_it(self):
        self.running=not self.running
        if self.running:
            self.move_the_ball()

if __name__ == "__main__":
    BounceTest() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass an object to a class, then make an object of it and pass again TomasAm 11 4,562 Nov-09-2020, 04:47 PM
Last Post: buran
  Cant move object in game mrsneakys 1 2,248 Mar-25-2019, 01:01 PM
Last Post: ichabod801
  AttributeError: 'tuple' object has no attribute 'move' senfik99 2 4,044 Feb-26-2019, 12:42 PM
Last Post: stullis
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,496 Mar-05-2018, 03:48 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020