利用pygame模块实现飞机大战附加功能如下: (1) 实现碰撞检测(可以消灭敌机,也可以被敌机消灭) (2) 随机出现云朵在天空中漂浮 (3) 随机出现甜甜圈进行能量补给(生命力或子弹类型发生变化) (4) 有GAME OVER条件控制处理
时间: 2023-12-27 18:02:07 浏览: 139
好的,下面是代码实现。
首先导入必要的模块和库:
```python
import pygame
import random
import math
```
定义一些常量:
```python
# 窗口大小
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 700
# 飞机大小
PLAYER_WIDTH = 50
PLAYER_HEIGHT = 64
# 敌机大小
ENEMY_WIDTH = 50
ENEMY_HEIGHT = 60
# 子弹大小
BULLET_WIDTH = 9
BULLET_HEIGHT = 21
# 甜甜圈大小
SUPPLY_WIDTH = 52
SUPPLY_HEIGHT = 52
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 字体
FONT_SMALL = 'fonts/font.ttf'
FONT_MEDIUM = 'fonts/font.ttf'
FONT_LARGE = 'fonts/font.ttf'
# 音效
SOUND_BULLET = 'sounds/bullet.wav'
SOUND_SUPPLY = 'sounds/supply.wav'
SOUND_BOOM = 'sounds/boom.wav'
SOUND_GAME_OVER = 'sounds/gameover.wav'
```
初始化 Pygame:
```python
pygame.init()
pygame.mixer.init()
```
创建窗口:
```python
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('飞机大战')
```
定义游戏对象的基类:
```python
class GameObject(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, image_path):
super().__init__()
self.x = x
self.y = y
self.width = width
self.height = height
self.image_path = image_path
self.image = pygame.image.load(image_path).convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def move(self, dx, dy):
self.x += dx
self.y += dy
self.rect.x = self.x
self.rect.y = self.y
def draw(self, screen):
screen.blit(self.image, self.rect)
```
定义玩家飞机类:
```python
class Player(GameObject):
def __init__(self, x, y):
super().__init__(x, y, PLAYER_WIDTH, PLAYER_HEIGHT, 'images/player.png')
self.speed = 8
self.bullet_type = 0
self.bullet_speed = 10
self.bullet_count = 1
self.bullet_interval = 0.3
self.last_bullet_time = pygame.time.get_ticks()
self.life = 3
def move_up(self):
if self.y > 0:
self.move(0, -self.speed)
def move_down(self):
if self.y < WINDOW_HEIGHT - self.height:
self.move(0, self.speed)
def move_left(self):
if self.x > 0:
self.move(-self.speed, 0)
def move_right(self):
if self.x < WINDOW_WIDTH - self.width:
self.move(self.speed, 0)
def shoot(self):
if pygame.time.get_ticks() - self.last_bullet_time >= self.bullet_interval * 1000:
if self.bullet_type == 0:
bullet = Bullet(self.x + self.width / 2 - BULLET_WIDTH / 2, self.y - BULLET_HEIGHT, self.bullet_speed)
bullets.add(bullet)
elif self.bullet_type == 1:
bullet1 = Bullet(self.x + self.width / 4 - BULLET_WIDTH / 2, self.y - BULLET_HEIGHT, self.bullet_speed)
bullet2 = Bullet(self.x + self.width / 4 * 3 - BULLET_WIDTH / 2, self.y - BULLET_HEIGHT, self.bullet_speed)
bullets.add(bullet1)
bullets.add(bullet2)
elif self.bullet_type == 2:
bullet1 = Bullet(self.x + self.width / 2 - BULLET_WIDTH / 2, self.y - BULLET_HEIGHT, self.bullet_speed)
bullet2 = Bullet(self.x + self.width / 4 - BULLET_WIDTH / 2, self.y - BULLET_HEIGHT, self.bullet_speed)
bullet3 = Bullet(self.x + self.width / 4 * 3 - BULLET_WIDTH / 2, self.y - BULLET_HEIGHT, self.bullet_speed)
bullets.add(bullet1)
bullets.add(bullet2)
bullets.add(bullet3)
self.last_bullet_time = pygame.time.get_ticks()
shoot_sound.play()
def hit(self):
self.life -= 1
if self.life <= 0:
game_over()
```
定义敌机类:
```python
class Enemy(GameObject):
def __init__(self, x, y, speed):
super().__init__(x, y, ENEMY_WIDTH, ENEMY_HEIGHT, 'images/enemy.png')
self.speed = speed
def move(self):
self.move(0, self.speed)
def hit(self):
boom_sound.play()
self.kill()
```
定义子弹类:
```python
class Bullet(GameObject):
def __init__(self, x, y, speed):
super().__init__(x, y, BULLET_WIDTH, BULLET_HEIGHT, 'images/bullet.png')
self.speed = speed
def move(self):
self.move(0, -self.speed)
def hit(self):
self.kill()
```
定义甜甜圈类:
```python
class Supply(GameObject):
def __init__(self, x, y):
super().__init__(x, y, SUPPLY_WIDTH, SUPPLY_HEIGHT, 'images/supply.png')
self.speed = 5
self.type = random.randint(0, 1)
def move(self):
self.move(0, self.speed)
def hit(self):
supply_sound.play()
self.kill()
```
定义游戏结束函数:
```python
def game_over():
game_over_sound.play()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.fill(BLACK)
game_over_image = pygame.image.load('images/gameover.png').convert_alpha()
screen.blit(game_over_image, (WINDOW_WIDTH / 2 - game_over_image.get_width() / 2, WINDOW_HEIGHT / 2 - game_over_image.get_height() / 2))
pygame.display.update()
```
创建玩家飞机、子弹、敌机和甜甜圈的精灵组:
```python
player = Player(WINDOW_WIDTH / 2 - PLAYER_WIDTH / 2, WINDOW_HEIGHT - PLAYER_HEIGHT - 20)
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
supplies = pygame.sprite.Group()
```
定义游戏循环:
```python
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_UP:
player.move_up()
elif event.key == pygame.K_DOWN:
player.move_down()
elif event.key == pygame.K_LEFT:
player.move_left()
elif event.key == pygame.K_RIGHT:
player.move_right()
elif event.key == pygame.K_SPACE:
player.shoot()
# 生成敌机
if random.randint(1, 50) == 1:
enemy = Enemy(random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH), -ENEMY_HEIGHT, random.randint(3, 6))
enemies.add(enemy)
# 生成甜甜圈
if random.randint(1, 200) == 1:
supply = Supply(random.randint(0, WINDOW_WIDTH - SUPPLY_WIDTH), -SUPPLY_HEIGHT)
supplies.add(supply)
# 更新玩家飞机、子弹、敌机和甜甜圈的位置
player_group = pygame.sprite.Group(player)
player_group.update()
bullets.update()
enemies.update()
supplies.update()
# 碰撞检测
for enemy in pygame.sprite.spritecollide(player, enemies, True):
player.hit()
for bullet in pygame.sprite.groupcollide(bullets, enemies, True, True):
bullet.hit()
for supply in pygame.sprite.spritecollide(player, supplies, True):
if supply.type == 0:
player.life += 1
elif supply.type == 1:
player.bullet_type = random.randint(0, 2)
supply.hit()
# 绘制背景
screen.fill(WHITE)
cloud_image = pygame.image.load('images/cloud.png').convert_alpha()
for i in range(3):
screen.blit(cloud_image, (random.randint(0, WINDOW_WIDTH - cloud_image.get_width()), random.randint(-200, 0)))
pygame.draw.line(screen, BLACK, (0, WINDOW_HEIGHT - 20), (WINDOW_WIDTH, WINDOW_HEIGHT - 20), 1)
# 绘制玩家飞机、子弹、敌机和甜甜圈
player_group.draw(screen)
bullets.draw(screen)
enemies.draw(screen)
supplies.draw(screen)
# 绘制生命值和子弹类型
font = pygame.font.Font(FONT_SMALL, 20)
text = font.render('Life: {}'.format(player.life), True, BLACK)
screen.blit(text, (10, 10))
text = font.render('Bullet: {}'.format(player.bullet_type), True, BLACK)
screen.blit(text, (10, 30))
# 更新屏幕
pygame.display.update()
# 退出游戏
pygame.quit()
```
完整代码:
阅读全文