飞机大战Python
时间: 2023-12-14 11:33:23 浏览: 27
飞机大战是一款经典的游戏,下面是一个Python实现的飞机大战项目,具体内容如下:
1.导入必要的模块和常量定义
```python
import gc
import random
import pygame
# 常量定义
class Constants:
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
HERO_SPEED = 10
ENEMY_SPEED = 5
BULLET_SPEED = 12
ENEMY_INTERVAL = 50
HERO_HP = 3
ENEMY_HP = 1
```
2.创建玩家飞机精灵类
```python
class HeroPlane(pygame.sprite.Sprite):
def __init__(self, hero_surface, hero_rect, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = hero_surface
self.rect = hero_rect
self.rect.topleft = init_pos
self.speed = Constants.HERO_SPEED
self.bullets = pygame.sprite.Group()
self.hp = Constants.HERO_HP
def move(self, offset):
x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
if x < 0:
x = 0
elif x > Constants.SCREEN_WIDTH - self.rect.width:
x = Constants.SCREEN_WIDTH - self.rect.width
if y < 0:
y = 0
elif y > Constants.SCREEN_HEIGHT - self.rect.height:
y = Constants.SCREEN_HEIGHT - self.rect.height
self.rect.topleft = (x, y)
def shoot(self, bullet_surface):
bullet = Bullet(bullet_surface, self.rect.midtop)
self.bullets.add(bullet)
def hit(self):
self.hp -= 1
def reset(self):
self.hp = Constants.HERO_HP
self.bullets.empty()
```
3.创建敌机精灵类
```python
class EnemyPlane(pygame.sprite.Sprite):
def __init__(self, enemy_surface, enemy_rect, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_surface
self.rect = enemy_rect
self.rect.topleft = init_pos
self.speed = Constants.ENEMY_SPEED
self.hp = Constants.ENEMY_HP
def move(self):
self.rect.top += self.speed
def shoot(self, bullet_surface):
bullet = Bullet(bullet_surface, self.rect.midbottom, True)
return bullet
def hit(self):
self.hp -= 1
```
4.创建子弹精灵类
```python
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_surface, init_pos, is_enemy=False):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_surface
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.speed = Constants.BULLET_SPEED if not is_enemy else -Constants.BULLET_SPEED
def move(self):
self.rect.top += self.speed
def is_out_of_screen(self):
return self.rect.bottom < 0 or self.rect.top > Constants.SCREEN_HEIGHT
```
5.创建游戏主函数
```python
def main():
# 初始化
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT))
pygame.display.set_caption('飞机大战')
pygame.mixer.music.load('bgm.mp3')
pygame.mixer.music.play(-1, 0.0)
hero_surface = pygame.image.load('hero.png').convert_alpha()
hero_rect = hero_surface.get_rect()
enemy_surface = pygame.image.load('enemy.png').convert_alpha()
enemy_rect = enemy_surface.get_rect()
bullet_surface = pygame.image.load('bullet.png').convert_alpha()
clock = pygame.time.Clock()
hero = HeroPlane(hero_surface, hero_rect, ((Constants.SCREEN_WIDTH - hero_rect.width) // 2, Constants.SCREEN_HEIGHT - hero_rect.height - 50))
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
enemy_interval = Constants.ENEMY_INTERVAL
score = 0
font = pygame.font.Font(None, 36)
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
hero.shoot(bullet_surface)
# 移动玩家飞机
offset = pygame.key.get_pressed()
hero.move(offset)
# 移动敌机
enemy_interval -= 1
if enemy_interval == 0:
enemy_interval = Constants.ENEMY_INTERVAL
enemy = EnemyPlane(enemy_surface, enemy_rect, (random.randint(0, Constants.SCREEN_WIDTH - enemy_rect.width), -enemy_rect.height))
enemies.add(enemy)
for enemy in enemies:
enemy.move()
# 移动子弹
for bullet in bullets:
bullet.move()
# 碰撞检测
for enemy in enemies:
for bullet in hero.bullets:
if pygame.sprite.collide_rect(enemy, bullet):
enemy.hit()
hero.bullets.remove(bullet)
bullets.remove(bullet)
if enemy.hp == 0:
enemies.remove(enemy)
score += 1
for bullet in bullets:
if bullet.is_out_of_screen():
bullets.remove(bullet)
for enemy in enemies:
for bullet in enemy.bullets:
if pygame.sprite.collide_rect(hero, bullet):
hero.hit()
enemy.bullets.remove(bullet)
bullets.remove(bullet)
if hero.hp == 0:
pygame.quit()
sys.exit()
# 绘制屏幕
screen.fill((255, 255, 255))
screen.blit(hero_surface, hero.rect)
for enemy in enemies:
screen.blit(enemy_surface, enemy.rect)
bullet = enemy.shoot(bullet_surface)
if bullet:
enemy.bullets.add(bullet)
for bullet in hero.bullets:
screen.blit(bullet_surface, bullet.rect)
for bullet in bullets:
screen.blit(bullet_surface, bullet.rect)
score_text = font.render('Score: %d' % score, True, (0, 0, 0))
screen.blit(score_text, (10, 10))
hp_text = font.render('HP: %d' % hero.hp, True, (0, 0, 0))
screen.blit(hp_text, (Constants.SCREEN_WIDTH - hp_text.get_width() - 10, 10))
pygame.display.update()
# 控制帧率
clock.tick(60)
```