This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
256 lines (218 loc) · 8.34 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import tkinter as tk
class Direction:
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
class Cell:
SPEED = 0.02
def __init__(self, player_id, x, y, direction=Direction.UP):
self.player_id = player_id
self.x = x
self.y = y
self.completion = 0
self.direction = direction
def is_completed(self):
return self.completion >= 1
def step(self):
if self.is_completed():
self.x = round(self.x)
self.y = round(self.y)
return True
if self.direction == Direction.UP:
self.y -= self.SPEED
elif self.direction == Direction.RIGHT:
self.x += self.SPEED
elif self.direction == Direction.DOWN:
self.y += self.SPEED
else:
self.x -= self.SPEED
self.completion += self.SPEED
self.x = round(self.x, 6)
self.y = round(self.y, 6)
return False
class StaticCell:
def __init__(self, player_id, value=0):
self.player_id = player_id
self.value = value
class Grid:
def __init__(self, size):
self.size = size
self.grid = [None] * (size ** 2)
def put(self, player_id, x, y):
cell = self.get(x, y)
if not cell:
self.set(x, y, StaticCell(player_id))
return []
if cell.value < 2:
self.set(x, y, StaticCell(player_id, cell.value + 1))
return []
# split
self.set(x, y, None)
animation_list = []
if y > 0:
animation_list.append(Cell(player_id, x, y, Direction.UP))
if x < (self.size - 1):
animation_list.append(Cell(player_id, x, y, Direction.RIGHT))
if y < (self.size - 1):
animation_list.append(Cell(player_id, x, y, Direction.DOWN))
if x > 0:
animation_list.append(Cell(player_id, x, y, Direction.LEFT))
return animation_list
def apply(self, animation_list):
new_animations = []
for anim in animation_list:
if anim.is_completed():
new_animations += self.put(anim.player_id, anim.x, anim.y)
return new_animations
def get(self, x, y):
return self.grid[int(x) + int(y) * self.size]
def get_winner(self):
i = 0
while i < len(self.grid) and (not self.grid[i]):
i += 1
if i >= len(self.grid):
return None
winner_id = self.grid[i].player_id
while i < len(self.grid) and (not self.grid[i] or self.grid[i].player_id == winner_id):
i += 1
if i < len(self.grid):
return None
return winner_id
def set(self, x, y, value):
self.grid[int(x) + int(y) * self.size] = value
class Game(tk.Frame):
SIZE = 600
MAP_SIZE = 5
CELL_SIZE_PERCENT = 0.86
PLAYER_SIZE_PERCENT = 0.80
DOT_SIZE_PERCENT = 0.16
def __init__(self, master):
super(Game, self).__init__(master)
self.width = self.height = self.SIZE
self.canvas = tk.Canvas(self, bg="#dddddd", width=self.width, height=self.height)
self.canvas.pack()
self.pack()
self.setup_game()
self.draw_map()
self.draw_static()
self.canvas.focus_set()
self.canvas.bind("<Button-1>", self.mouse_click)
def setup_metricks(self):
self.slot_size = self.SIZE / self.MAP_SIZE
self.cell_size = self.slot_size * self.CELL_SIZE_PERCENT
self.cell_space = (self.slot_size - self.cell_size) / 2
self.player_size = self.cell_size * self.PLAYER_SIZE_PERCENT
self.dot_rad = self.player_size * self.DOT_SIZE_PERCENT / 2
def setup_game(self):
self.map = Grid(self.MAP_SIZE)
self.setup_metricks()
# manually set players
self.map.set(1, 1, StaticCell(0))
self.map.set(self.MAP_SIZE - 2, self.MAP_SIZE - 2, StaticCell(1))
self.player_turn = 0
self.animations = []
def mouse_click(self, event):
if self.animations:
return
x = event.x // self.slot_size
xrem = event.x % self.slot_size
y = event.y // self.slot_size
yrem = event.y % self.slot_size
# check for slot-space zone
if xrem < self.cell_space or xrem > self.cell_size or \
yrem < self.cell_space or yrem > self.cell_size:
return
cell = self.map.get(x, y)
if not cell or cell.player_id != self.player_turn:
return
self.animations = self.map.put(self.player_turn, x, y)
self.player_turn = (self.player_turn + 1) % 2
self.game_loop()
def game_loop(self):
flag = False
for anim in self.animations:
flag |= anim.step()
# if anims and they're done
if flag:
self.animations = self.map.apply(self.animations)
# update screen
self.draw()
# check for wins
if not self.animations:
winner = self.map.get_winner()
if winner == 0:
self.draw_text(300, 200, "Player 1 wins")
self.canvas.unbind("<Button-1>")
elif winner == 1:
self.draw_text(300, 200, "Player 2 wins")
self.canvas.unbind("<Button-1>")
else:
self.after(10, self.game_loop)
def draw(self):
self.canvas.delete("tmp")
self.draw_static()
self.draw_moving()
def draw_map(self):
for y in range(self.MAP_SIZE):
for x in range(self.MAP_SIZE):
xx = x * self.slot_size + self.cell_space
yy = y * self.slot_size + self.cell_space
self.canvas.create_rectangle(xx, yy, xx + self.cell_size, yy + self.cell_size, fill="#aaaaaa")
def draw_static(self):
# draw static cells
for y in range(self.MAP_SIZE):
for x in range(self.MAP_SIZE):
cell = self.map.get(x, y)
if not cell:
continue
xx = x * self.slot_size + self.cell_space
yy = y * self.slot_size + self.cell_space
color = "#ee1010"
if cell.player_id == 1:
color = "#1010ee"
self.draw_player(xx, yy, color, cell.value)
def draw_player(self, x, y, color, value=0):
# args - coords of the cell (not slot)
player_space = (self.cell_size - self.player_size) / 2
xx = x + player_space
yy = y + player_space
self.canvas.create_rectangle(xx, yy, xx + self.player_size, yy + self.player_size, fill=color, tags="tmp")
# dots
if value == 0:
# center
self.draw_circle(xx + self.player_size / 2, yy + self.player_size / 2, self.dot_rad)
elif value == 1:
dot_space = (self.player_size - self.dot_rad * 4) / 3
# left
self.draw_circle(xx + dot_space + self.dot_rad, yy + self.player_size / 2, self.dot_rad)
# right
self.draw_circle(xx + dot_space * 2 + self.dot_rad * 3, yy + self.player_size / 2, self.dot_rad)
else:
dot_space = (self.player_size - self.dot_rad * 4) / 3
# up
self.draw_circle(xx + self.player_size / 2, yy + dot_space + self.dot_rad, self.dot_rad)
# left
self.draw_circle(xx + dot_space + self.dot_rad, yy + self.player_size - dot_space - self.dot_rad, self.dot_rad)
# right
self.draw_circle(xx + self.player_size - dot_space - self.dot_rad, yy + self.player_size - dot_space - self.dot_rad, self.dot_rad)
def draw_circle(self, x, y, rad, fill="#eeeeee"):
self.canvas.create_oval(x - rad, y - rad, x + rad, y + rad, fill=fill, tags="tmp")
def draw_text(self, x, y, text, size="40"):
font = ('Forte', size)
return self.canvas.create_text(x, y, text=text, font=font, fill="#10ee10")
def draw_moving(self):
for anim in self.animations:
x = anim.x * self.slot_size + self.cell_space
y = anim.y * self.slot_size + self.cell_space
color = "#ee1010"
if anim.player_id == 1:
color = "#1010ee"
self.draw_player(x, y, color)
def main():
root = tk.Tk()
root.title("Clonium")
game = Game(root)
game.mainloop()
if __name__ == "__main__":
main()