-
Notifications
You must be signed in to change notification settings - Fork 1
/
items.py
101 lines (77 loc) · 2.72 KB
/
items.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
from functools import cached_property
from time import time
import settings as s
from fallbacks import pygame
class Item(object):
def __init__(self):
pass
def avatar(self):
raise NotImplementedError()
def render(self, screen, x, y):
screen.blit(self.avatar, (x, y))
def get_state(self) -> tuple:
raise NotImplementedError()
class Coin(Item):
avatar = pygame.image.load('assets/coin.png')
def __init__(self, pos, collectable=False):
super(Coin, self).__init__()
self.x = pos[0]
self.y = pos[1]
self.collectable = collectable
def get_state(self):
return self.x, self.y
class Bomb(Item):
DEFAULT_AVATARS = {color: pygame.image.load(f'assets/bomb_{color}.png') for color in s.AGENT_COLORS}
def __init__(self, pos, owner, timer, power, color, custom_sprite=None):
super(Bomb, self).__init__()
self.x = pos[0]
self.y = pos[1]
self.owner = owner
self.timer = timer
self.power = power
self.active = True
self.color = color
self.custom_sprite = custom_sprite
@cached_property
def avatar(self):
if self.custom_sprite:
return self.custom_sprite
return Bomb.DEFAULT_AVATARS[self.color]
def get_state(self):
return (self.x, self.y), self.timer
def get_blast_coords(self, arena):
x, y = self.x, self.y
blast_coords = [(x, y)]
for i in range(1, self.power + 1):
if arena[x + i, y] == -1:
break
blast_coords.append((x + i, y))
for i in range(1, self.power + 1):
if arena[x - i, y] == -1:
break
blast_coords.append((x - i, y))
for i in range(1, self.power + 1):
if arena[x, y + i] == -1:
break
blast_coords.append((x, y + i))
for i in range(1, self.power + 1):
if arena[x, y - i] == -1:
break
blast_coords.append((x, y - i))
return blast_coords
class Explosion(Item):
STAGES = [pygame.image.load(f'assets/explosion_{i}.png') for i in range(6)]
def __init__(self, blast_coords, screen_coords, owner, timer):
super().__init__()
self.blast_coords = blast_coords
self.screen_coords = screen_coords
self.owner = owner
self.timer = timer
self.active = True
self.stages = Explosion.STAGES
def render(self, screen, **kwargs):
img = pygame.transform.rotate(self.stages[self.timer], (-50 * time()) % 360)
rect = img.get_rect()
for (x, y) in self.screen_coords:
rect.center = x + 15, y + 15
screen.blit(img, rect.topleft)