用python使用pygame写飞机大战的小游戏
时间: 2024-05-24 15:05:13 浏览: 89
好的,我可以帮你回答这个问题。首先需要安装pygame模块,可以在命令行中输入 pip install pygame 进行安装。然后,使用下面的示例代码,可以实现一个基础版的飞机大战游戏:
```
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏界面大小、背景颜色
screen_width = 480
screen_height = 700
bg_color = pygame.Color('gray')
# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption('飞机大战')
# 加载背景图
bg = pygame.image.load('images/background.png')
# 加载玩家飞机图片
player_img = pygame.image.load('images/player.png')
player_rect = player_img.get_rect(center=(screen_width//2, screen_height-100))
# 加载子弹图片
bullet_img = pygame.image.load('images/bullet.png')
# 加载敌机图片
enemy_img1 = pygame.image.load('images/enemy1.png')
enemy_img2 = pygame.image.load('images/enemy2.png')
enemy_img3 = pygame.image.load('images/enemy3.png')
enemy_imgs = [enemy_img1, enemy_img2, enemy_img3]
# 飞机、子弹及敌机列表
player = pygame.sprite.GroupSingle()
player.add(pygame.sprite.Sprite())
player.sprite.image = player_img
player.sprite.rect = player_rect
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
# 创建敌机
for i in range(6):
enemy = pygame.sprite.Sprite()
enemy.image = random.choice(enemy_imgs)
enemy.rect = enemy.image.get_rect(
center=(random.randint(30, screen_width-30), random.randint(-100, -50)))
enemies.add(enemy)
# 设置帧率
clock = pygame.time.Clock()
# 定义游戏循环
while True:
# 事件检测
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 玩家移动操作
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player.sprite.rect.move_ip(0, -5)
if keys[pygame.K_DOWN]:
player.sprite.rect.move_ip(0, 5)
if keys[pygame.K_LEFT]:
player.sprite.rect.move_ip(-5, 0)
if keys[pygame.K_RIGHT]:
player.sprite.rect.move_ip(5, 0)
# 玩家边界限制
if player.sprite.rect.left <= 0:
player.sprite.rect.left = 0
if player.sprite.rect.right >= screen_width:
player.sprite.rect.right = screen_width
if player.sprite.rect.top <= 0:
player.sprite.rect.top = 0
if player.sprite.rect.bottom >= screen_height:
player.sprite.rect.bottom = screen_height
# 子弹发射操作
if keys[pygame.K_SPACE]:
bullet = pygame.sprite.Sprite()
bullet.image = bullet_img
bullet.rect = bullet.image.get_rect(center=player.sprite.rect.midtop)
bullets.add(bullet)
# 子弹移动
for bullet in bullets:
bullet.rect.move_ip(0, -5)
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
# 敌机移动
for enemy in enemies:
enemy.rect.move_ip(0, 3)
if enemy.rect.top >= screen_height:
enemy.kill()
enemy = pygame.sprite.Sprite()
enemy.image = random.choice(enemy_imgs)
enemy.rect = enemy.image.get_rect(
center=(random.randint(30, screen_width-30), random.randint(-100, -50)))
enemies.add(enemy)
# 子弹击中敌机
for enemy in pygame.sprite.groupcollide(enemies, bullets, True, True):
enemy = pygame.sprite.Sprite()
enemy.image = random.choice(enemy_imgs)
enemy.rect = enemy.image.get_rect(
center=(random.randint(30, screen_width-30), random.randint(-100, -50)))
enemies.add(enemy)
# 玩家与敌机碰撞检测
if pygame.sprite.spritecollide(player.sprite, enemies, True):
pygame.quit()
exit()
# 游戏界面显示
screen.blit(bg, (0,0))
player.draw(screen)
bullets.draw(screen)
enemies.draw(screen)
pygame.display.flip()
# 设置帧率
clock.tick(60)
```
希望以上代码对你有所帮助!
阅读全文