用Python写一个飞机大战游戏
时间: 2024-03-30 12:27:21 浏览: 79
用Python写一个飞机大战小游戏
3星 · 编辑精心推荐
好的,下面是一个简单的飞机大战游戏的Python代码:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置窗口大小和标题
WIDTH, HEIGHT = 480, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("飞机大战")
# 加载图像
bg_img = 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()
# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 设置字体
font = pygame.font.SysFont(None, 30)
# 定义游戏对象类
class GameObject(pygame.sprite.Sprite):
def __init__(self, img, x, y, speed):
pygame.sprite.Sprite.__init__(self)
self.image = img
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = speed
def update(self):
self.rect.y += self.speed
# 定义玩家类
class Player(GameObject):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
self.score = 0
def shoot(self):
bullet = Bullet(bullet_img, self.rect.centerx, self.rect.top, -10)
all_sprites.add(bullet)
bullets.add(bullet)
# 定义敌人类
class Enemy(GameObject):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
def update(self):
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speed = random.randrange(1, 8)
player.score += 1
# 定义子弹类
class Bullet(GameObject):
def __init__(self, img, x, y, speed):
super().__init__(img, x, y, speed)
def update(self):
self.rect.y += self.speed
if self.rect.bottom < 0:
self.kill()
# 创建游戏对象
player = Player(player_img, WIDTH/2, HEIGHT-100, 0)
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
# 设置敌人数量
NUM_ENEMIES = 10
# 创建敌人
for i in range(NUM_ENEMIES):
enemy = Enemy(enemy_img, random.randrange(WIDTH - enemy_img.get_width()), random.randrange(-1500, -100), random.randrange(1, 8))
enemies.add(enemy)
all_sprites.add(enemy)
# 创建时钟对象
clock = pygame.time.Clock()
# 游戏循环
running = True
while running:
# 设置帧率
clock.tick(60)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
# 移动玩家
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.rect.x -= 5
if keys[pygame.K_RIGHT]:
player.rect.x += 5
# 更新游戏对象
all_sprites.update()
# 检测敌人和子弹是否碰撞
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
enemy = Enemy(enemy_img, random.randrange(WIDTH - enemy_img.get_width()), random.randrange(-1500, -100), random.randrange(1, 8))
enemies.add(enemy)
all_sprites.add(enemy)
# 检测玩家和敌人是否碰撞
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
running = False
# 绘制游戏界面
screen.blit(bg_img, (0, 0))
all_sprites.draw(screen)
score_text = font.render("Score: {}".format(player.score), True, WHITE)
screen.blit(score_text, (10, 10))
pygame.display.update()
# 退出Pygame
pygame.quit()
```
你可以将上述代码保存在一个名为“plane_game.py”的文件中,并在与图片文件夹同一目录下新建一个名为“images”的文件夹,将游戏所需的图片文件放入其中,然后运行该文件,即可开始游戏。
阅读全文