generated from jittat/tk-cat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamelib.py
167 lines (118 loc) · 3.93 KB
/
gamelib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import tkinter as tk
import tkinter.ttk as ttk
from abc import ABC, abstractmethod
from utils import distance
class GameElement(ABC):
@abstractmethod
def show(self):
pass
@abstractmethod
def hide(self):
pass
@abstractmethod
def render(self):
pass
@abstractmethod
def delete(self):
pass
class GameCanvasElement(GameElement):
def __init__(self, game_app, x=0, y=0):
self.x = x
self.y = y
self.canvas = game_app.canvas
self.is_visible = True
self.init_canvas_object()
self.init_element()
self.to_be_deleted = False
def show(self):
self.is_visible = True
self.canvas.itemconfigure(self.canvas_object_id, state="normal")
def hide(self):
self.is_visible = False
self.canvas.itemconfigure(self.canvas_object_id, state="hidden")
def render(self):
if self.is_visible:
self.canvas.coords(self.canvas_object_id, self.x, self.y)
def delete(self):
self.canvas.delete(self.canvas_object_id)
def distance_to(self, element):
return distance(self.x, self.y, element.x, element.y)
def is_within_distance(self, element, d):
return self.distance_to(element) <= d
def init_canvas_object(self):
pass
def init_element(self):
pass
def update(self):
pass
class Text(GameCanvasElement):
def __init__(self, game_app, text, x=0, y=0):
self.text = text
super().__init__(game_app, x, y)
def init_canvas_object(self):
self.canvas_object_id = self.canvas.create_text(
self.x,
self.y,
text=self.text)
def set_text(self, text):
self.text = text
self.canvas.itemconfigure(self.canvas_object_id, text=text)
class Sprite(GameCanvasElement):
def __init__(self, game_app, image_filename, x=0, y=0):
self.image_filename = image_filename
super().__init__(game_app, x, y)
def init_canvas_object(self):
self.photo_image = tk.PhotoImage(file=self.image_filename)
self.canvas_object_id = self.canvas.create_image(
self.x,
self.y,
image=self.photo_image)
class GameApp(ttk.Frame):
def __init__(self, parent, canvas_width=800, canvas_height=500, update_delay=33):
super().__init__(parent)
self.parent = parent
self.canvas_width = canvas_width
self.canvas_height = canvas_height
self.update_delay = update_delay
self.grid(sticky="news")
self.create_canvas()
self.elements = []
self.init_game()
self.is_stopped = False
self.parent.bind('<KeyPress>', self.on_key_pressed)
self.parent.bind('<KeyRelease>', self.on_key_released)
def create_canvas(self):
self.canvas = tk.Canvas(self, borderwidth=0,
width=self.canvas_width, height=self.canvas_height,
highlightthickness=0)
self.canvas.grid(sticky="news")
def stop_animation(self):
self.is_stopped = True
def resume_animation(self):
self.is_stopped = False
def animate(self):
if not self.is_stopped:
self.pre_update()
remaining_elements = []
for element in self.elements:
element.update()
element.render()
if element.to_be_deleted:
element.delete()
else:
remaining_elements.append(element)
self.elements = remaining_elements
self.post_update()
self.after(self.update_delay, self.animate)
def start(self):
self.after(0, self.animate)
def init_game(self):
pass
def pre_update(self):
pass
def post_update(self):
pass
def on_key_pressed(self, event):
pass
def on_key_released(self, event):
pass