pygame飞机大战代码
时间: 2023-08-02 21:09:56 浏览: 115
以下是一个简单的Pygame飞机大战游戏的示例代码:
```python
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("飞机大战")
# 加载背景图片
background = pygame.image.load('background.png')
# 加载玩家飞机图片
player_img = pygame.image.load('player.png')
player_width = player_img.get_width()
player_height = player_img.get_height()
# 加载敌机图片
enemy_img = pygame.image.load('enemy.png')
enemy_width = enemy_img.get_width()
enemy_height = enemy_img.get_height()
# 加载子弹图片
bullet_img = pygame.image.load('bullet.png')
bullet_width = bullet_img.get_width()
bullet_height = bullet_img.get_height()
# 定义颜色变量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 定义玩家飞机初始位置
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - player_height - 20
# 定义敌机列表和初始位置
enemies = []
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = -enemy_height
# 定义子弹列表和初始位置
bullets = []
bullet_x = 0
bullet_y = player_y
# 定义玩家分数
score = 0
# 创建游戏时钟
clock = pygame.time.Clock()
# 定义玩家飞机移动速度
player_speed = 5
# 定义敌机移动速度
enemy_speed = 3
# 定义子弹移动速度
bullet_speed = 7
# 定义游戏状态
game_running = True
while game_running:
# 检测游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# 创建子弹
bullet_x = player_x + player_width // 2 - bullet_width // 2
bullets.append([bullet_x, bullet_y])
# 移动玩家飞机
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if player_x < 0:
player_x = 0
if keys[pygame.K_RIGHT]:
player_x += player_speed
if player_x > screen_width - player_width:
player_x = screen_width - player_width
# 移动敌机
enemy_y += enemy_speed
if enemy_y > screen_height:
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = -enemy_height
score += 1
# 移动子弹
for bullet in bullets:
bullet[1] -= bullet_speed
if bullet[1] < 0:
bullets.remove(bullet)
# 碰撞检测
for bullet in bullets:
if bullet_x < enemy_x + enemy_width and bullet_x + bullet_width > enemy_x and bullet_y < enemy_y + enemy_height and bullet_y + bullet_height > enemy_y:
bullets.remove(bullet)
enemy_x = random.randint(0, screen_width - enemy_width)
enemy_y = -enemy_height
score += 10
if player_x < enemy_x + enemy_width and player_x + player_width > enemy_x and player_y < enemy_y + enemy_height and player_y + player_height > enemy_y:
game_running = False
# 填充背景颜色
screen.fill(WHITE)
# 绘制背景图片
screen.blit(background, (0, 0))
# 绘制玩家飞机
screen.blit(player_img, (player_x, player_y))
# 绘制敌机
screen.blit(enemy_img, (enemy_x, enemy_y))
# 绘制子弹
for bullet in bullets:
screen.blit(bullet_img, (bullet[0], bullet[1]))
# 绘制分数
font = pygame.font.Font(None, 36)
score_text = font.render("Score: " + str(score), True, BLACK)
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 设置游戏帧率
clock.tick(60)
# 关闭Pygame
pygame.quit()
```
请注意,在运行此代码之前,你需要准备好游戏所需的背景图片、玩家飞机图片、敌机图片和子弹图片,并将其与代码文件放在同一目录下。这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。祝你玩得愉快!
阅读全文