用python写一个飞机大战的小游戏
时间: 2023-06-12 14:02:12 浏览: 203
好的,以下是一个简单的飞机大战小游戏的Python代码,希望对你有所帮助:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义游戏窗口大小
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 600
WINDOW_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# 定义游戏窗口标题
pygame.display.set_caption('飞机大战')
# 加载背景图
BG_IMG = pygame.image.load('images/bg.png')
BG_IMG = pygame.transform.scale(BG_IMG, WINDOW_SIZE)
# 加载玩家飞机图
PLAYER_IMG = pygame.image.load('images/me.png')
PLAYER_IMG = pygame.transform.scale(PLAYER_IMG, (50, 50))
PLAYER_SPEED = 5
# 加载敌机图
ENEMY_IMG = pygame.image.load('images/enemy.png')
ENEMY_IMG = pygame.transform.scale(ENEMY_IMG, (50, 50))
ENEMY_SPEED = 3
# 加载子弹图
BULLET_IMG = pygame.image.load('images/bullet.png')
BULLET_IMG = pygame.transform.scale(BULLET_IMG, (10, 20))
BULLET_SPEED = 10
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = PLAYER_IMG
self.rect = self.image.get_rect()
self.rect.x = WINDOW_WIDTH / 2 - self.rect.width / 2
self.rect.y = WINDOW_HEIGHT - self.rect.height - 20
self.speed = PLAYER_SPEED
def move_left(self):
if self.rect.x > 0:
self.rect.x -= self.speed
def move_right(self):
if self.rect.x < WINDOW_WIDTH - self.rect.width:
self.rect.x += self.speed
def move_up(self):
if self.rect.y > 0:
self.rect.y -= self.speed
def move_down(self):
if self.rect.y < WINDOW_HEIGHT - self.rect.height:
self.rect.y += self.speed
def fire(self, bullets):
bullet = Bullet(self.rect.x + self.rect.width / 2 - BULLET_IMG.get_width() / 2, self.rect.y)
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, WINDOW_WIDTH - self.rect.width)
self.rect.y = -self.rect.height
self.speed = ENEMY_SPEED
def update(self):
self.rect.y += self.speed
if self.rect.y > WINDOW_HEIGHT:
self.kill()
# 定义子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = BULLET_IMG
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = BULLET_SPEED
def update(self):
self.rect.y -= self.speed
if self.rect.y < 0:
self.kill()
# 初始化玩家飞机和敌机组
player = Player()
enemies = pygame.sprite.Group()
# 初始化子弹组
bullets = pygame.sprite.Group()
# 创建Pygame时钟对象
clock = pygame.time.Clock()
# 创建Pygame显示窗口
screen = pygame.display.set_mode(WINDOW_SIZE)
# 游戏循环
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_LEFT] or keys[pygame.K_a]:
player.move_left()
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
player.move_right()
if keys[pygame.K_UP] or keys[pygame.K_w]:
player.move_up()
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
player.move_down()
if keys[pygame.K_SPACE]:
player.fire(bullets)
# 添加敌机
if random.randint(0, 100) < 5:
enemy = Enemy()
enemies.add(enemy)
# 更新玩家飞机和敌机组
enemies.update()
# 检测碰撞
for enemy in enemies:
if pygame.sprite.collide_rect(player, enemy):
pygame.quit()
exit()
for bullet in bullets:
if pygame.sprite.collide_rect(bullet, enemy):
enemy.kill()
bullet.kill()
# 更新子弹组
bullets.update()
# 画背景
screen.blit(BG_IMG, (0, 0))
# 画玩家飞机
screen.blit(player.image, player.rect)
# 画敌机
for enemy in enemies:
screen.blit(enemy.image, enemy.rect)
# 画子弹
for bullet in bullets:
screen.blit(bullet.image, bullet.rect)
# 更新屏幕
pygame.display.update()
# 设置帧率
clock.tick(60)
```
你可以根据自己的需要修改代码中的图像、速度、分数等参数,以及添加更多的游戏功能。
阅读全文