Attempted to do simple movement in tkinter:
import tkinter as tk
class GameApp(object):
"""
An object for the game window.
Attributes:
master: Main window tied to the application
canvas: The canvas of this window
"""
def __init__(self, master):
"""
Initialize the window and canvas of the game.
"""
self.master = master
self.master.title = "Game"
self.master.geometry('{}x{}'.format(500, 500))
self.canvas = tk.Canvas(self.master)
self.canvas.pack(side="top", fill="both", expand=True)
self.start_game()
#----------------------------------------------#
def start_game(self):
"""
Actual loading of the game.
"""
player = Player(self)
#----------------------------------------------#
#----------------------------------------------#
class Player(object):
"""
The player of the game.
Attributes:
color: color of sprite (string)
dimensions: dimensions of the sprite (array)
canvas: the canvas of this sprite (object)
window: the actual game window object (object)
momentum: how fast the object is moving (array)
"""
def __init__(self, window):
self.color = ""
self.dimensions = [225, 225, 275, 275]
self.window = window
self.properties()
#----------------------------------------------#
def properties(self):
"""
Establish the properties of the player.
"""
self.color = "blue"
self.momentum = [5, 0]
self.draw()
self.mom_calc()
#----------------------------------------------#
def draw(self):
"""
Draw the sprite.
"""
self.sprite = self.window.canvas.create_rectangle(*self.dimensions, fill=self.color, outline=self.color)
#----------------------------------------------#
def mom_calc(self):
"""
Calculate the actual momentum of the thing
"""
self.window.canvas.move(self.sprite, *self.momentum)
self.window.master.after(2, self.mom_calc)
#----------------------------------------------#
#----------------------------------------------#
root = tk.Tk()
game_window = GameApp(root)
Where self.momentum
is an array containing 2 integers: one for the x movement, and another for the y movement. However, the actual movement of the rectangle is really slow (about 5 movements per second), with the self.window.master.after()
time not seeming to have an effect.
Previously on another tkinter project I had managed to get really responsive tkinter movement, so I'm just wondering if there is a way I can minimize that movement updating time in this case, by either using a different style of OOP, or just different code altogether.
UPDATE: Turns out the time in the .after()
method does matter, and it actually stacks onto the real time of the method. After using timeit
to time calling the method, I got this output:
>>> print(timeit.timeit("(self.window.master.after(2, self.mom_calc))", number=10000, globals={"self":self}))
0.5395521819053108
So I guess the real question is: Why is that .after()
method taking so long?
UPDATE 2: Tested on multiple computers, movement is still slow on any platform.
"The default Windows timer resolution is ~15ms. Trying to fire a timer every 1ms is not likely to work the way you want, and for a game is probably quite unnecessary (a display running a 60FPS updates only every ~16ms). See Why are .NET timers limited to 15 ms resolution?"
Found the solution at Python - tkinter call to after is too slow where Andrew Medico gave a good answer (in a comment).
I don't see the problem you report on Windows 10 using Python 3.6 at least. I tested the program as shown and had to add a root.mainloop()
at the end. This showed no rectangle because the object has moved off the canvas too fast to see.
So I modified this to bounce between the walls and added a counter to print the number of mom_calc
calls per second. With the after timeout set at 20ms I get 50 motion calls per second, as expected. Setting this to 2ms as in your post I get around 425 per second so there is a little error here and its taking about 2.3 or 2.4 ms per call. This is a bit variable as other processes can take up some of the time at this granularity.
Here is the (slightly) modified code:
import tkinter as tk
class GameApp(object):
"""
An object for the game window.
Attributes:
master: Main window tied to the application
canvas: The canvas of this window
"""
def __init__(self, master):
"""
Initialize the window and canvas of the game.
"""
self.master = master
self.master.title = "Game"
self.master.geometry('{}x{}'.format(500, 500))
self.canvas = tk.Canvas(self.master, background="white")
self.canvas.pack(side="top", fill="both", expand=True)
self.start_game()
#----------------------------------------------#
def start_game(self):
"""
Actual loading of the game.
"""
player = Player(self)
#----------------------------------------------#
#----------------------------------------------#
class Player(object):
"""
The player of the game.
Attributes:
color: color of sprite (string)
dimensions: dimensions of the sprite (array)
canvas: the canvas of this sprite (object)
window: the actual game window object (object)
momentum: how fast the object is moving (array)
"""
def __init__(self, window):
self.color = ""
self.dimensions = [225, 225, 275, 275]
self.window = window
self.movement = 0
self.movement_last = 0
self.properties()
#----------------------------------------------#
def properties(self):
"""
Establish the properties of the player.
"""
self.color = "blue"
self.momentum = [5, 0]
self.draw()
self.mom_calc()
self.velocity()
#----------------------------------------------#
def draw(self):
"""
Draw the sprite.
"""
self.sprite = self.window.canvas.create_rectangle(*self.dimensions, fill=self.color, outline=self.color)
#----------------------------------------------#
def mom_calc(self):
"""
Calculate the actual momentum of the thing
"""
pos = self.window.canvas.coords(self.sprite)
if pos[2] > 500:
self.momentum = [-5, 0]
elif pos[0] < 2:
self.momentum = [5, 0]
self.window.canvas.move(self.sprite, *self.momentum)
self.window.master.after(2, self.mom_calc)
self.movement = self.movement + 1
def velocity(self):
print(self.movement - self.movement_last)
self.movement_last = self.movement
self.aid_velocity = self.window.master.after(1000, self.velocity)
#----------------------------------------------#
#----------------------------------------------#
if __name__ == '__main__':
root = tk.Tk()
game_window = GameApp(root)
root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With