forked from IronSpiderMan/TankWar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtank_war.py
204 lines (187 loc) · 7.53 KB
/
tank_war.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
import pygame
from sprites import *
class TankWar:
def __init__(self):
self.screen = pygame.display.set_mode(Settings.SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.game_still = True
self.hero = None
self.enemies = None
self.enemy_bullets = None
self.walls = None
@staticmethod
def __init_game():
"""
初始化游戏的一些设置
:return:
"""
pygame.init() # 初始化pygame模块
pygame.display.set_caption(Settings.GAME_NAME) # 设置窗口标题
pygame.mixer.init() # 初始化音频模块
def __create_sprite(self):
self.hero = Hero(Settings.HERO_IMAGE_NAME, self.screen)
self.enemies = pygame.sprite.Group()
self.enemy_bullets = pygame.sprite.Group()
self.walls = pygame.sprite.Group()
for i in range(Settings.ENEMY_COUNT):
direction = random.randint(0, 3)
enemy = Enemy(Settings.ENEMY_IMAGES[direction], self.screen)
enemy.direction = direction
self.enemies.add(enemy)
self.__draw_map()
def __draw_map(self):
"""
绘制地图
:return:
"""
for y in range(len(Settings.MAP_ONE)):
for x in range(len(Settings.MAP_ONE[y])):
if Settings.MAP_ONE[y][x] == 0:
continue
wall = Wall(Settings.WALLS[Settings.MAP_ONE[y][x]], self.screen)
wall.rect.x = x*Settings.BOX_SIZE
wall.rect.y = y*Settings.BOX_SIZE
if Settings.MAP_ONE[y][x] == Settings.RED_WALL:
wall.type = Settings.RED_WALL
elif Settings.MAP_ONE[y][x] == Settings.IRON_WALL:
wall.type = Settings.IRON_WALL
elif Settings.MAP_ONE[y][x] == Settings.WEED_WALL:
wall.type = Settings.WEED_WALL
elif Settings.MAP_ONE[y][x] == Settings.BOSS_WALL:
wall.type = Settings.BOSS_WALL
wall.life = 1
self.walls.add(wall)
def __check_keydown(self, event):
"""检查按下按钮的事件"""
if event.key == pygame.K_LEFT:
# 按下左键
self.hero.direction = Settings.LEFT
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_RIGHT:
# 按下右键
self.hero.direction = Settings.RIGHT
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_UP:
# 按下上键
self.hero.direction = Settings.UP
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_DOWN:
# 按下下键
self.hero.direction = Settings.DOWN
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_SPACE:
# 坦克发子弹
self.hero.shot()
def __check_keyup(self, event):
"""检查松开按钮的事件"""
if event.key == pygame.K_LEFT:
# 松开左键
self.hero.direction = Settings.LEFT
self.hero.is_moving = False
elif event.key == pygame.K_RIGHT:
# 松开右键
self.hero.direction = Settings.RIGHT
self.hero.is_moving = False
elif event.key == pygame.K_UP:
# 松开上键
self.hero.direction = Settings.UP
self.hero.is_moving = False
elif event.key == pygame.K_DOWN:
# 松开下键
self.hero.direction = Settings.DOWN
self.hero.is_moving = False
def __event_handler(self):
for event in pygame.event.get():
# 判断是否是退出游戏
if event.type == pygame.QUIT:
TankWar.__game_over()
elif event.type == pygame.KEYDOWN:
TankWar.__check_keydown(self, event)
elif event.type == pygame.KEYUP:
TankWar.__check_keyup(self, event)
def __check_collide(self):
# 保证坦克不移出屏幕
self.hero.hit_wall()
for enemy in self.enemies:
enemy.hit_wall_turn()
# 子弹击中墙
for wall in self.walls:
# 我方英雄子弹击中墙
for bullet in self.hero.bullets:
if pygame.sprite.collide_rect(wall, bullet):
if wall.type == Settings.RED_WALL:
wall.kill()
bullet.kill()
elif wall.type == Settings.BOSS_WALL:
self.game_still = False
elif wall.type == Settings.IRON_WALL:
bullet.kill()
# 敌方英雄子弹击中墙
for enemy in self.enemies:
for bullet in enemy.bullets:
if pygame.sprite.collide_rect(wall, bullet):
if wall.type == Settings.RED_WALL:
wall.kill()
bullet.kill()
elif wall.type == Settings.BOSS_WALL:
self.game_still = False
elif wall.type == Settings.IRON_WALL:
bullet.kill()
# 我方坦克撞墙
if pygame.sprite.collide_rect(self.hero, wall):
# 不可穿越墙
if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.BOSS_WALL:
self.hero.is_hit_wall = True
# 移出墙内
self.hero.move_out_wall(wall)
# 敌方坦克撞墙
for enemy in self.enemies:
if pygame.sprite.collide_rect(wall, enemy):
if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.BOSS_WALL:
enemy.move_out_wall(wall)
enemy.random_turn()
# 子弹击中、敌方坦克碰撞、敌我坦克碰撞
pygame.sprite.groupcollide(self.hero.bullets, self.enemies, True, True)
# 敌方子弹击中我方
for enemy in self.enemies:
for bullet in enemy.bullets:
if pygame.sprite.collide_rect(bullet, self.hero):
bullet.kill()
self.hero.kill()
def __update_sprites(self):
if self.hero.is_moving:
self.hero.update()
self.walls.update()
self.hero.bullets.update()
self.enemies.update()
for enemy in self.enemies:
enemy.bullets.update()
enemy.bullets.draw(self.screen)
self.enemies.draw(self.screen)
self.hero.bullets.draw(self.screen)
self.screen.blit(self.hero.image, self.hero.rect)
self.walls.draw(self.screen)
def run_game(self):
self.__init_game()
self.__create_sprite()
while True and self.hero.is_alive and self.game_still:
self.screen.fill(Settings.SCREEN_COLOR)
# 1、设置刷新帧率
self.clock.tick(Settings.FPS)
# 2、事件监听
self.__event_handler()
# 3、碰撞监测
self.__check_collide()
# 4、更新/绘制精灵/经理组
self.__update_sprites()
# 5、更新显示
pygame.display.update()
self.__game_over()
@staticmethod
def __game_over():
pygame.quit()
exit()