【飞机大战】第一天游戏界面绘制,源代码可运行

飞机大战是一个经典的小游戏,通常由玩家控制一个飞机,目标是摧毁尽可能多的敌机,并躲避敌机的攻击。下面是一个简单的Python飞机大战游戏的示例,使用了pygame库来创建游戏界面和处理游戏逻辑。
在这里插入图片描述

效果图在这里插入图片描述

首先,确保你已经安装了pygame库。如果没有安装,可以通过以下命令安装:

pip install pygame

接下来是飞机大战游戏的代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕尺寸
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640

# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 创建屏幕对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# 设置游戏标题
pygame.display.set_caption('飞机大战')

# 加载飞机图片并缩放
player_img = pygame.image.load('player.png').convert_alpha()
player_img = pygame.transform.scale(player_img, (int(player_img.get_width() * 0.5), int(player_img.get_height() * 0.5)))
player_x = SCREEN_WIDTH / 2 - player_img.get_width() / 2
player_y = SCREEN_HEIGHT - player_img.get_height() - 10
player_speed = 5

# 加载敌机图片并缩放
enemy_img = pygame.image.load('enemy.png').convert_alpha()
enemy_img = pygame.transform.scale(enemy_img, (int(enemy_img.get_width() * 0.5), int(enemy_img.get_height() * 0.5)))
enemy_x = random.randint(0, SCREEN_WIDTH - enemy_img.get_width())
enemy_y = random.randint(50, 150)
enemy_speed = 2  # 减慢敌机的移动速度

# 初始化子弹列表
bullets = []

# 子弹属性
bullet_speed = 10
bullet_width = 4
bullet_height = 10

# 游戏循环标志
running = True

# 敌机得分
score = 0


# 函数:绘制子弹
def draw_bullets(bullet_list):
    for bullet in bullet_list:
        pygame.draw.rect(screen, WHITE, (bullet[0], bullet[1], bullet_width, bullet_height))


# 函数:更新子弹位置
def update_bullets(bullet_list):
    for bullet in bullet_list[:]:
        bullet[1] -= bullet_speed
        if bullet[1] < 0:
            bullet_list.remove(bullet)


# 函数:检测子弹和敌机的碰撞
def check_collision(bullet, enemy):
    bullet_rect = pygame.Rect(bullet[0], bullet[1], bullet_width, bullet_height)
    enemy_rect = pygame.Rect(enemy[0], enemy[1], enemy_img.get_width(), enemy_img.get_height())
    return bullet_rect.colliderect(enemy_rect)


# 游戏主循环
while running:
    # 填充屏幕背景色
    screen.fill(BLACK)

    # 事件处理
    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 = [player_x + player_img.get_width() / 2 - bullet_width / 2, 0]
                bullets.append(bullet)

    # 检测按键
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed
    if keys[pygame.K_RIGHT] and player_x < SCREEN_WIDTH - player_img.get_width():
        player_x += player_speed

    # 飞机移动边界控制
    player_x = max(0, min(player_x, SCREEN_WIDTH - player_img.get_width()))

    # 敌机移动
    enemy_y += enemy_speed
    if enemy_y > SCREEN_HEIGHT:
        score -= 10  # 敌机逃脱减少得分
        enemy_x = random.randint(0, SCREEN_WIDTH - enemy_img.get_width())
        enemy_y = random.randint(50, 150)

    # 绘制飞机和敌机
    screen.blit(player_img, (player_x, player_y))
    screen.blit(enemy_img, (enemy_x, enemy_y))

    # 更新子弹位置并绘制
    update_bullets(bullets)
    draw_bullets(bullets)

    # 检测子弹和敌机的碰撞
    for bullet in bullets[:]:
        if check_collision([bullet[0], bullet[1]], [enemy_x, enemy_y]):
            score += 10  # 增加得分
            bullets.remove(bullet)
            break  # 每次只处理一次碰撞

    # 更新屏幕显示
    pygame.display.update()

    # 显示得分
    font = pygame.font.SysFont(None, 35)
    score_text = font.render('Score: ' + str(score), True, WHITE)
    screen.blit(score_text, (10, 10))

# 退出游戏
pygame.quit()

在这个示例中,我们首先初始化了pygame库并设置了屏幕的尺寸和标题。然后,我们加载了飞机和敌机的图片,并设置了它们的初始位置和速度。
在游戏的主循环中,我们处理了事件(例如退出游戏),检测了按键输入以控制飞机的移动,并更新了敌机的位置。如果敌机飞出了屏幕,我们会重新生成它的位置。
最后,我们在屏幕上绘制飞机和敌机,并更新屏幕显示。当游戏退出时,我们调用pygame.quit()来清理资源。

请注意,这个代码示例假设你有一个名为player.png的飞机图片和一个名为enemy.png的敌机图片,并将它们放在了与代码相同的目录下。你需要将这些图片替换为你自己的图片,或者更改代码中的图片路径以匹配你的文件位置。

这个简单的飞机大战游戏可以作为一个很好的起点,你可以在此基础上添加更多的功能,比如子弹射击、碰撞检测、得分系统、音效和更多种类的敌机等。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 10:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 10:04:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 10:04:03       82 阅读
  4. Python语言-面向对象

    2024-03-28 10:04:03       91 阅读

热门阅读

  1. python保存中间变量(学习笔记)

    2024-03-28 10:04:03       33 阅读
  2. AcWing 1221. 四平方和

    2024-03-28 10:04:03       35 阅读
  3. shutil模块篇

    2024-03-28 10:04:03       39 阅读
  4. 手机安卓系统内嵌测试代码分享

    2024-03-28 10:04:03       48 阅读
  5. 在 Android 系统中,窗口(Window)按照功能和层级

    2024-03-28 10:04:03       44 阅读
  6. 视觉循迹小车(旭日x3派、摄像头、循迹)

    2024-03-28 10:04:03       43 阅读
  7. 2023.03.28

    2024-03-28 10:04:03       45 阅读
  8. 软考 - 软件架构设计师 - 架构风格

    2024-03-28 10:04:03       43 阅读
  9. 【React】React 组件 API

    2024-03-28 10:04:03       46 阅读
  10. 深入理解 React 中的 children props 和 render props

    2024-03-28 10:04:03       51 阅读
  11. 11 React 组件通信 父传子

    2024-03-28 10:04:03       40 阅读
  12. React系列之常用ReactHook

    2024-03-28 10:04:03       38 阅读