-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
345 lines (272 loc) · 12.5 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import tkinter
from PIL import ImageTk
from PIL import Image
from tkinter import messagebox
from random import *
from copy import deepcopy
import time
#importing some mechanics
import Player
import Enemy
import Tile
import Weapon
from entity import *
#подключаем музычку, но позже
main_field = Main_field()
count_of_moves = 0
dead = []
#создаем "список живых существ"
height_f, width_f, cellsize = [16, 12, 64]
dodge_dist = cellsize
#height_f, width_f, cellsize = [3, 3, 64]
#тут читаем поле
field = []
with open("polygon.txt", "r") as f:
for line in f:
field.append(list(line.strip()))
entity_id_to_id = {}
entities_to_obj = {}
#а тут задаем параметры ГГ (кодовое имя --- Курточка)
main_field.add_entity('player',entity_id='player',entity_pos = [cellsize*4,cellsize*4])
entity_id_to_id['player'] = 0
Jacket = Player.player()
Jacket.x, Jacket.y = main_field.get_list_ent()[0][1][0] + cellsize * 3 // 2, main_field.get_list_ent()[0][1][1] + cellsize * 3 // 2
Jacket.speed = main_field.get_list_ent()[0][0].max_move_speed
Jacket.sprite = os.getcwd() + '/sprites/player.png'
#пилим окно
root = tkinter.Tk()
canvas = tkinter.Canvas(root, width = cellsize * height_f, height = cellsize * width_f)
#создаем спрайты
Jacketsprite = ImageTk.PhotoImage(Image.open(Jacket.sprite))
Mansprite = ImageTk.PhotoImage(Image.open(os.getcwd() + '/sprites/enemy_very_pink.png'))
Attacksprite = ImageTk.PhotoImage(Image.open(os.getcwd() + '/sprites/atack.png'))
imgwall = ImageTk.PhotoImage(Image.open(os.getcwd() + '/sprites/wall1.png'))
imgfloor = ImageTk.PhotoImage(Image.open(os.getcwd() + '/sprites/floor3.png'))
#генерируем текстуры поля
for j in range(height_f):
for i in range(width_f):
if field[i][j] == '#':
canvas.create_image(j * cellsize, i * cellsize, anchor = "nw", image=imgwall)
elif field[i][j] == '.':
canvas.create_image(j * cellsize, i * cellsize, anchor = "nw", image=imgfloor)
#создаем тело персонажа
entities_to_obj['player'] = canvas.create_image(Jacket.x, Jacket.y, image=Jacketsprite)
def create_enemy(pos = [1024//2,768//2]):
global entity_id_to_id
global entities_to_obj
global canvas
global Mansprite
enemy_name = 'enemy_'+str(len(main_field.get_list_ent()))
main_field.add_entity('enemy',entity_id=enemy_name,entity_pos=pos)
entity_id_to_id[enemy_name] = 1
Man = Player.player()
Man.x, Man.y = main_field.get_list_ent()[len(main_field.get_list_ent())-1][1][0] + cellsize * 3 // 2, main_field.get_list_ent()[len(main_field.get_list_ent())-1][1][1] + cellsize * 3 // 2
Man.speed = main_field.get_list_ent()[0][0].max_move_speed
entities_to_obj[enemy_name] = canvas.create_image(Man.x, Man.y, image=Mansprite)
create_enemy()
## здесь скоро будут бинды
## а сейчас здесь только танцы с бубном
# (c) <--- бубен
# O/
# /V
# |
# / \
# *танцует*
# а вот и бинды
#warning: из-за того, что я ниоч понимаю принцип работы ткинтера, все будет костыльно
def char_move_left(x, whom):
global entities_to_obj
old_pos = deepcopy(main_field.get_list_ent()[entity_id_to_id[whom]][1])
main_field.move_left(entity_id_to_id[whom])
new_pos = main_field.get_list_ent()[entity_id_to_id[whom]][1]
#Jacket.move_up()
canvas.move(entities_to_obj[whom], new_pos[0]-old_pos[0], new_pos[1]-old_pos[1])
canvas.update()
def char_move_down(x, whom):
global entities_to_obj
old_pos = deepcopy(main_field.get_list_ent()[entity_id_to_id[whom]][1])
main_field.move_down(entity_id_to_id[whom])
new_pos = main_field.get_list_ent()[entity_id_to_id[whom]][1]
#Jacket.move_up()
canvas.move(entities_to_obj[whom], new_pos[0]-old_pos[0], new_pos[1]-old_pos[1])
canvas.update()
def char_move_right(x, whom):
global entities_to_obj
old_pos = deepcopy(main_field.get_list_ent()[entity_id_to_id[whom]][1])
main_field.move_right(entity_id_to_id[whom])
new_pos = main_field.get_list_ent()[entity_id_to_id[whom]][1]
#Jacket.move_up()
canvas.move(entities_to_obj[whom], new_pos[0]-old_pos[0], new_pos[1]-old_pos[1])
canvas.update()
def char_move_up(x, whom):
global entities_to_obj
old_pos = deepcopy(main_field.get_list_ent()[entity_id_to_id[whom]][1])
main_field.move_up(entity_id_to_id[whom])
new_pos = main_field.get_list_ent()[entity_id_to_id[whom]][1]
#Jacket.move_up()
canvas.move(entities_to_obj[whom], new_pos[0]-old_pos[0], new_pos[1]-old_pos[1])
canvas.update()
#рандомный кувырок. все как вы любите --- сплошные костыли
def char_random_dodge(whom, x):
for _ in range(x):
act = randint(0, 3)
if act == 0:
char_move_up(dodge_dist, whom)
elif act == 1:
char_move_down(dodge_dist, whom)
elif act == 2:
char_move_left(dodge_dist, whom)
elif act == 3:
char_move_right(dodge_dist, whom)
killed = []
score = 0
txt = canvas.create_text((height_f - 2) * cellsize, cellsize // 2, text = "Score: " + str(score), font = "Verdana 24", justify = tkinter.CENTER, fill = "black")
#атака
def char_attack(whom):
global killed
global score, txt
old_pos = (main_field.get_list_ent()[entity_id_to_id[whom]][1][0] + cellsize * 3 // 2, main_field.get_list_ent()[entity_id_to_id[whom]][1][1] + cellsize * 3 // 2)
r = main_field.get_list_ent()[0][0].atack_range
atck_gui = canvas.create_image(old_pos[0], old_pos[1], image=Attacksprite)
if whom == 'player':
sprite = Jacketsprite
else:
sprite = Mansprite
char_copy = canvas.create_image(old_pos[0], old_pos[1], image=sprite)
died_list = main_field.atack(entity_id_to_id[whom])
canvas.update()
#print(died_list)
time.sleep(0.125)
canvas.delete(atck_gui)
canvas.delete(char_copy)
if died_list is not None:
for dead in died_list:
canvas.delete(entities_to_obj[dead])
killed.append(dead)
if 'player' in died_list:
#endpic = ImageTk.PhotoImage(Image.open(os.getcwd() + '/sprites/gameover.png'))
#canvas.create_image(0, 0, image=endpic)
messagebox.showinfo('', 'You died')
root.withdraw()
for _ in range(200):
print("YOU DIED")
exit()
score += 1
canvas.delete(txt)
txt = canvas.create_text((height_f - 2) * cellsize, cellsize // 2, text = "Score: " + str(score), font = "Verdana 24", justify = tkinter.CENTER, fill = "black")
#тут отзываемся на нажатия клавиш
def callback(event):
global count_of_moves
global killed
dead = killed
if event.char == "w":
char_move_up(Jacket.speed, 'player')
time.sleep(0.0625)
list_of_ent = main_field.get_list_ent()
for key in entity_id_to_id.keys():
if entity_id_to_id[key] != 0 and (key not in dead):
result = list_of_ent[entity_id_to_id[key]][0].move_to_player(list_of_ent[0][1],list_of_ent[0][0].hit_box_range,list_of_ent[entity_id_to_id[key]][1])
if result == 'up':
char_move_up(None,key)
if result == 'down':
char_move_down(None,key)
if result == 'left':
char_move_left(None,key)
if result == 'right':
char_move_right(None,key)
if result == 'atack':
char_attack(key)
if event.char == "a":
char_move_left(Jacket.speed, 'player')
time.sleep(0.0625)
list_of_ent = main_field.get_list_ent()
for key in entity_id_to_id.keys():
if entity_id_to_id[key] != 0 and (key not in dead):
result = list_of_ent[entity_id_to_id[key]][0].move_to_player(list_of_ent[0][1],list_of_ent[0][0].hit_box_range,list_of_ent[entity_id_to_id[key]][1])
if result == 'up':
char_move_up(None,key)
if result == 'down':
char_move_down(None,key)
if result == 'left':
char_move_left(None,key)
if result == 'right':
char_move_right(None,key)
if result == 'atack':
char_attack(key)
if event.char == "s":
char_move_down(Jacket.speed, 'player')
time.sleep(0.0625)
list_of_ent = main_field.get_list_ent()
for key in entity_id_to_id.keys():
if entity_id_to_id[key] != 0 and (key not in dead):
result = list_of_ent[entity_id_to_id[key]][0].move_to_player(list_of_ent[0][1],list_of_ent[0][0].hit_box_range,list_of_ent[entity_id_to_id[key]][1])
if result == 'up':
char_move_up(None,key)
if result == 'down':
char_move_down(None,key)
if result == 'left':
char_move_left(None,key)
if result == 'right':
char_move_right(None,key)
if result == 'atack':
char_attack(key)
if event.char == "d":
char_move_right(Jacket.speed, 'player')
time.sleep(0.0625)
list_of_ent = main_field.get_list_ent()
for key in entity_id_to_id.keys():
if entity_id_to_id[key] != 0 and (key not in dead):
result = list_of_ent[entity_id_to_id[key]][0].move_to_player(list_of_ent[0][1],list_of_ent[0][0].hit_box_range,list_of_ent[entity_id_to_id[key]][1])
if result == 'up':
char_move_up(None,key)
if result == 'down':
char_move_down(None,key)
if result == 'left':
char_move_left(None,key)
if result == 'right':
char_move_right(None,key)
if result == 'atack':
char_attack(key)
if event.char == "f":
char_random_dodge('player', 2)
list_of_ent = main_field.get_list_ent()
for key in entity_id_to_id.keys():
if entity_id_to_id[key] != 0 and (key not in dead):
result = list_of_ent[entity_id_to_id[key]][0].move_to_player(list_of_ent[0][1],list_of_ent[0][0].hit_box_range,list_of_ent[entity_id_to_id[key]][1])
if result == 'up':
char_move_up(None,key)
if result == 'down':
char_move_down(None,key)
if result == 'left':
char_move_left(None,key)
if result == 'right':
char_move_right(None,key)
if result == 'atack':
char_attack(key)
if event.char == " ":
char_attack('player')
list_of_ent = main_field.get_list_ent()
for key in entity_id_to_id.keys():
if entity_id_to_id[key] != 0 and (key not in dead):
result = list_of_ent[entity_id_to_id[key]][0].move_to_player(list_of_ent[0][1],list_of_ent[0][0].hit_box_range,list_of_ent[entity_id_to_id[key]][1])
if result == 'up':
char_move_up(None,key)
if result == 'down':
char_move_down(None,key)
if result == 'left':
char_move_left(None,key)
if result == 'right':
char_move_right(None,key)
if result == 'atack':
char_attack(key)
name = [' ','f','w','s','a','d']
if event.char in name:
if count_of_moves//10 < (count_of_moves+1)//10:
create_enemy()
count_of_moves+=1
else:
count_of_moves+=1
root.bind("<Key>", callback)
canvas.pack()
root.mainloop()