# 飞机大战游戏规则:
# 玩家键盘的上下左右以及w、s、a、d键控制飞机的移动;空格键发出子弹;按b可自爆结束游戏。
# 蓝色降落伞图案为补给,可增长寿命;红色鱼雷图案为炸弹,可摧毁飞机
import pygame
from pygame.locals import *
import time
import random
# 定义的全局变量
goal = 0 # 玩家得分
life = 100 # 生命值
bb = False
class Hero(object):
#玩家英雄类
def __init__(self, screen_temp):
self.x = 210
self.y = 700
self.life = 20
self.image = pygame.image.load("飞机/hero1.png")
self.screen = screen_temp
self.bullet_list = [] # 用来存储子弹对象的引用
# 爆炸效果用的如下属性
self.hit = False # 表示是否要爆炸
self.bomb_list = [] # 存储爆炸时需要的图片
self.__create_images() # 调用这个方法向bomb_list中添加图片
self.image_num = 0 # 用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图然后清空,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0 # 用来记录当前要显示的爆炸效果的图片的序号
self.score = 0
def __create_images(self):
# 添加爆炸图片,形成动图效果
self.bomb_list.append(pygame.image.load("飞机/hero_blowup_n1.png"))
self.bomb_list.append(pygame.image.load("飞机/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("飞机/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image.load("飞机/hero_blowup_n4.png"))
def display(self,screen):
# 显示玩家的飞机
# 如果被击中,就显示爆炸效果,否则显示普通的飞机效果
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y)) # (self.x, self.y)是指当前英雄的位置
# blit方法 (一个对象,左上角位置)
self.image_num += 1
if self.image_num == 3:
self.image_num = 0
self.image_index += 1
if self.image_index > 3:
time.sleep(1)
self.image = pygame.image.load('飞机大战/失败.jpeg')
sound1 = pygame.mixer.Sound('飞机大战/游戏失败.mp3')
screen.blit(self.image, (0, 0))
sound1.play()
exit() # 调用exit让游戏退出
else:
if self.x < 0: # 控制英雄,不让它跑出界面
self.x = 0
elif self.x > 1100:
self.x = 1100
if self.y < 0:
self.y = 0
elif self.y > 650:
self.y = 650
self.screen.blit(self.image, (self.x, self.y)) # 这里是只要没有被打中,就一直是刚开始的样子
# 显示发射出去的子弹
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def move(self, move_x, move_y):
self.x += move_x
self.y += move_y
def fire(self):
# 通过创建一个子弹对象,完成发射子弹
bullet = Bullet(self.screen, self.x, self.y) # 创建一个子弹对象
self.bullet_list.append(bullet)
def bomb(self):
self.hit = True
def judge(self):
global life
if life <= 0:
self.bomb()
class Bullet(object):
# 玩家子弹类
def __init__(self, screen_temp, x_temp, y_temp):
self.x = x_temp + 40
self.y = y_temp - 20
self.image = pygame.image.load("飞机/bullet.png")
self.screen = screen_temp
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y -= 10
class Bullet_Enemy(object):
# 敌机子弹类
def __init__(self, screen_temp, x_temp, y_temp):
self.x = x_temp + 25
self.y = y_temp + 30
self.image = pygame.image.load("飞机/bullet1.png")
self.screen = screen_temp
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self, hero):
self.y += 10
global life
if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
life -= 10
print("---judge_enemy---")
return True
if life <= 0:
hero.bomb()
return False
class Base(object):
# 基类
def __init__(self, screen_temp, x, y, image_name):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load(image_name)
self.alive = True
def display(self):
if self.alive == True:
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y += 5
class bomb_bullet(Base):
# 炸弹类
def __init__(self, screen_temp):
Base.__init__(self, screen_temp, random.randint(45, 400), 0, "飞机/bomb.png")
def judge(self, hero,screen):
if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
self.alive = False
hero.bomb()
if self.y >= 750:
self.y = 0
self.x = random.randint(45, 1100)
class supply(Base):
# 补给类
def __init__(self, screen_temp):
Base.__init__(self, screen_temp, random.randint(45, 400), -300, "飞机/bomb-1.gif")
def judge(self, hero):
global life
if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
self.alive = False
life += 5
if self.y >= 750:
self.y = 0
self.x = random.randint(45, 1200)
self.alive = True
class EnemyPlane(object):
# 敌机类
def __init__(self, screen_temp):
self.x = random.randint(15, 480)
self.y = 0
self.image = pygame.image.load("飞机/enemy1.png")
self.screen = screen_temp
self.bullet_list = [] # 用来存储子弹对象的引用
self.hit = False
self.bomb_list = []
self.__create_images()
self.image_num = 0
self.image_index = 0
self.k = random.randint(1, 1200)
if self.k <= 600:
self.direction = "right"
elif self.k > 600:
self.direction = "left"
def display(self, hero):
# 显示敌人的飞机
if not self.hit:
self.screen.blit(self.image, (self.x, self.y))
else:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
self.image_num += 1
if self.image_num == 3 and self.image_index < 3:
self.image_num = 0
self.image_index += 1
for bullet in self.bullet_list:
bullet.display()
if (bullet.move(hero)):
self.bullet_list.remove(bullet)
def move(self):
# 利用随机数来控制飞机移动距离,以及移动范围
d1 = random.uniform(1, 10)
d2 = random.uniform(0.02, 3)
p1 = random.uniform(50, 100)
p2 = random.uniform(-20, 0)
if self.direction == "right":
self.x += d1
elif self.direction == "left":
self.x -= d1
if self.x > 1200 - p1:
self.direction = "left"
elif self.x < p2:
self.direction = "right"
self.y += d2
def bomb(self):
self.hit = True
def __create_images(self):
self.bomb_list.append(pygame.image.load("飞机/enemy1_down1.png"))
self.bomb_list.append(pygame.image.load("飞机/enemy1_down2.png"))
self.bomb_list.append(pygame.image.load("飞机/enemy1_down3.png"))
self.bomb_list.append(pygame.image.load("飞机/enemy1_down4.png"))
def fire(self):
# 控制敌机的开火,1/80的概率
s = random.randint(0, 500)
bullet1 = Bullet_Enemy(self.screen, self.x, self.y)
if s < 10:
self.bullet_list.append(bullet1)
class EnemyPlanes(EnemyPlane):
# 敌机群类 继承自EnemyPlane类
def __init__(self, screen_temp):
EnemyPlane.__init__(self, screen_temp)
self.num = 0
self.enemy_list = [] # 用列表存储产生的多架敌机
self.screen = screen_temp
def add_enemy(self, num):
# 产生多架敌机的函数
self.num = num
for i in range(num):
enemy = EnemyPlane(self.screen)
self.enemy_list.append(enemy)
def display(self, hero):
for i in range(self.num):
self.enemy_list[i].display(hero)
def move(self):
for i in range(self.num):
self.enemy_list[i].move()
def fire(self):
for i in range(self.num):
self.enemy_list[i].fire()
def judge1(hero, enemy):
# 判断敌机的炸毁
for bullet1 in hero.bullet_list:
if bullet1.y in range(int(enemy.y), int(enemy.y + 30)) and bullet1.x in range(int(enemy.x - 10),int(enemy.x + 50)):
hero.bullet_list.remove(bullet1)
enemy.bomb()
if bullet1.y < 0 or bullet1.x < 0 or bullet1.x > 1200: # 删除越界的玩家子弹
hero.bullet_list.remove(bullet1)
def clear_enemy(enemies):
# 清除敌机群类中被炸毁的敌机
global goal
for enemy in enemies.enemy_list:
if enemy.hit == True and enemy.image_index == 3:
enemies.enemy_list.remove(enemy)
enemies.num -= 1
goal += 1
if enemy.y >= 750:
enemies.enemy_list.remove(enemy)
enemies.num -= 1
def judge_num(enemies):
# 判断频幕上敌人的数量,如果为零,继续添加敌人
n = random.randint(1, 5)
if len(enemies.enemy_list) == 0:
enemies.add_enemy(n)
def creat_bomb(screen_temp):
bomb = bomb_bullet(screen_temp)
bomb_list = []
bomb_list.apend(bomb)
def show_score(screen):
#分数
font = pygame.font.Font('freesansbold.ttf', 32)
text = f"Score: {goal}"
score_render = font.render(text, True, (255, 0, 0))
screen.blit(score_render, (10, 700))
def show_life(screen):
#分数
font = pygame.font.Font('freesansbold.ttf', 32)
text = f"Life: {life}"
score_render = font.render(text, True, (255, 0, 0))
screen.blit(score_render, (10, 650))
#
# def over(screen):
# if bb:
# # over_font = pygame.font.Font('freesansbold.ttf', 64)
# # render = over_font.render("Game Over", True, (255, 0, 0))
# # screen.blit(render, (30, 500))
# img = pygame.image.load('飞机大战/失败.jpeg')
# sound1 = pygame.mixer.Sound('飞机大战/游戏失败.mp3')
# screen.blit(img,(30,500))
# sound1.play()
def main():
# 主函数执行:获取事件
move_x = 0
move_y = 0
pygame.init()
screen = pygame.display.set_mode((1200, 800), 0, 32)
background = pygame.image.load("飞机/bg.png")
pygame.mixer.music.load('飞机大战/bg.wav')
pygame.mixer.music.play(-1) # 单曲循环
pygame.mixer.Sound('飞机大战/子弹.mp3')
pygame.display.set_caption('飞机大战')
pygame.display.set_caption("飞机大战")
hero = Hero(screen) # 创建玩家飞机
enemis = EnemyPlanes(screen) # 创建敌机群
enemis.add_enemy(3)
bomb = bomb_bullet(screen) # 创建炸弹对象
supply0 = supply(screen) # 创建补给对象
left_key, right_key, up_key, down_key, done = 0, 0, 0, 0, 0
while True:
if done:
if done % 8 == 0:
done = 1
hero.fire()
else:
done += 1
for event in pygame.event.get():
if event.type == QUIT: # 判断是否是点击了退出按钮
print("exit")
exit()
if event.type == KEYDOWN: # 判断是否是按下了键
if event.key == K_a or event.key == K_LEFT:
move_x = -5
left_key += 1
elif event.key == K_d or event.key == K_RIGHT:
move_x = 5
right_key += 1
elif event.key == K_w or event.key == K_UP:
move_y = -5
up_key += 1
elif event.key == K_s or event.key == K_DOWN:
move_y = 5
down_key += 1
elif event.key == K_SPACE: # 检测按键是否是空格键
hero.fire()
done = 1
elif event.key == K_b:
print('b')
hero.bomb()
if event.type == KEYUP:
if event.key == K_a or event.key == K_LEFT:
left_key -= 1
if right_key == 0:
move_x = 0
else:
move_x = 5
if event.key == K_d or event.key == K_RIGHT:
right_key -= 1
if left_key == 0:
move_x = 0
else:
move_x = -5
if event.key == K_w or event.key == K_UP:
up_key -= 1
if down_key == 0:
move_y = 0
else:
move_y = 5
if event.key == K_s or event.key == K_DOWN:
down_key -= 1
if up_key == 0:
move_y = 0
else:
move_y = -5
if event.key == K_SPACE:
done = 0
screen.blit(background, (0, 0))
hero.move(move_x, move_y)
hero.display(screen)
hero.judge()
enemis.display(hero)
enemis.move()
enemis.fire()
bomb.display()
bomb.judge(hero,screen)
bomb.move()
supply0.display()
supply0.judge(hero)
supply0.move()
for i in range(enemis.num):
judge1(hero, enemis.enemy_list[i])
clear_enemy(enemis)
judge_num(enemis)
show_score(screen)
show_life(screen)
pygame.display.update()
if __name__ == "__main__":
main()
python-飞机大战
最新推荐文章于 2024-06-07 15:17:39 发布