Python战机

基础版

import pygame
import random

# 设置游戏屏幕大小
screen_width = 480
screen_height = 600

# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 初始化pygame
pygame.init()

# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("雷霆战机")

# 加载背景音乐
pygame.mixer.music.load("background.wav")
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)

# 加载玩家飞机图片
player_img = pygame.image.load("player.png")
player_rect = player_img.get_rect()
player_rect.centerx = screen_width // 2
player_rect.bottom = screen_height - 10

# 创建子弹精灵组
bullet_group = pygame.sprite.Group()

# 设置游戏时钟
clock = pygame.time.Clock()

# 初始化分数
score = 0

# 游戏主循环
running = True
while running:
    # 设置帧率
    clock.tick(60)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bullet = pygame.Rect(player_rect.centerx, player_rect.top - 10, 5, 10)
                bullet_group.add(bullet)

    # 检测子弹是否击中敌机
    for bullet in bullet_group:
        bullet.y -= 10
        if bullet.y < 0:
            bullet_group.remove(bullet)
        else:
            pygame.draw.rect(screen, WHITE, bullet)

    # 绘制玩家飞机
    screen.blit(player_img, player_rect)

    # 更新屏幕
    pygame.display.flip()

    # 清空屏幕
    screen.fill(BLACK)

完善版

import pygame
import random

# 设置游戏屏幕大小
screen_width = 480
screen_height = 600

# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 初始化pygame
pygame.init()

# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("雷霆战机")

# 加载背景音乐
pygame.mixer.music.load("background.wav")
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)

# 加载玩家飞机图片
player_img = pygame.image.load("player.png")
player_rect = player_img.get_rect()
player_rect.centerx = screen_width // 2
player_rect.bottom = screen_height - 10

# 加载敌机图片
enemy_img = pygame.image.load("enemy.png")

# 创建子弹精灵组
bullet_group = pygame.sprite.Group()

# 创建敌机精灵组
enemy_group = pygame.sprite.Group()

# 设置游戏时钟
clock = pygame.time.Clock()

# 初始化分数
score = 0

# 显示分数的函数
def show_score():
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))

# 显示游戏结束的函数
def show_game_over():
    font = pygame.font.Font(None, 48)
    game_over_text = font.render("Game Over", True, RED)
    screen.blit(game_over_text, (screen_width//2 - 100, screen_height//2 - 50))

# 游戏主循环
running = True
while running:
    # 设置帧率
    clock.tick(60)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bullet = pygame.Rect(player_rect.centerx, player_rect.top - 10, 5, 10)
                bullet_group.add(bullet)

    # 检测子弹是否击中敌机
    for bullet in bullet_group:
        bullet.y -= 10
        if bullet.y < 0:
            bullet_group.remove(bullet)
        else:
            pygame.draw.rect(screen, WHITE, bullet)

    # 绘制玩家飞机
    screen.blit(player_img, player_rect)

    # 生成敌机
    if random.randint(0, 100) < 3:
        enemy_rect = enemy_img.get_rect()
        enemy_rect.x = random.randint(0, screen_width - enemy_rect.width)
        enemy_rect.y = -enemy_rect.height
        enemy_group.add(enemy_rect)

    # 移动敌机
    for enemy in enemy_group:
        enemy.y += 5
        if enemy.y > screen_height:
            enemy_group.remove(enemy)

    # 绘制敌机
    screen.blit(enemy_img, enemy_rect)

    # 检测敌机和玩家飞机是否碰撞
    if pygame.sprite.spritecollide(player_rect, enemy_group, True):
        running = False

    # 检测子弹和敌机是否碰撞
    if pygame.sprite.groupcollide(bullet_group, enemy_group, True, True):
        score += 1

    # 显示分数
    show_score()

    # 更新屏幕
    pygame.display.flip()

    # 清空屏幕
    screen.fill(BLACK)

# 显示游戏结束界面
show_game_over()

# 更新屏幕
pygame.display.flip()

# 等待2秒钟后退出游戏
pygame.time.wait(2000)

# 退出游戏
pygame.quit()
 

相关推荐

  1. Python战机

    2024-01-10 11:36:03       32 阅读
  2. python无人医疗战车

    2024-01-10 11:36:03       29 阅读
  3. Python在无人战争机器人

    2024-01-10 11:36:03       28 阅读
  4. P5461 赦免战俘

    2024-01-10 11:36:03       23 阅读
  5. python

    2024-01-10 11:36:03       39 阅读
  6. python

    2024-01-10 11:36:03       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 11:36:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 11:36:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 11:36:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 11:36:03       20 阅读

热门阅读

  1. spring redis 连接和连接池配置 使用

    2024-01-10 11:36:03       37 阅读
  2. DevOps(4)

    2024-01-10 11:36:03       37 阅读
  3. 2024系统分析师---论软件系统架构风格

    2024-01-10 11:36:03       32 阅读
  4. 机器学习 -- 贝叶斯决策理论

    2024-01-10 11:36:03       40 阅读
  5. API的介绍

    2024-01-10 11:36:03       40 阅读
  6. git stash 命令详解

    2024-01-10 11:36:03       64 阅读
  7. redis(1)

    2024-01-10 11:36:03       38 阅读
  8. MATLAB中slist函数用法

    2024-01-10 11:36:03       33 阅读