-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
276 lines (208 loc) · 7.37 KB
/
main.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
# coding=utf8
import pygame
import random
import time
WIDTH = 480
HEIGHT = 800
SHOOT_PC = 0
ENEMY_PC = 0
enemy_add = 300
player_shoot = 0
score = 0
level = 1
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('飞机大战')
background = pygame.image.load('resources/image/background.png').convert()
plane_img = pygame.image.load('resources/image/shoot.png')
shootMusic = pygame.mixer.Sound('resources/sound/bullet.mp3')
level_up_music = pygame.mixer.Sound('resources/sound/get_bomb.mp3')
# 子弹对象
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_image, bullet_position):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_image
self.rect = self.image.get_rect()
self.rect.midbottom = bullet_position
self.move = 1.1
def bulletMove(self):
self.rect.top -= self.move
# player对象
class Player(pygame.sprite.Sprite):
def __init__(self, plane_img, player_rect, player_position):
pygame.sprite.Sprite.__init__(self)
self.image = []
for i in range(len(player_rect)):
self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
self.rect = player_rect[0]
self.rect.topleft = player_position
self.img_index = 0
self.move = 1
self.bullets = pygame.sprite.Group()
self.is_hit = False
# player 移动
def moveUp(self):
if self.rect.top <= 0:
self.rect.top = 0
else:
self.rect.top -= self.move
def moveDown(self):
if self.rect.bottom >= HEIGHT:
self.rect.bottom = HEIGHT
else:
self.rect.bottom += self.move
def moveLeft(self):
if self.rect.left <= 0:
self.rect.left = 0
else:
self.rect.left -= self.move
def moveRight(self):
if self.rect.right >= WIDTH:
self.rect.right = WIDTH
else:
self.rect.right += self.move
def shoot(self, bullet_img):
bullet = Bullet(bullet_img, self.rect.midtop)
self.bullets.add(bullet)
shootMusic.play()
# 敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self, enemy_img, enemy_shoot_img, enemy_position):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.topleft = enemy_position
self.shoot_imgs = enemy_shoot_img
self.move = 1
self.shoot_index = 0
def enemyMove(self):
self.rect.top += self.move
# 加载player 飞机图片
player_rect = [pygame.Rect(0, 99, 102, 126),
pygame.Rect(165, 360, 102, 126),
pygame.Rect(165, 234, 102, 126),
pygame.Rect(330, 624, 102, 126),
pygame.Rect(330, 498, 102, 126),
pygame.Rect(432, 624, 102, 126)]
player_position = [100, 400]
player = Player(plane_img, player_rect, player_position)
# 加载子弹图片
bullet_rect = pygame.Rect(69, 78, 9, 21)
bullet_img = plane_img.subsurface(bullet_rect)
# 加载敌机图片
enemy_rect = pygame.Rect(534, 612, 57, 43)
enemy_img = plane_img.subsurface(enemy_rect)
enemies = pygame.sprite.Group()
# 加载敌机被击中图片
enemies_shoot_img = [plane_img.subsurface(pygame.Rect(267, 347, 57, 43)),
plane_img.subsurface(pygame.Rect(873, 697, 57, 43)),
plane_img.subsurface(pygame.Rect(267, 296, 57, 43)),
plane_img.subsurface(pygame.Rect(930, 697, 57, 43))]
enemies_shoot = pygame.sprite.Group()
backgroundMusic = pygame.mixer.music.load('resources/sound/game_music.mp3')
pygame.mixer.music.play(-1, 0.0)
enemy_shoot_sound = pygame.mixer.Sound('resources/sound/enemy3_down.mp3')
enemy_shoot_sound.set_volume(1.0)
RUN = True
while RUN:
screen.fill(0)
screen.blit(background, (0, 0))
if not player.is_hit:
screen.blit(player.image[player.img_index], player.rect)
player.img_index = SHOOT_PC / 247
else:
player.img_index = player_shoot / 247
screen.blit(player.image[player.img_index], player.rect)
player_shoot += 31
if player_shoot > 495:
RUN = False
time.sleep(1)
if ENEMY_PC % enemy_add == 0:
enemy_position = [random.randint(0, WIDTH - enemy_rect.width), 0]
enemy = Enemy(enemy_img, enemies_shoot_img, enemy_position)
enemies.add(enemy)
ENEMY_PC = ENEMY_PC + 2
if ENEMY_PC == 1000:
ENEMY_PC = 0
for bullet in player.bullets:
bullet.bulletMove()
if bullet.rect.bottom <= 0:
player.bullets.remove(bullet)
player.bullets.draw(screen)
for enemy in enemies:
enemy.enemyMove()
if pygame.sprite.collide_circle_ratio(0.6)(player, enemy):
enemies_shoot.add(enemy)
enemies.remove(enemy)
player.is_hit = True
break
if enemy.rect.top > HEIGHT:
enemies.remove(enemy)
for enemy_shoots in enemies_shoot:
if enemy_shoots.shoot_index == 0:
pass
if enemy_shoots.shoot_index > 70:
enemies_shoot.remove(enemy_shoots)
score += 100
continue
screen.blit(enemy_shoots.shoot_imgs[enemy_shoots.shoot_index / 20], enemy_shoots.rect)
enemy_shoots.shoot_index += 1
enemies.draw(screen)
# 分数显示
score_font = pygame.font.Font(None, 36)
score_text = score_font.render(str(score), True, (128, 128, 128))
text_rect = score_text.get_rect()
text_rect.topleft = [10, 10]
screen.blit(score_text, text_rect)
if score == 100 * (level ** 2 + level):
level += 1
level_up_music.play()
if level != 10:
enemy_add -= 20
# 等级显示
level_font = pygame.font.Font(None, 42)
level_text = level_font.render('Level ' + str(level), True, (128, 128, 128, 128))
level_rect = level_text.get_rect()
level_rect.midtop = [240, 10]
screen.blit(level_text, level_rect)
enemies_is_shoot = pygame.sprite.groupcollide(enemies, player.bullets, 0.6, 0.8)
for enemy_shoot in enemies_is_shoot:
enemy_shoot_sound.play()
enemies_shoot.add(enemy_shoot)
# 获取键盘按键
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
player.moveUp()
if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
player.moveDown()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
player.moveLeft()
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
player.moveRight()
if key_pressed[pygame.K_SPACE]:
if SHOOT_PC % 495 == 0:
player.shoot(bullet_img)
SHOOT_PC = SHOOT_PC + 5
if SHOOT_PC >= 495:
SHOOT_PC = 0
pygame.display.update()
# 退出程序
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if not RUN:
pygame.mixer.music.stop()
GAMEOVER = True
# 加载gameOver图片
gameOver_image = pygame.image.load('resources/image/gameover.png')
# 加载GameOVer音乐
gameOver_music = pygame.mixer.music.load('resources/sound/game_over.mp3')
pygame.mixer.music.play(0, 0.0)
while GAMEOVER:
screen.fill(0)
screen.blit(gameOver_image, (0, 0))
pygame.display.update()
if pygame.mixer.music.get_busy() == 0:
pygame.quit()
exit()