import pygame
import random
# 初始化Pygame
pygame.init()
# 窗口大小
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 800
# 飞机大小
PLANE_WIDTH = 60
PLANE_HEIGHT = 80
# 子弹大小
BULLET_WIDTH = 8
BULLET_HEIGHT = 20
# 创建窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("飞机大战")
# 加载图像
background_img = pygame.image.load("background.png").convert()
plane_img = pygame.image.load("plane.png").convert_alpha()
bullet_img = pygame.image.load("bullet.png").convert_alpha()
enemy_img = pygame.image.load("enemy.png").convert_alpha()
# 设置帧率和时间
clock = pygame.time.Clock()
FPS = 60
delta_time = 0.0
# 定义飞机类
class Plane(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = plane_img
self.rect = self.image.get_rect()
self.rect.centerx = WINDOW_WIDTH / 2
self.rect.bottom = WINDOW_HEIGHT - 10
self.speed = 5
def update(self):
# 移动飞机
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= self.speed
elif keys[pygame.K_RIGHTnetwork error
Pygame库编写的一个简单的飞机大战游戏示例代码
最新推荐文章于 2024-05-30 19:04:56 发布