请给我用python 写一个飞机大战的游戏代码 标好注释
时间: 2024-04-06 14:34:42 浏览: 69
当然,下面是用Python实现的飞机大战游戏代码,注释已经添加在代码中,用于解释代码的不同部分的含义和功能。
```python
# 导入所需的模块
import pygame
import random
# 初始化pygame模块
pygame.init()
# 设置游戏窗口大小和标题
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("飞机大战")
# 加载背景图片和飞机图片
background = pygame.image.load("background.png").convert()
player = pygame.image.load("player.png").convert_alpha()
enemy = pygame.image.load("enemy.png").convert_alpha()
# 设置帧率和字体
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 24)
# 定义常量
player_speed = 5
enemy_speed = 3
bullet_speed = 7
bullet_interval = 250 # 子弹发射间隔时间(单位:毫秒)
# 定义游戏对象类
class GameObject:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
def draw(self):
screen.blit(self.image, (self.x, self.y))
def collide(self, other):
x_distance = abs(self.x - other.x)
y_distance = abs(self.y - other.y)
if x_distance < (self.image.get_width() + other.image.get_width()) / 2 and y_distance < (self.image.get_height() + other.image.get_height()) / 2:
return True
else:
return False
# 定义玩家类
class Player(GameObject):
def move_left(self):
if self.x > 0:
self.x -= player_speed
def move_right(self):
if self.x < width - self.image.get_width():
self.x += player_speed
def shoot(self):
bullet = Bullet(self.x + self.image.get_width() / 2, self.y)
bullets.add(bullet)
# 定义敌机类
class Enemy(GameObject):
def move(self):
self.y += enemy_speed
def out_of_screen(self):
return self.y > height
# 定义子弹类
class Bullet(GameObject):
def move(self):
self.y -= bullet_speed
def out_of_screen(self):
return self.y < -self.image.get_height()
# 创建玩家对象和敌机对象的集合
player = Player(width / 2 - player.get_width() / 2, height - player.get_height(), player)
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# 定义游戏主循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if 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()
# 更新游戏对象状态
for enemy in enemies:
enemy.move()
if enemy.out_of_screen():
enemies.remove(enemy)
for bullet in bullets:
bullet.move()
if bullet.out_of_screen():
bullets.remove(bullet)
# 判断游戏对象碰撞
for enemy in enemies:
if player.collide(enemy):
pygame.quit()
quit()
for bullet in bullets:
if bullet.collide(enemy):
enemies.remove(enemy)
bullets.remove(bullet)
# 创建敌机
if random.randint(1, 100) == 1:
enemy = Enemy(random.randint(0, width - enemy.get_width()), -enemy.get_height(), enemy)
enemies.add(enemy)
# 绘制游戏画面
screen.blit(background, (0, 0))
player.draw()
for enemy in enemies:
enemy.draw()
for bullet in bullets:
bullet.draw()
# 绘制游戏分数
score_text = font.render("Score: " + str(len(bullets)), True, (255, 255, 255))
screen.blit(score_text, (10, 10))
# 更新游戏画面
pygame.display.update()
# 控制游戏帧率
clock.tick(60)
```
以上是飞机大战游戏的Python实现代码,其中实现了玩家、敌机、子弹等游戏对象的定义和操作,以及游戏画面的绘制、碰撞检测等功能。需要注意的是,这只是一个简单的示例代码,实际的游戏代码需要更加复杂和完善。
阅读全文