飞机大战游戏代码python
时间: 2024-06-21 11:03:47 浏览: 292
飞机大战是一款经典的2D射击游戏,如果您想了解其Python代码实现,可以参考以下主要功能实现:
1. 游戏窗口和背景:使用Pygame库创建游戏窗口,并在其中加载背景图片。
2. 玩家飞机:定义玩家飞机类,并实现其移动、射击、碰撞检测等功能。
3. 敌方飞机:定义敌方飞机类,并实现其移动、射击、碰撞检测等功能。
4. 游戏控制:实现游戏开始、结束、重置等控制功能。
下面是一个简单的示例代码:
```
import pygame
from pygame.locals import *
import random
# 初始化Pygame库
pygame.init()
# 创建游戏窗口
screen_width = 480
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('飞机大战')
# 加载背景图片
bg = pygame.image.load('images/background.png').convert()
# 定义玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/hero.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left = (screen_width - self.rect.width) // 2
self.rect.top = screen_height - self.rect.height - 50
self.speed = 6
self.bullets = pygame.sprite.Group()
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
def move_down(self):
if self.rect.bottom < screen_height - 50:
self.rect.top += self.speed
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
def move_right(self):
if self.rect.right < screen_width:
self.rect.left += self.speed
def shoot(self):
bullet = Bullet(self.rect.left + self.rect.width // 2, self.rect.top)
self.bullets.add(bullet)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/bullet.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left = x - self.rect.width // 2
self.rect.top = y - self.rect.height
self.speed = 10
def update(self):
self.rect.top -= self.speed
if self.rect.bottom < 0:
self.kill()
# 定义敌方飞机类
class Enemy(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/enemy.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left = random.randint(0, screen_width - self.rect.width)
self.rect.bottom = 0
self.speed = random.randint(3, 6)
def update(self):
self.rect.top += self.speed
if self.rect.top > screen_height:
self.kill()
# 创建玩家飞机对象
player = Player()
# 创建敌方飞机组对象
enemies = pygame.sprite.Group()
# 设置帧率和游戏状态
clock = pygame.time.Clock()
game_running = True
while game_running:
# 处理事件
for event in pygame.event.get():
if event.type == QUIT:
game_running = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
game_running = False
elif event.key == K_UP:
player.move_up()
elif event.key == K_DOWN:
player.move_down()
elif event.key == K_LEFT:
player.move_left()
elif event.key == K_RIGHT:
player.move_right()
elif event.key == K_SPACE:
player.shoot()
# 添加敌方飞机
if random.randint(1, 60) == 1:
enemy = Enemy()
enemies.add(enemy)
# 更新玩家飞机和子弹
player.bullets.update()
# 更新敌方飞机
enemies.update()
# 碰撞检测
for enemy in pygame.sprite.spritecollide(player, enemies, True):
game_running = False
for bullet in pygame.sprite.groupcollide(player.bullets, enemies, True, True):
pass
# 绘制游戏窗口
screen.blit(bg, (0, 0))
screen.blit(player.image, player.rect)
player.bullets.draw(screen)
enemies.draw(screen)
# 刷新游戏窗口
pygame.display.update()
# 控制帧率
clock.tick(60)
# 退出游戏
pygame.quit()
```
阅读全文