python写一个飞机大战源码
时间: 2023-05-11 18:06:32 浏览: 111
以下是一个简单的飞机大战游戏的 Python 源码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口的大小
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption("飞机大战")
# 加载背景图片
background = pygame.image.load("images/background.png").convert()
# 加载飞机图片
player_img = pygame.image.load("images/player.png").convert_alpha()
# 加载敌机图片
enemy_img = pygame.image.load("images/enemy.png").convert_alpha()
# 加载子弹图片
bullet_img = pygame.image.load("images/bullet.png").convert_alpha()
# 设置字体
font = pygame.font.SysFont(None, 48)
# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
# 定义玩家飞机类
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()
def update(self):
# 移动飞机
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_LEFT]:
self.rect.x -= self.speed
if key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_UP]:
self.rect.y -= self.speed
if key_pressed[pygame.K_DOWN]:
self.rect.y += self.speed
# 限制飞机的移动范围
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > screen_width:
self.rect.right = screen_width
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
# 发射子弹
if key_pressed[pygame.K_SPACE]:
bullet = Bullet()
bullet.rect.centerx = self.rect.centerx
bullet.rect.bottom = self.rect.top
self.bullets.add(bullet)
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_img
self.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.speed
if self.rect.top > screen_height:
self.kill()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = bullet_img
self.rect = self.image.get_rect()
self.speed = -10
def update(self):
self.rect.y += self.speed
if self.rect.bottom < 0:
self.kill()
# 创建玩家飞机
player = Player()
# 创建敌机组
enemies = pygame.sprite.Group()
# 创建子弹组
bullets = pygame.sprite.Group()
# 设置帧率
clock = pygame.time.Clock()
FPS = 60
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新玩家飞机
player.update()
# 更新敌机
if random.randint(1, 60) == 1:
enemy = Enemy()
enemies.add(enemy)
enemies.update()
# 更新子弹
bullets.update()
# 检测子弹是否击中敌机
hits = pygame.sprite.groupcollide(bullets, enemies, True, True)
# 显示得分
score = len(hits)
score_text = font.render(str(score), True, white)
screen.blit(score_text, (10, 10))
# 绘制游戏界面
screen.blit(background, (0, 0))
player.bullets.draw(screen)
enemies.draw(screen)
bullets.draw(screen)
player.draw(screen)
pygame.display.update()
# 控制帧率
clock.tick(FPS)
# 退出 Pygame
pygame.quit()
```
这个源码实现了一个简单的飞机大战游戏,玩家可以通过键盘控制飞机移动和发射子弹,敌机会从屏幕顶部随机出现并向下移动,玩家需要尽可能地击落敌机来得分。
阅读全文