用python写一个飞机大战
时间: 2024-05-29 20:03:39 浏览: 111
好的,下面是一个简单的飞机大战游戏的代码实现。请注意,此代码仅用于学习和参考目的,可能存在一些不足之处。
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 创建游戏窗口
win_width = 480
win_height = 700
window = pygame.display.set_mode((win_width, win_height))
# 设置游戏标题
pygame.display.set_caption("飞机大战")
# 加载游戏资源
bg_img = pygame.image.load("images/background.png")
player_img = pygame.image.load("images/player.png")
enemy_img = pygame.image.load("images/enemy.png")
bullet_img = pygame.image.load("images/bullet.png")
boom_img = pygame.image.load("images/boom.png")
# 设置游戏音效
bullet_sound = pygame.mixer.Sound("sounds/bullet.wav")
boom_sound = pygame.mixer.Sound("sounds/boom.wav")
bg_music = pygame.mixer.music.load("sounds/bg_music.mp3")
pygame.mixer.music.play(-1)
# 设置游戏字体
font_score = pygame.font.SysFont("Arial", 30, True)
font_gameover = pygame.font.SysFont("Arial", 60, True)
# 设置游戏变量
score = 0
player_speed = 5
enemy_speed = 3
bullet_speed = 7
bullet_interval = 10
enemy_interval = 50
enemies = []
# 定义游戏角色类
class Actor:
def __init__(self, img, x, y, speed):
self.img = img
self.x = x
self.y = y
self.speed = speed
self.width = img.get_width()
self.height = img.get_height()
def draw(self):
window.blit(self.img, (self.x, self.y))
def move_left(self):
self.x -= self.speed
if self.x < 0:
self.x = 0
def move_right(self):
self.x += self.speed
if self.x > win_width - self.width:
self.x = win_width - self.width
# 定义子弹类
class Bullet:
def __init__(self, img, x, y, speed):
self.img = img
self.x = x
self.y = y
self.speed = speed
self.width = img.get_width()
self.height = img.get_height()
def draw(self):
window.blit(self.img, (self.x, self.y))
def move(self):
self.y -= self.speed
if self.y < -self.height:
return True
return False
# 定义敌机类
class Enemy:
def __init__(self, img, x, y, speed):
self.img = img
self.x = x
self.y = y
self.speed = speed
self.width = img.get_width()
self.height = img.get_height()
self.hp = 1
def draw(self):
window.blit(self.img, (self.x, self.y))
def move(self):
self.y += self.speed
if self.y > win_height:
return True
return False
def hit(self):
self.hp -= 1
if self.hp <= 0:
boom_sound.play()
enemies.remove(self)
return True
return False
# 创建角色和子弹
player = Actor(player_img, win_width // 2 - player_img.get_width() // 2, win_height - player_img.get_height(), player_speed)
bullets = []
# 定义游戏结束函数
def game_over():
text_gameover = font_gameover.render("Game Over", True, (255, 255, 255))
window.blit(text_gameover, (win_width // 2 - text_gameover.get_width() // 2, win_height // 2 - text_gameover.get_height() // 2))
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
# 游戏循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if len(bullets) < bullet_interval:
bullets.append(Bullet(bullet_img, player.x + player.width // 2 - bullet_img.get_width() // 2, player.y, bullet_speed))
bullet_sound.play()
# 渲染游戏背景
window.blit(bg_img, (0, 0))
# 渲染游戏分数
text_score = font_score.render("Score: " + str(score), True, (255, 255, 255))
window.blit(text_score, (10, 10))
# 渲染角色和子弹
player.draw()
for bullet in bullets:
bullet.draw()
if bullet.move():
bullets.remove(bullet)
# 创建敌机
if random.randint(0, enemy_interval) == 0:
enemies.append(Enemy(enemy_img, random.randint(0, win_width - enemy_img.get_width()), -enemy_img.get_height(), enemy_speed))
# 渲染敌机
for enemy in enemies:
enemy.draw()
if enemy.move():
game_over()
for bullet in bullets:
if bullet.x + bullet.width > enemy.x and bullet.x < enemy.x + enemy.width and bullet.y + bullet.height > enemy.y and bullet.y < enemy.y + enemy.height:
if enemy.hit():
score += 1
if bullet in bullets:
bullets.remove(bullet)
# 处理角色移动
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move_left()
elif keys[pygame.K_RIGHT]:
player.move_right()
# 更新游戏窗口
pygame.display.update()
```
这里只是一个简单的实现,你可以根据自己的需求和想法进行扩展和优化。
阅读全文