-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsprites.py
235 lines (198 loc) · 7.34 KB
/
sprites.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
import time
import random
import pygame
from threading import Thread
from settings import Settings
class BaseSprite(pygame.sprite.Sprite):
"""
BaseSprite类,游戏中所有变化物体的底层父类
"""
def __init__(self, image_name, screen):
super().__init__()
self.screen = screen
self.direction = None
self.speed = None
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
def update(self):
# 根据方向移动
if self.direction == Settings.LEFT:
self.rect.x -= self.speed
elif self.direction == Settings.RIGHT:
self.rect.x += self.speed
elif self.direction == Settings.UP:
self.rect.y -= self.speed
elif self.direction == Settings.DOWN:
self.rect.y += self.speed
class Bullet(BaseSprite):
def __init__(self, image_name, screen):
super().__init__(image_name, screen)
self.speed = Settings.BULLET_SPEED
class TankSprite(BaseSprite):
"""
ImageSprite类,BaseSprite的子类,所有带图片的精灵的父类
"""
def __init__(self, image_name, screen):
super().__init__(image_name, screen)
self.type = None
self.bullets = pygame.sprite.Group()
self.is_alive = True
self.is_moving = False
def shot(self):
"""
射击类,坦克调用该类发射子弹
:return:
"""
# 把消失的子弹移除
self.__remove_sprites()
if not self.is_alive:
return
if len(self.bullets) >= 3:
return
if self.type == Settings.HERO:
pygame.mixer.music.load(Settings.FIRE_MUSIC)
pygame.mixer.music.play()
# 发射子弹
bullet = Bullet(Settings.BULLET_IMAGE_NAME, self.screen)
bullet.direction = self.direction
if self.direction == Settings.LEFT:
bullet.rect.right = self.rect.left
bullet.rect.centery = self.rect.centery
elif self.direction == Settings.RIGHT:
bullet.rect.left = self.rect.right
bullet.rect.centery = self.rect.centery
elif self.direction == Settings.UP:
bullet.rect.bottom = self.rect.top
bullet.rect.centerx = self.rect.centerx
elif self.direction == Settings.DOWN:
bullet.rect.top = self.rect.bottom
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)
def move_out_wall(self, wall):
if self.direction == Settings.LEFT:
self.rect.left = wall.rect.right + 2
elif self.direction == Settings.RIGHT:
self.rect.right = wall.rect.left - 2
elif self.direction == Settings.UP:
self.rect.top = wall.rect.bottom + 2
elif self.direction == Settings.DOWN:
self.rect.bottom = wall.rect.top - 2
def __remove_sprites(self):
"""
移除无用的子弹
:return:
"""
for bullet in self.bullets:
if bullet.rect.bottom <= 0 or \
bullet.rect.top >= Settings.SCREEN_RECT.bottom or \
bullet.rect.right <= 0 or \
bullet.rect.left >= Settings.SCREEN_RECT.right:
self.bullets.remove(bullet)
bullet.kill()
def update(self):
if not self.is_alive:
return
super(TankSprite, self).update()
def boom(self):
pygame.mixer.music.load(Settings.BOOM_MUSIC)
pygame.mixer.music.play()
for boom in Settings.BOOMS:
self.image = pygame.image.load(boom)
time.sleep(0.05)
self.screen.blit(self.image, self.rect)
pygame.mixer.music.stop()
super(TankSprite, self).kill()
def kill(self):
self.is_alive = False
t = Thread(target=self.boom)
t.start()
class Hero(TankSprite):
def __init__(self, image_name, screen):
super(Hero, self).__init__(image_name, screen)
self.type = Settings.HERO
self.speed = Settings.HERO_SPEED
self.direction = Settings.UP
self.is_hit_wall = False
# 初始化英雄的位置
self.rect.centerx = Settings.SCREEN_RECT.centerx - Settings.BOX_RECT.width * 2
self.rect.bottom = Settings.SCREEN_RECT.bottom
def __turn(self):
self.image = pygame.image.load(Settings.HERO_IMAGES.get(self.direction))
def hit_wall(self):
if self.direction == Settings.LEFT and self.rect.left <= 0 or \
self.direction == Settings.RIGHT and self.rect.right >= Settings.SCREEN_RECT.right or \
self.direction == Settings.UP and self.rect.top <= 0 or \
self.direction == Settings.DOWN and self.rect.bottom >= Settings.SCREEN_RECT.bottom:
self.is_hit_wall = True
def update(self):
if not self.is_hit_wall:
super().update()
self.__turn()
def kill(self):
self.is_alive = False
self.boom()
class Enemy(TankSprite):
def __init__(self, image_name, screen):
super().__init__(image_name, screen)
self.is_hit_wall = False
self.type = Settings.ENEMY
self.speed = Settings.ENEMY_SPEED
self.direction = random.randint(0, 3)
self.terminal = float(random.randint(40*2, 40*8))
def random_turn(self):
# 随机转向
self.is_hit_wall = False
directions = [i for i in range(4)]
directions.remove(self.direction)
self.direction = directions[random.randint(0, 2)]
self.terminal = float(random.randint(40*2, 40*8))
self.image = pygame.image.load(Settings.ENEMY_IMAGES.get(self.direction))
def random_shot(self):
shot_flag = random.choice([True] + [False]*59)
if shot_flag:
super().shot()
def hit_wall_turn(self):
turn = False
if self.direction == Settings.LEFT and self.rect.left <= 0:
turn = True
self.rect.left = 2
elif self.direction == Settings.RIGHT and self.rect.right >= Settings.SCREEN_RECT.right-1:
turn = True
self.rect.right = Settings.SCREEN_RECT.right-2
elif self.direction == Settings.UP and self.rect.top <= 0:
turn = True
self.rect.top = 2
elif self.direction == Settings.DOWN and self.rect.bottom >= Settings.SCREEN_RECT.bottom-1:
turn = True
self.rect.bottom = Settings.SCREEN_RECT.bottom-2
if turn:
self.random_turn()
def update(self):
self.random_shot()
if self.terminal <= 0:
self.random_turn()
else:
super().update()
# 碰墙掉头
self.terminal -= self.speed
class Wall(BaseSprite):
def __init__(self, image_name, screen):
super().__init__(image_name, screen)
self.type = None
self.life = 2
def update(self):
pass
def boom(self):
pygame.mixer.music.load(Settings.BOOM_MUSIC)
pygame.mixer.music.play()
for boom in Settings.BOOMS:
self.image = pygame.image.load(boom)
time.sleep(0.07)
self.screen.blit(self.image, self.rect)
pygame.mixer.music.stop()
super().kill()
def kill(self):
self.life -= 1
if not self.life:
t = Thread(target=self.boom)
t.start()