玩法
你可以控制飞机左右移动,躲避敌机子弹,同时发射自己的炮弹,将敌人击落!
部署方案:
1、代码如下图;
2、将代码保存到一个python中,比如planeFight.py;
3、在你的电脑中安装python环境,然后使用命令:“py planeFight.py” 运行这个文件即可;
代码
import pygame
import random# 初始化 Pygame
pygame.init()# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))# 设置标题
pygame.display.set_caption("飞机大战")# 加载并缩放图片
player_image = pygame.image.load("player.png")
player_image = pygame.transform.scale(player_image, (50, 50))enemy_image = pygame.image.load("enemy.png")
enemy_image = pygame.transform.scale(enemy_image, (50, 50))bullet_image = pygame.image.load("bullet.png")
bullet_image = pygame.transform.scale(bullet_image, (10, 30))# 设置字体
font = pygame.font.Font(None, 36)# 玩家类
class Player(pygame.sprite.Sprite):def __init__(self):super(Player, self).__init__()self.image = player_imageself.rect = self.image.get_rect()self.rect.center = (screen_width // 2, screen_height - 50)self.speed = 5self.lives = 3 # 初始生命值self.score = 0 # 初始分数def update(self):keys = pygame.key.get_pressed()if keys[pygame.K_LEFT] and self.rect.left > 0:self.rect.x -= self.speedif keys[pygame.K_RIGHT] and self.rect.right < screen_width:self.rect.x += self.speeddef draw(self, surface):surface.blit(self.image, self.rect)# 敌人类
class Enemy(pygame.sprite.Sprite):def __init__(self):super(Enemy, self).__init__()self.image = enemy_imageself.rect = self.image.get_rect()self.rect.x = random.randint(0, screen_width - self.rect.width)self.rect.y = random.randint(-100, -40)self.speed = random.randint(1, 3)def update(self):self.rect.y += self.speedif self.rect.top > screen_height:self.rect.x = random.randint(0, screen_width - self.rect.width)self.rect.y = random.randint(-100, -40)self.speed = random.randint(1, 3)# 子弹类
class Bullet(pygame.sprite.Sprite):def __init__(self, x, y):super(Bullet, self).__init__()self.image = bullet_imageself.rect = self.image.get_rect()self.rect.center = (x, y)self.speed = 5def update(self):self.rect.y -= self.speedif self.rect.bottom < 0:self.kill()# 创建精灵组
player = Player()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()for _ in range(10):enemy = Enemy()enemies.add(enemy)all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(enemies)# 游戏主循环
running = True
while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:bullet = Bullet(player.rect.centerx, player.rect.top)bullets.add(bullet)all_sprites.add(bullet)# 更新all_sprites.update()# 检查子弹与敌人的碰撞hits = pygame.sprite.groupcollide(bullets, enemies, True, True)for hit in hits:player.score += 10enemy = Enemy()enemies.add(enemy)all_sprites.add(enemy)# 检查敌人和玩家的碰撞hits = pygame.sprite.spritecollide(player, enemies, True)for hit in hits:player.lives -= 1enemy = Enemy()enemies.add(enemy)all_sprites.add(enemy)if player.lives <= 0:running = False# 绘制screen.fill((0, 0, 0))all_sprites.draw(screen)# 显示分数和生命值score_text = font.render("score: " + str(player.score), True, (255, 255, 255))lives_text = font.render("life: " + str(player.lives), True, (255, 255, 255))screen.blit(score_text, (10, 10))screen.blit(lives_text, (10, 40))pygame.display.flip()# 控制帧率pygame.time.Clock().tick(60)pygame.quit()