forked from Freedom-3100/pythonProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
433 lines (366 loc) · 12.7 KB
/
project.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import pygame
from PIL import Image, ImageOps
from random import randint
import random
import Vawe
import Shoot
pygame.init()
class Node():
def __init__(self, v):
self.value = v
self.next = None
class List():
def __init__(self):
self.tail = None
self.root = None
def AddRoot(self, node):
node.next = self.root
if self.tail is None:
self.tail = node
self.root = node
def AddTail(self, node):
if self.tail is None:
self.root = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def RemoveRoot(self):
to_remove = self.root
self.root = self.root.next
if self.tail == to_remove:
self.tail = None
return to_remove
def At(self, index):
current = self.root
for i in range(index):
if current is None:
return None
current = current.next
return current
def RemoveIndex(self, index):
if index == 0:
return self.RemoveRoot()
current = self.At(index - 1)
to_remove = current.next
current.next = to_remove.next
if self.tail == to_remove:
self.tail = current
return to_remove
def RemoveTail(self):
if self.root == self.tail:
return self.RemoveRoot()
current = self.root
while current.next != self.tail:
current = current.next
to_remove = self.tail
current.next = None
self.tail = current
return to_remove
def Print(self):
current = self.root
while (current != None):
print(current.value)
current = current.next
# print(self.tail.value)
def Find(self, value):
current = self.root
while current.next.value != value:
if current == None:
return None
current = current.next
return current
def Remove(self, value):
if self.root.value == value:
return self.RemoveRoot()
current = self.root
while current.next.value != value:
current = current.next
to_remove = current.next
current.next = to_remove.next
if self.tail == to_remove:
self.tail = current
return to_remove
class Queue():
def __init__(self):
self.list = List()
def push(self, value):
node = Node(value)
self.list.AddTail(node)
def pop(self):
current = self.list.RemoveIndex(0)
return current.value
class Picture:
def __init__(self, PNG):
self.image = Image.open(PNG)
def rotate(self):
self.rotated_img = self.image.rotate(180)
self.rotated_img.save('rotated.png')
self.rotated_png = Image.open('rotated.png')
self.rotated_png.show()
def cut(self, x, y, hight, wigth):
self.new_picture = self.image.crop((x, y, hight, wigth))
self.new_picture.save('cut.png')
def show(self):
self.image.show()
def flip(self):
self.img = ImageOps.mirror(self.new_picture)
self.img.save('mirror.png')
def add_to_queue(self, value):
self.queue = Queue()
self.value = value
if self.value == "flip":
self.new_pictures = Picture(PNG)
self.new_pictures.cut(35, 0, 64, 40)
self.new_pictures.flip()
self.new_picture_new = Image.open('mirror.png')
self.new_picture_new.save('mirror_1.png')
self.queue.push('mirror_1.png')
self.new_pictures.cut(65, 0, 95, 40)
self.new_pictures.flip()
self.new_picture_new = Image.open('mirror.png')
self.new_picture_new.save('mirror_2.png')
self.queue.push('mirror_2.png')
self.new_pictures.cut(0, 0, 32, 40)
self.new_pictures.flip()
self.new_picture_new = Image.open('mirror.png')
self.new_picture_new.save('mirror_3.png')
self.queue.push('mirror_3.png')
return self.queue
elif self.value == "cut":
self.new_pictures = Picture(PNG)
self.new_pictures.cut(35, 0, 64, 40)
self.new_picture_new = Image.open('cut.png')
self.new_picture_new.save('cut_1.png')
self.queue.push('cut_1.png')
self.new_pictures.cut(65, 0, 94, 40)
self.new_picture_new = Image.open('cut.png')
self.new_picture_new.save('cut_2.png')
self.queue.push('cut_2.png')
self.new_pictures.cut(0, 0, 32, 40)
self.new_pictures.flip()
self.new_picture_new = Image.open('cut.png')
self.new_picture_new.save('cut_3.png')
self.queue.push('cut_3.png')
return self.queue
def go_queue(self, queue):
self.queue = queue
self.value = self.queue.pop()
self.queue.push(self.value)
return self.value
class Guns:
def __init__(self, PNG):
self.image = Image.open(PNG)
def draw(self, screen, angle, cord_x, cord_y, state):
self.state = state
self.screen = screen
self.angle = angle
self.gun = self.image.crop((0, 100, 55, 135))
self.gun.save('gun_1.png')
self.gun = Image.open('gun_1.png')
self.gun = ImageOps.mirror(self.gun)
self.gun.save('gun_1.png')
self.surf = pygame.image.load('gun_1.png').convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.guns = pygame.transform.rotate(self.surf, self.angle)
self.guns_rect = self.guns.get_rect(center=(cord_x, cord_y))
if self.state == 1:
self.screen.blit(self.guns, self.guns_rect)
def give_gun(self, pressed_key, state, angle):
self.angle = angle
self.state = state
if pressed_key[K_m]:
if self.state == 1:
self.state = 0
self.angle = 0
if pressed_key[K_n]:
if self.state == 0:
self.state = 1
return self.state, self.angle
class World:
def __init__(self, x, y):
self.x = x / 10
self.y = y / 10
self.y = int(self.y)
self.x = int(self.x)
self.white = (255, 255, 255)
self.red = (255, 0, 0)
self.surf_few = pygame.Surface((10, 10))
self.surf = pygame.Surface((10, 10))
self.pic = self.surf.get_rect(topleft=(self.x, self.y))
self.surf_few.fill(self.white)
self.surf.fill(self.red)
self.N = 6000
def revival(self,SCREEN_WIDTH,SCREEN_HIGHT):
self.cur = Vawe.wave_search(self.arr_world, (0, 3),SCREEN_WIDTH,SCREEN_HIGHT)
if self.cur == False:
while self.cur == False:
self.loc_built(SCREEN_WIDTH,SCREEN_HIGHT)
self.cur = Vawe.wave_search(self.arr_world, (0, 3),SCREEN_WIDTH,SCREEN_HIGHT)
return self.cur[0] * 10 , self.cur[1] * 10
else:
return self.cur[0] * 10 , self.cur[1] * 10
def update(self, cord_x, cord_y):
for i in range(len(self.arr_x)):
self.picture = self.surf.get_rect(topleft=(self.arr_x[i] * 10, self.arr_y[i] * 10))
if self.picture.collidepoint(cord_x, cord_y + 10):
cord_y -= 5
if self.picture.collidepoint(cord_x, cord_y - 10):
cord_y += 5
if self.picture.collidepoint(cord_x + 10, cord_y):
cord_x -= 5
if self.picture.collidepoint(cord_x - 10, cord_y):
cord_x += 5
return cord_x, cord_y
def loc_built(self, SCREEN_WIDTH, SCREEN_HEIGHT):
self.SCREEN_HEIGHT = SCREEN_HEIGHT / 10
self.SCREEN_WIDTH = SCREEN_WIDTH / 10
self.SCREEN_WIDTH = int(self.SCREEN_WIDTH)
self.SCREEN_HEIGHT = int(self.SCREEN_HEIGHT)
self.arr_world = []
self.arr_x = [self.x / 10]
self.arr_y = [self.y / 10]
for i in range(self.SCREEN_HEIGHT):
self.arr_world.append([0] * self.SCREEN_WIDTH)
for i in range(self.N):
self.arr_world[self.y][self.x] = 1
self.y += random.choice([-1, 1, 0])
self.x += random.choice([-1, 1, 0])
if self.x < 0:
self.x = 80 + self.x
if self.y < 0:
self.y = 60 + self.y
if self.x > 79:
self.x = 10
if self.y > 59:
self.y = 10
self.arr_x.append(self.x)
self.arr_y.append(self.y)
def draw(self, screen):
self.screen = screen
for i in range(self.SCREEN_HEIGHT):
for j in range(self.SCREEN_WIDTH):
if self.arr_world[i][j] == 1:
self.screen.blit(self.surf, (j * 10, i * 10))
class Player(pygame.sprite.Sprite):
def __init__(self, PNG):
self.queue = Queue()
self.state = 0
self.picture = Picture(PNG)
self.queue = self.picture.add_to_queue("flip")
self.queue_1 = self.picture.add_to_queue("cut")
super(Player, self).__init__()
self.surf = pygame.image.load('mirror.png').convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.worm = self.surf.get_rect()
def update(self, pressed_keys, x, y, angle):
self.x = x
self.y = y
self.angle = angle
if pressed_keys[K_UP]:
self.worm.move_ip(0, -5)
self.y -= 5
if pressed_keys[K_DOWN]:
self.worm.move_ip(0, 5)
self.y += 5
if pressed_keys[K_LEFT]:
self.worm.move_ip(-5, 0)
self.x -= 5
if pressed_keys[K_RIGHT]:
self.worm.move_ip(5, 0)
self.x += 5
if pressed_keys[K_e]:
if self.angle >= 270:
self.angle -= 5
self.angle += 5
if pressed_keys[K_r]:
if self.angle <= -90:
self.angle += 5
self.angle -= 5
if self.worm.left < 0:
self.worm.left = 0
self.x = 15
if self.worm.right > SCREEN_WIDTH:
self.worm.right = SCREEN_WIDTH
self.x = SCREEN_WIDTH - 15
if self.worm.top < 0:
self.worm.top = 0
self.y = 18
if self.worm.bottom >= SCREEN_HEIGHT:
self.worm.bottom = SCREEN_HEIGHT
self.y = SCREEN_HEIGHT - 18
return self.x, self.y, self.angle
def move(self, pressed_keys):
if pressed_keys[K_RIGHT]:
self.png = self.picture.go_queue(self.queue)
self.surf = pygame.image.load(self.png).convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.worm = self.surf.get_rect(center=(self.x, self.y))
self.state = 0
elif pressed_keys[K_LEFT]:
self.png = self.picture.go_queue(self.queue_1)
self.surf = pygame.image.load(self.png).convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.worm = self.surf.get_rect(center=(self.x, self.y))
self.state = 1
else:
if self.state == 0:
self.surf = pygame.image.load('mirror.png').convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
elif self.state == 1:
self.surf = pygame.image.load('cut.png').convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.worm = self.surf.get_rect(center=(self.x, self.y))
return
class Enemy:
def __init__(self):
pass
from pygame.locals import (
RLEACCEL,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
K_e,
K_r,
K_m,
K_n,
QUIT,
)
pygame.init()
FPS = 15 # число кадров в секунду
clock = pygame.time.Clock()
x = 100
y = 100
state = 0
angle = 0
PNG = 'worms.png'
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
player = Player(PNG)
world = World(x, y)
world.loc_built(SCREEN_WIDTH, SCREEN_HEIGHT)
X, Y = world.revival(SCREEN_WIDTH,SCREEN_HEIGHT)
Guns = Guns(PNG)
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
pressed_keys = pygame.key.get_pressed()
X, Y, angle = player.update(pressed_keys, X, Y, angle)
player.move(pressed_keys)
screen.fill((135, 206, 250))
state, angle = Guns.give_gun(pressed_keys, state, angle)
screen.blit(player.surf, player.worm)
Guns.draw(screen, angle, X, Y, state)
world.draw(screen)
X, Y = world.update(X, Y)
pygame.display.flip()
clock.tick(FPS)