[Python] 纯文本查看 复制代码
#像素枪战(下)
# 更新所有精灵
all_sprites.update()
# 检测子弹与敌人的碰撞
hits = pygame.sprite.groupcollide(enemies, bullets, False, True)
for enemy, bullet_list in hits.items():
enemy.health -= len(bullet_list)
if enemy.health <= 0:
# 创建爆炸效果
explosion = Explosion(enemy.rect.center)
all_sprites.add(explosion)
enemy.kill()
score += SCORE_PER_KILL
# 检测敌人与玩家的碰撞
hits = pygame.sprite.spritecollide(player, enemies, True)
for hit in hits:
player.health -= 1
explosion = Explosion(hit.rect.center)
all_sprites.add(explosion)
if player.health <= 0:
game_over = True
# 渲染
screen.fill(BLACK)
if show_menu:
# 显示菜单
title_text = game_font.render("像素枪战", True, WHITE)
start_text = small_font.render("按空格键开始游戏", True, WHITE)
exit_text = small_font.render("按ESC键退出", True, WHITE)
screen.blit(title_text, (SCREEN_WIDTH//2 -
title_text.get_width()//2, SCREEN_HEIGHT//3))
screen.blit(start_text, (SCREEN_WIDTH//2 -
start_text.get_width()//2, SCREEN_HEIGHT//2))
screen.blit(exit_text, (SCREEN_WIDTH//2 -
exit_text.get_width()//2, SCREEN_HEIGHT//2 + 50))
elif game_over:
# 显示游戏结束画面
game_over_text = game_font.render("游戏结束", True, WHITE)
score_text = game_font.render(f"得分: {score}", True, WHITE)
restart_text = small_font.render("按空格键重新开始", True, WHITE)
exit_text = small_font.render("按ESC键退出", True, WHITE)
screen.blit(game_over_text, (SCREEN_WIDTH//2 -
game_over_text.get_width()//2, SCREEN_HEIGHT//4))
screen.blit(score_text, (SCREEN_WIDTH//2 -
score_text.get_width()//2, SCREEN_HEIGHT//3))
screen.blit(restart_text, (SCREEN_WIDTH//2 -
restart_text.get_width()//2, SCREEN_HEIGHT//2))
screen.blit(exit_text, (SCREEN_WIDTH//2 -
exit_text.get_width()//2, SCREEN_HEIGHT//2 + 50))
else:
# 绘制所有精灵
all_sprites.draw(screen)
# 显示分数和生命值
score_text = small_font.render(f"得分: {score}", True, WHITE)
health_text = small_font.render(f"生命值: {player.health}", True, WHITE)
screen.blit(score_text, (10, 10))
screen.blit(health_text, (10, 40))
# 显示准星
mouse_x, mouse_y = pygame.mouse.get_pos()
pygame.draw.circle(screen, WHITE, (mouse_x, mouse_y), 8, 1)
pygame.draw.line(screen, WHITE, (mouse_x-5, mouse_y),
(mouse_x+5, mouse_y), 1)
pygame.draw.line(screen, WHITE, (mouse_x, mouse_y-5),
(mouse_x, mouse_y+5), 1)
# 更新显示
pygame.display.flip()
# 退出游戏
pygame.quit()
sys.exit()
[/mw_shl_code]