forked from TheGhost8/CollaborativeDevelopment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
270 lines (224 loc) · 11.4 KB
/
interface.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""head module - interface."""
from tkinter import *
import tkinter as tk
from tkinter import messagebox
from map import Map
from clock import Clock
class Application(tk.Tk):
"""Class conteined all."""
def __init__(self, master=None, title="Paper.io", **kwargs):
"""Create class."""
self.field = Map()
super().__init__(master, **kwargs)
self.frame = tk.Frame(self)
self.frame.pack()
self.restart = tk.Button(self.frame, text="New game", command=self.new_game, width=20, height=2).pack(side=tk.LEFT)
self.language = tk.Button(self.frame, text="Change language", command=self.change_languege, width=19, height=2).pack(side=tk.LEFT)
self.info = tk.Button(self.frame, text="Information", command=self.text_info, width=19, height=2).pack(side=tk.LEFT)
self.exit = tk.Button(self.frame, text="Exit", command=self.quit, width=20, height=2).pack(side=tk.LEFT)
self.clock = Clock(self.frame).pack(side=tk.LEFT)
self.buttons = [False for i in range(9)]
# self.bind("<KeyPress>", self.keydown)
self.click()
self.window = tk.Frame(self)
self.window.pack()
self.canv = tk.Canvas(self.window, bg='#fff', width=800, height=600)
self.canv.pack(side=tk.LEFT)
self.new_game()
def click(self):
"""Click some buttons."""
self.bind_all('<KeyPress-p>', self.__p_handler)
self.bind_all('<KeyPress-i>', self.text_info)
self.bind_all('<KeyPress-n>', self.new_game)
self.bind_all('<KeyPress-c>', self.change_languege)
self.bind_all('<KeyPress-Escape>', self.escape_handler)
self.bind_all('<KeyPress-a>', self.__a_handler)
self.bind_all('<KeyPress-w>', self.__w_handler)
self.bind_all('<KeyPress-s>', self.__s_handler)
self.bind_all('<KeyPress-d>', self.__d_handler)
self.bind_all('<KeyPress-Left>', self.__arrow_left_handler)
self.bind_all('<KeyPress-Up>', self.__arrow_up_handler)
self.bind_all('<KeyPress-Down>', self.__arrow_down_handler)
self.bind_all('<KeyPress-Right>', self.__arrow_right_handler)
self.bind_all('<KeyRelease-a>', self.__a_release)
self.bind_all('<KeyRelease-w>', self.__w_release)
self.bind_all('<KeyRelease-s>', self.__s_release)
self.bind_all('<KeyRelease-d>', self.__d_release)
self.bind_all('<KeyRelease-Left>', self.__arrow_left_release)
self.bind_all('<KeyRelease-Up>', self.__arrow_up_release)
self.bind_all('<KeyRelease-Down>', self.__arrow_down_release)
self.bind_all('<KeyRelease-Right>', self.__arrow_right_release)
def change_languege(self, event=None):
"""Change languege."""
pass
def new_game(self, event=None):
"""New."""
self.field = Map()
self.draw_everything()
def draw_everything(self):
"""Draw everything."""
self.canv.delete('all')
for i in self.field.get_tiles_player1():
self.canv.create_rectangle(i[0] * self.field.player1.player_size(), i[1] * self.field.player1.player_size(), i[0] * self.field.player1.player_size() + self.field.player1.player_size(), i[1] * self.field.player1.player_size() + self.field.player1.player_size(), fill=self.field.player1.get_color(), outline=self.field.player1.get_color())
for i in self.field.get_tiles_player2():
self.canv.create_rectangle(i[0] * self.field.player2.player_size(), i[1] * self.field.player2.player_size(), i[0] * self.field.player2.player_size() + self.field.player2.player_size(), i[1] * self.field.player2.player_size() + self.field.player2.player_size(), fill=self.field.player2.get_color(), outline=self.field.player2.get_color())
for i in self.field.get_potential_tiles_player1():
self.canv.create_rectangle(i[0] * self.field.player1.player_size(), i[1] * self.field.player1.player_size(), i[0] * self.field.player1.player_size() + self.field.player1.player_size(), i[1] * self.field.player1.player_size() + self.field.player1.player_size(), fill=self.field.player1.get_transparent_color(), outline=self.field.player1.get_transparent_color())
for i in self.field.get_potential_tiles_player2():
self.canv.create_rectangle(i[0] * self.field.player2.player_size(), i[1] * self.field.player2.player_size(), i[0] * self.field.player2.player_size() + self.field.player2.player_size(), i[1] * self.field.player2.player_size() + self.field.player2.player_size(), fill=self.field.player2.get_transparent_color(), outline=self.field.player2.get_transparent_color())
self.canv.create_rectangle(self.field.player1.get_x(), self.field.player1.get_y(), self.field.player1.get_x() + self.field.player1.player_size(), self.field.player1.get_y() + self.field.player1.player_size(), fill=self.field.player1.get_color())
self.canv.create_rectangle(self.field.player2.get_x(), self.field.player2.get_y(), self.field.player2.get_x() + self.field.player2.player_size(), self.field.player2.get_y() + self.field.player2.player_size(), fill=self.field.player2.get_color())
def escape_handler(self, event):
"""Escape handler."""
self.quit()
def __p_handler(self, event):
"""Pause control."""
self.buttons[8] = not self.buttons[8]
self.check_buttons()
def __a_handler(self, event):
"""A_handler."""
self.buttons[0] = True
self.check_buttons()
def __w_handler(self, event):
"""W handler."""
self.buttons[1] = True
self.check_buttons()
def __s_handler(self, event):
"""S handler."""
self.buttons[2] = True
self.check_buttons()
def __d_handler(self, event):
"""D handler."""
self.buttons[3] = True
self.check_buttons()
def __arrow_left_handler(self, event):
"""Left handler."""
self.buttons[4] = True
self.check_buttons()
def __arrow_up_handler(self, event):
"""Up handler."""
self.buttons[5] = True
self.check_buttons()
def __arrow_down_handler(self, event):
"""Down handler."""
self.buttons[6] = True
self.check_buttons()
def __arrow_right_handler(self, event):
"""Right handler."""
self.buttons[7] = True
self.check_buttons()
def __a_release(self, event):
"""A_release."""
self.buttons[0] = False
self.check_buttons()
def __w_release(self, event):
"""W release."""
self.buttons[1] = False
self.check_buttons()
def __s_release(self, event):
"""S release."""
self.buttons[2] = False
self.check_buttons()
def __d_release(self, event):
"""D release."""
self.buttons[3] = False
self.check_buttons()
def __arrow_left_release(self, event):
"""Left release."""
self.buttons[4] = False
self.check_buttons()
def __arrow_up_release(self, event):
"""Up release."""
self.buttons[5] = False
self.check_buttons()
def __arrow_down_release(self, event):
"""Down release."""
self.buttons[6] = False
self.check_buttons()
def __arrow_right_release(self, event):
"""Right release."""
self.buttons[7] = False
self.check_buttons()
def check_buttons(self):
"""Check buttons."""
# print(self.buttons)
if not self.buttons[8]:
if self.buttons[0]:
self.a_press()
if self.buttons[1]:
self.w_press()
if self.buttons[2]:
self.s_press()
if self.buttons[3]:
self.d_press()
if self.buttons[4]:
self.left_press()
if self.buttons[5]:
self.up_press()
if self.buttons[6]:
self.down_press()
if self.buttons[7]:
self.right_press()
def a_press(self):
"""Press A."""
self.field.process_movement('A')
if (self.field.player1.check_win()):
self.won()
self.draw_everything()
def w_press(self):
"""Press W."""
self.field.process_movement('W')
if (self.field.player1.check_win()):
self.won()
self.draw_everything()
def s_press(self):
"""Press S."""
self.field.process_movement('S')
if (self.field.player1.check_win()):
self.won()
self.draw_everything()
def d_press(self):
"""Press D."""
self.field.process_movement('D')
if (self.field.player1.check_win()):
self.won()
self.draw_everything()
def left_press(self):
"""Press left."""
self.field.process_movement('ARROW_LEFT')
if (self.field.player2.check_win()):
self.lose()
self.draw_everything()
def up_press(self):
"""Press Up."""
self.field.process_movement('ARROW_UP')
if (self.field.player2.check_win()):
self.lose()
self.draw_everything()
def down_press(self):
"""Press down."""
self.field.process_movement('ARROW_DOWN')
if (self.field.player2.check_win()):
self.lose()
self.draw_everything()
def right_press(self):
"""Press right."""
self.field.process_movement('ARROW_RIGHT')
if (self.field.player2.check_win()):
self.lose()
self.draw_everything()
def text_info(self, event=None):
"""Text info."""
messagebox.showinfo('information', 'Игра для двоих пользователей.\nЕсть два игрока\n - небольшие квадратики разного цвета с выделенным ободком.\n Каждый игрок может двигаться влево/вправо/вверх/вниз и наискосок. Когда игрок идёт по нейтральной территории, за ним тянется полупрозрачный след из его цвета\nИгровые кнопки:\n "Esc" - выход из игры\n "p" - пауза\n "i" - показать окно информации\n "n" - новая игра\n Управление игроками:\n Первый игрок: "w", "a", "s", "d"\n Второй игрок: стрелочки))\n\n Правила игры - на игровом поле перемещать своего персонажа и захватывать территории\nпутем закрашивания пройденного следа персонажем и возможности закрашивать\nв свой цвет области, обведенные уже закрашенными областями поля\nЕсли до того, как игрок вернулся на свою закращенную область, соперник перешёл через его полупрозрачный след (разорвал связь игрока с его закрашенной областью), то игрок проигрывает - побеждает соперник.\n Цель игры - уничтожить соперника')
def won(self):
"""Win game."""
messagebox.showinfo('End_game', 'First player win!')
self.new_game()
def lose(self):
"""Lose game."""
messagebox.showinfo('End_game', 'Second player win!')
self.new_game()
app = Application()
app.title('paper.io')
app.resizable(width=False, height=False)
app.mainloop()