Newer
Older
class Enemy:
def __init__(self, text, x, y):
self.x = x
self.y = y
self.text = text
self.matched = 0
def draw(self):
txt = font.render(self.text[0:self.matched], True, (255, 0, 0))
txtRest = font.render(self.text[self.matched:], True, (255, 255, 255))
width = txt.get_width() + txtRest.get_width()
screen.blit(txt, (self.x - width / 2, self.y))
screen.blit(txtRest, (self.x - width / 2 + txt.get_width(), self.y))
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
self.y += 1
if self.y > height:
objects.remove(self)
def typed(self, key):
if not self.text:
return
if self.text[self.matched] != key:
self.matched = 0
if self.text[self.matched] == key:
self.matched += 1
if self.matched == len(self.text):
self.text = ""
shoot(self, self.x, self.y)
return True
def hit(self):
objects.remove(self)
explode(self.x, self.y, (255, 0, 0), 30)
class Fighter(Enemy):
def __init__(self, text, x, y):
super().__init__(text, x, y)
def draw(self):
if self.y < 0:
return
super().draw()
pygame.draw.lines(screen, (0, 255, 0), True,
((self.x, self.y),
(self.x + 10, self.y - 20),
(self.x - 10, self.y - 20),
))
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
class Shot(Enemy):
def __init__(self, text, x, y, dx, dy):
super().__init__(text, x, y)
self.dx = dx
self.dy = dy
self.v = sqrt(self.dx * self.dx + self.dy * self.dy)
def update(self, tick):
self.x += self.dx
self.y += self.dy
px = playerX - self.x
py = playerY - self.y
pv = sqrt(px * px + py * py)
if pv < 20:
objects.remove(self)
self.dx = 0.95 * self.dx + 0.05 * px / pv * self.v
self.dy = 0.95 * self.dy + 0.05 * py / pv * self.v
if self.y > height:
objects.remove(self)
def draw(self):
super().draw()
pygame.draw.lines(screen, (255, 255, 0), True,
((self.x - 5, self.y),
(self.x + 5, self.y),
(self.x, self.y - 5),
(self.x, self.y + 5)
))
class Shooter(Enemy):
def __init__(self, text, x, y, shots):
super().__init__(text, x, y)
self.shots = shots
def draw(self):
if self.y < 0:
return
super().draw()
pygame.draw.lines(screen, (0, 255, 0), True,
((self.x, self.y),
(self.x + 10, self.y - 20),
(self.x + 4, self.y - 20),
(self.x, self.y),
(self.x - 4, self.y - 20),
(self.x - 10, self.y - 20),
))
def update(self, tick):
super().update(tick)
if tick % 50 == 49:
objects.append(Shot(choice(self.shots), self.x, self.y, -2 + random() * 4, -2 + random() * 4))
class Boss(Enemy):
def __init__(self, text, x, y):
super().__init__(text, x, y)
def draw(self):
if self.y < 0:
return
super().draw()
pygame.draw.lines(screen, (0, 255, 0), True,
((self.x - 20, self.y - 20),
(self.x - 10, self.y),
(self.x , self.y - 20),
(self.x + 10, self.y),
(self.x + 20, self.y - 20),
))