-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path08Explode.py
More file actions
151 lines (120 loc) · 4.19 KB
/
Copy path08Explode.py
File metadata and controls
151 lines (120 loc) · 4.19 KB
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
import pygame
import sys
from demos.W3.myplane.Bullet import Bullet
from demos.W3.myplane.Enemy import SmallEnemy
from demos.W3.myplane.Hero import Hero
# 全局初始化
pygame.init()
pygame.mixer.init()
# 设置窗口大小和标题
resolution = width, height = 480, 700
windowSurface = pygame.display.set_mode(resolution) # 设置分辨率并得到全局的绘图表面
pygame.display.set_caption("飞机大战")
# 加载背景图
bgSurface = pygame.image.load("./images/background.png").convert()
# 加载背景音乐
pygame.mixer.music.load("./sound/game_music.ogg")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.4)
# 加载音效
bombSound = pygame.mixer.Sound("./sound/use_bomb.wav")
bulletSound = pygame.mixer.Sound("./sound/bullet.wav")
# 加载字体
textFont = pygame.font.Font("./font/font.ttf", 30)
# 创建时钟对象
clock = pygame.time.Clock()
# 系统常量
ENEMY_NUM = 10
BULLET_NUM = 10
if __name__ == '__main__':
# 创建英雄实例
hero = Hero(width, height)
# 创建敌机Group
seGroup = pygame.sprite.Group()
for i in range(ENEMY_NUM):
se = SmallEnemy(width, height)
seGroup.add(se)
# 创建子弹
bList = []
bIndex = 0
for i in range(BULLET_NUM):
b = Bullet()
bList.append(b)
count = 0
# 开启消息循环
while True:
count += 1
# 处理用户输入
for event in pygame.event.get():
# 处理退出事件
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 感应和处理鼠标事件
if event.type == pygame.MOUSEBUTTONDOWN:
print("MOUSEBUTTONDOWN @ ", event.pos)
if hero.rect.collidepoint(event.pos):
print("别摸我")
if event.type == pygame.MOUSEBUTTONUP:
print("MOUSEBUTTONUP @ ", event.pos)
if event.type == pygame.MOUSEMOTION:
# print("MOUSEMOTION @ ", event.pos)
pass
# 处理键盘事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print("开炮!")
# 检测当前按下的按钮有哪些
bools = pygame.key.get_pressed()
# print(bools)
if bools[pygame.K_w]:
hero.moveUp()
if bools[pygame.K_s]:
hero.moveDown()
if bools[pygame.K_a]:
hero.moveLeft()
if bools[pygame.K_d]:
hero.moveRight()
# 绘制背景
windowSurface.blit(bgSurface, (0, 0))
# 绘制英雄
if count % 3 == 0:
windowSurface.blit(hero.mSurface1, hero.rect)
else:
windowSurface.blit(hero.mSurface2, hero.rect)
# 绘制敌机
for se in seGroup:
if se.isAlive:
windowSurface.blit(se.mSurface, se.rect)
# 每一帧都让敌机飞行5公里
se.move()
else:
se.destroy(count, windowSurface, bombSound)
# 每10帧在机头射出一颗子弹
if count % 10 == 0:
b = bList[bIndex] # 取出一颗子弹
b.reset(hero.rect.midtop) # 立即装载到【当前】机头位置
# windowSurface.blit(b.mSurface, b.rect) # 画子弹
bulletSound.play() # 呼啸声
bIndex = (bIndex + 1) % BULLET_NUM # 序号递增
# 每帧都让子弹飞
for b in bList:
if b.isAlive:
windowSurface.blit(b.mSurface, b.rect) # 画子弹
b.move()
# 绘制文字
textSurface = textFont.render("Score:000", True, (255, 255, 255))
windowSurface.blit(textSurface, (10, 10))
# 子弹-敌机碰撞检测
for b in bList:
if b.isAlive:
hitEnemyList = pygame.sprite.spritecollide(b, seGroup, False, pygame.sprite.collide_mask)
if len(hitEnemyList) > 0:
b.isAlive = False
for se in hitEnemyList:
se.isAlive = False
# 刷新界面
pygame.display.flip()
# 时钟停留一帧的时长
clock.tick(60)
pass