利用pygame模块实现飞机大战附加功能如下: (1) 实现碰撞检测(可以消灭敌机,也可以被敌机消灭) (2) 随机出现云朵在天空中漂浮 (3) 随机出现甜甜圈进行能量补给(生命力或子弹类型发生变化) (4) 有GAME OVER条件控制处理
时间: 2023-10-21 13:05:16 浏览: 64
好的,下面是代码实现:
```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("飞机大战")
# 加载图片资源
background_img = pygame.image.load("images/background.png")
player_img = pygame.image.load("images/player.png")
enemy_img = pygame.image.load("images/enemy.png")
cloud_img = pygame.image.load("images/cloud.png")
bullet_img = pygame.image.load("images/bullet.png")
supply_img = pygame.image.load("images/supply.png")
# 设置字体
font = pygame.font.Font(None, 36)
# 定义颜色
white = (255, 255, 255)
red = (255, 0, 0)
# 定义玩家类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_img
self.rect = self.image.get_rect()
self.rect.centerx = screen_width // 2
self.rect.bottom = screen_height - 10
self.speed = 5
self.bullets = pygame.sprite.Group()
self.life = 3
def update(self, *args):
# 移动玩家
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < screen_width:
self.rect.x += self.speed
if keys[pygame.K_UP] and self.rect.top > 0:
self.rect.y -= self.speed
if keys[pygame.K_DOWN] and self.rect.bottom < screen_height:
self.rect.y += self.speed
# 发射子弹
if args and args[0].type == pygame.KEYDOWN and args[0].key == pygame.K_SPACE:
bullet = Bullet(self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
def draw(self, surface):
# 绘制玩家生命值
life_text = font.render(f"Life: {self.life}", True, white)
surface.blit(life_text, (10, 10))
# 绘制玩家
surface.blit(self.image, self.rect)
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = -10
def update(self, *args):
# 移动子弹
self.rect.y += self.speed
# 销毁超出屏幕的子弹
if self.rect.bottom < 0:
self.kill()
def draw(self, surface):
# 绘制子弹
surface.blit(self.image, self.rect)
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(0, screen_width)
self.rect.top = random.randint(-100, -50)
self.speed = random.randint(1, 5)
def update(self, *args):
# 移动敌机
self.rect.y += self.speed
# 销毁超出屏幕的敌机
if self.rect.top > screen_height:
self.kill()
def draw(self, surface):
# 绘制敌机
surface.blit(self.image, self.rect)
# 定义云朵类
class Cloud(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = cloud_img
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(0, screen_width)
self.rect.centery = random.randint(0, screen_height // 3)
def update(self, *args):
pass
def draw(self, surface):
# 绘制云朵
surface.blit(self.image, self.rect)
# 定义能量补给类
class Supply(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = supply_img
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(0, screen_width)
self.rect.top = random.randint(-100, -50)
self.speed = 3
def update(self, *args):
# 移动能量补给
self.rect.y += self.speed
# 销毁超出屏幕的能量补给
if self.rect.top > screen_height:
self.kill()
def draw(self, surface):
# 绘制能量补给
surface.blit(self.image, self.rect)
# 定义游戏结束函数
def game_over(surface):
# 绘制游戏结束文本
game_over_text = font.render("Game Over", True, red)
surface.blit(game_over_text, (screen_width // 2 - 80, screen_height // 2 - 50))
# 绘制得分文本
score_text = font.render(f"Score: {score}", True, white)
surface.blit(score_text, (screen_width // 2 - 60, screen_height // 2))
# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
clouds = pygame.sprite.Group()
supplies = pygame.sprite.Group()
# 创建玩家对象
player = Player()
# 添加对象到精灵组
all_sprites.add(player)
# 设置游戏循环
running = True
score = 0
game_over_flag = False
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
else:
player.update(event)
# 随机添加敌机
if random.randint(1, 100) == 1 and not game_over_flag:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# 随机添加云朵
if random.randint(1, 1000) == 1 and not game_over_flag:
cloud = Cloud()
all_sprites.add(cloud)
clouds.add(cloud)
# 随机添加能量补给
if random.randint(1, 2000) == 1 and not game_over_flag:
supply = Supply()
all_sprites.add(supply)
supplies.add(supply)
# 更新精灵组
all_sprites.update()
# 绘制背景
screen.blit(background_img, (0, 0))
# 绘制精灵组
for sprite in all_sprites:
sprite.draw(screen)
# 碰撞检测
for bullet in player.bullets:
enemies_hit = pygame.sprite.spritecollide(bullet, enemies, True)
for enemy in enemies_hit:
score += 10
bullet.kill()
enemies_hit = pygame.sprite.spritecollide(player, enemies, True)
for enemy in enemies_hit:
player.life -= 1
if player.life <= 0:
game_over_flag = True
supplies_hit = pygame.sprite.spritecollide(player, supplies, True)
for supply in supplies_hit:
player.life += 1
if player.life > 5:
player.life = 5
# 绘制得分
score_text = font.render(f"Score: {score}", True, white)
screen.blit(score_text, (10, screen_height - 50))
# 判断游戏是否结束
if game_over_flag:
game_over(screen)
pygame.display.update()
pygame.time.delay(2000)
running = False
# 更新屏幕
pygame.display.update()
# 退出游戏
pygame.quit()
```
注:以上代码中的图片资源可以自己准备,也可以在网上搜索下载。
阅读全文