用python写一个飞机大战的游戏
时间: 2024-05-29 08:03:48 浏览: 120
用Python写一个飞机大战小游戏
3星 · 编辑精心推荐
好的,下面是一个简单的飞机大战游戏的代码,使用Python的Pygame库实现:
```python
import pygame
import random
# 初始化 Pygame 库
pygame.init()
# 设置游戏窗口的尺寸
screen_width, screen_height = 480, 700
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏窗口标题和图标
pygame.display.set_caption("飞机大战")
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)
# 加载游戏所需的图像资源
background = pygame.image.load("background.png")
player_img = pygame.image.load("player.png")
bullet_img = pygame.image.load("bullet.png")
enemy_img = pygame.image.load("enemy.png")
explosion_imgs = [pygame.image.load(f"explosion{i}.png") for i in range(1, 6)]
# 设置游戏的帧率和时钟对象
FPS = 60
clock = pygame.time.Clock()
# 定义游戏角色的类
class Sprite:
def __init__(self, x, y, img):
self.x = x
self.y = y
self.img = img
self.rect = self.img.get_rect()
self.rect.center = (self.x, self.y)
def draw(self):
screen.blit(self.img, self.rect)
# 定义玩家角色的类
class Player(Sprite):
def __init__(self):
super().__init__(screen_width // 2, screen_height - 100, player_img)
self.speed = 5
self.bullets = []
self.health = 3
def move(self, dir):
if dir == "left":
self.x -= self.speed
elif dir == "right":
self.x += self.speed
if self.x < 0:
self.x = 0
elif self.x > screen_width:
self.x = screen_width
self.rect.center = (self.x, self.y)
def shoot(self):
bullet = Bullet(self.x, self.y)
self.bullets.append(bullet)
def check_collision(self, enemies):
for enemy in enemies:
if pygame.sprite.collide_rect(enemy, self):
self.health -= 1
if self.health <= 0:
self.kill()
enemy.kill()
def kill(self):
explosion = Explosion(self.x, self.y)
explosions.append(explosion)
self.health = 0
# 定义子弹角色的类
class Bullet(Sprite):
def __init__(self, x, y):
super().__init__(x, y, bullet_img)
self.speed = -10
def update(self):
self.y += self.speed
if self.y < 0:
self.kill()
# 定义敌人角色的类
class Enemy(Sprite):
def __init__(self):
x = random.randint(0, screen_width)
y = -50
super().__init__(x, y, enemy_img)
self.speed = random.randint(1, 5)
def update(self):
self.y += self.speed
if self.y > screen_height:
self.kill()
# 定义爆炸效果角色的类
class Explosion(Sprite):
def __init__(self, x, y):
super().__init__(x, y, explosion_imgs[0])
self.idx = 0
self.timer = 0
def update(self):
self.timer += 1
if self.timer >= 5:
self.idx += 1
if self.idx >= len(explosion_imgs):
self.kill()
else:
self.img = explosion_imgs[self.idx]
self.timer = 0
# 创建玩家对象和敌人列表
player = Player()
enemies = pygame.sprite.Group()
explosions = pygame.sprite.Group()
# 游戏主循环
running = True
while running:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.move("left")
elif event.key == pygame.K_RIGHT:
player.move("right")
elif event.key == pygame.K_SPACE:
player.shoot()
# 更新游戏角色
for bullet in player.bullets:
bullet.update()
player.bullets = [bullet for bullet in player.bullets if not bullet.is_dead()]
if random.randint(1, 30) == 1:
enemy = Enemy()
enemies.add(enemy)
for enemy in enemies:
enemy.update()
player.check_collision(enemies)
for explosion in explosions:
explosion.update()
# 绘制游戏画面
screen.blit(background, (0, 0))
player.draw()
for bullet in player.bullets:
bullet.draw()
enemies.draw(screen)
explosions.draw(screen)
# 更新游戏画面
pygame.display.update()
# 控制游戏帧率
clock.tick(FPS)
# 结束游戏
pygame.quit()
```
在运行该代码之前,你需要先准备好一些游戏所需的图像资源,例如背景图、玩家图、子弹图、敌人图和爆炸效果图等。你可以从互联网上下载这些图像资源,然后将它们保存在与代码文件相同的目录下。
阅读全文