python飞机大战代码
时间: 2023-10-21 12:30:03 浏览: 84
以下是一个简单的 Python 飞机大战的代码示例:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 700
WINDOW_SURFACE = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 设置游戏标题
pygame.display.set_caption('飞机大战')
# 加载游戏背景
BACKGROUND_IMAGE = pygame.image.load('background.png').convert()
# 加载玩家飞机图片
PLAYER_IMAGE = pygame.image.load('player.png').convert_alpha()
# 加载敌机图片
ENEMY_IMAGE = pygame.image.load('enemy.png').convert_alpha()
# 加载子弹图片
BULLET_IMAGE = pygame.image.load('bullet.png').convert_alpha()
# 设置字体
FONT = pygame.font.SysFont('arial', 36)
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = PLAYER_IMAGE
self.rect = self.image.get_rect()
self.rect.centerx = WINDOW_WIDTH // 2
self.rect.bottom = WINDOW_HEIGHT - 10
self.speed = 8
def update(self):
# 移动玩家飞机
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 < WINDOW_WIDTH:
self.rect.x += self.speed
# 发射子弹
if keys[pygame.K_SPACE]:
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
# 定义敌机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = ENEMY_IMAGE
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, WINDOW_WIDTH - self.rect.width)
self.rect.y = random.randint(-100, -40)
self.speed = random.randint(3, 6)
def update(self):
# 移动敌机
self.rect.y += self.speed
# 删除超出屏幕的敌机
if self.rect.top > WINDOW_HEIGHT:
self.kill()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = BULLET_IMAGE
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = -10
def update(self):
# 移动子弹
self.rect.y += self.speed
# 删除超出屏幕的子弹
if self.rect.bottom < 0:
self.kill()
# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# 创建玩家飞机
player = Player()
all_sprites.add(player)
# 设置帧率
clock = pygame.time.Clock()
FPS = 60
# 设置敌机生成间隔
ENEMY_INTERVAL = 60
# 设置得分
score = 0
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 生成敌机
if random.randint(1, ENEMY_INTERVAL) == 1:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
# 更新精灵组
all_sprites.update()
# 判断子弹是否击中敌机
hits = pygame.sprite.groupcollide(bullets, enemies, True, True)
for hit in hits:
score += 10
# 判断敌机是否撞到玩家飞机
hits = pygame.sprite.spritecollide(player, enemies, True)
if hits:
running = False
# 绘制游戏界面
WINDOW_SURFACE.blit(BACKGROUND_IMAGE, (0, 0))
all_sprites.draw(WINDOW_SURFACE)
score_text = FONT.render('Score: {}'.format(score), True, (255, 255, 255))
WINDOW_SURFACE.blit(score_text, (10, 10))
pygame.display.update()
# 控制帧率
clock.tick(FPS)
# 显示游戏结束界面
game_over_text = FONT.render('Game Over', True, (255, 0, 0))
game_over_rect = game_over_text.get_rect()
game_over_rect.centerx = WINDOW_WIDTH // 2
game_over_rect.centery = WINDOW_HEIGHT // 2
WINDOW_SURFACE.blit(game_over_text, game_over_rect)
pygame.display.update()
# 延迟3秒后退出游戏
pygame.time.delay(3000)
# 退出 Pygame
pygame.quit()
```
注意,这只是一个简单的示例,实际的游戏可能需要更多的功能和优化。
阅读全文