用python+pygame实现的飞机大战的源码,包含资源,可直接运行。通过鼠标控制。
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 绘画背景
screen.blit(background,(0, 0))
# 检测游戏状态
if not gameover:
# 定位鼠标的x, y坐标
x, y = pygame.mouse.get_pos()
# 发射子弹
interval_b -= 1
if interval_b < 0:
bullets[index_b].restart()
interval_b = 100
index_b = (index_b + 1) % count_b
for b in bullets:
if b.active:
# 检查子弹命中情况
for e in enemies:
# 击中敌机,分数加100
if checkHit(e, b):
score += 100
b.move()
screen.blit(b.image, (b.x, b.y))
# 绘画机群
for e in enemies:
e.move()
screen.blit(e.image, (e.x, e.y))
if checkCrash(e, plane):
gameover = True
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
screen.blit(plane.image, (plane.x, plane.y))
# 屏幕左上角显示分数
text = font.render("Score: %d" % score, 1, (0, 0, 0))
screen.blit(text, (0, 0))
else:
text = font.render("Score: %d" % score, 1, (0, 0, 0))
screen.blit(text, (190, 400))
# 游戏结束后,检测鼠标抬起就“重置游戏”
if gameover and event.type == pygame.MOUSEBUTTONUP:
plane.restart()
for e in enemies:
e.restart()
for b in bullets:
b.active = False
score = 0
gameover = False
pygame.display.update()
2019-12-21 21:09:15
4.08MB
python
1