编写飞机大战,完成飞机爆炸功能。完成后相关代码如下:
1.搭建界面,主要完成窗口和背景图的显示
import pygame
import time
from pygame.locals import *
import random
"""
判定输赢,飞机爆炸
敌方子弹在我方飞机上,我方子弹在敌方飞机上。
我方子弹和敌方子弹碰撞
step1:在飞机基类中添加is_hit=False,用于表示飞机是否被击中
step2:在飞机基类中添加test(),用于检测
step3:在主函数中,显示我机下,调用test(),并增条件语句,如果被击中,替换图片
"""
飞机的基类
class BasePlan:
def init(self, screen, x, y, image):
self.x = x
self.y = y
self.screen = screen
self.image = pygame.image.load(image)
self.bullet_list = [] # 存储发射的子弹
self.is_hit = False # # 此标志用来表示飞机是否被击中了
def test(self, bullets):
# 检测飞机被击中,子弹处于飞机的上
for bullet in bullets:
if self.x < bullet.x <self.x+self.image.get_width() and self.y < bullet.y < self.y + self.image.get_height():
self.is_hit = True
def display(self):
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
# 判断子弹是否越界
if bullet.judge():
self.bullet_list.remove(bullet)
class HeroPlan(BasePlan):
def __init__(self, screen, image="./feiji/hero1.png"):
super().__init__(screen,190, 576, image)
def move_left(self):
self.x -= 5
def move_right(self):
self.x += 5
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
敌机
class EnemyPlan(BasePlan):
def __init__(self, screen,image="./feiji/enemy0.png"):
super().__init__(screen, 0, 0, image)
self.direction = "right" # 新增一个属性,表示敌机的方向
def move(self):
# 敌机移动
if self.direction == "right":
self.x += 5
else:
self.x -= 5
if self.x > 450:
self.direction = "left"
elif self.x < 0:
self.direction = "right"
def fire(self):
random_num = random.randint(1, 100)
if random_num == 10 or random_num == 20:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))
子弹的基类:
class BaseBullet:
def __init__(self, screen, x, y,image):
self.x = x
self.y = y
self.screen = screen
self.image = pygame.image.load(image)
def display(self):
self.screen.blit(self.image, (self.x, self.y))
我机发射的子弹
class Bullet(BaseBullet):
def __init__(self, screen, x, y,image="./feiji/bullet.png"):
super().__init__(screen, x+40,y-20,image)
def move(self):
self.y -= 10
def judge(self):
if self.y < 0:
return True
else:
return False
敌机发射的子弹
class EnemyBullet(BaseBullet):
def __init__(self, screen, x, y,image="./feiji/bullet1.png"):
super().__init__(screen, x + 15, y + 20, image)
def move(self):
self.y += 10
def judge(self):
if self.y > 700:
return True
else:
return False
def key_control(hero):
# 获取事件,比如按键等
for event in pygame.event.get():
# 判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
# 判断是否是按下了键
elif event.type == KEYDOWN:
# 检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero.move_left()
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero.move_right()
# 检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
# 发射子弹
hero.fire()
def main():
# 1.创建一个窗口,用来显示内容,窗口宽480,高800
scree = pygame.display.set_mode((480, 700), 0, 32)
# 2.创建一个和窗口大小的图片,用来当背景图
background = pygame.image.load("feiji/background.png")
# 3.把背景图放到窗口中显示
# 4.创建一个飞机图片
hero = HeroPlan(scree)
# 5.创建一个敌机
enemy = EnemyPlan(scree)
hero_nums = 0
enemy_nums = 0
while True:
# 设定需要显示的背景图
scree.blit(background, (0, 0)) # 背景图的左上角,和窗口左上角重合
hero.display()
# 添加检测我机是否被炸
hero.test(enemy.bullet_list)
if hero.is_hit:
hero_nums += 1
if hero_nums == 10:
hero.image = pygame.image.load("./feiji/hero_blowup_n1.png")
elif hero_nums == 20:
hero.image = pygame.image.load("./feiji/hero_blowup_n2.png")
elif hero_nums == 30:
hero.image = pygame.image.load("./feiji/hero_blowup_n3.png")
elif hero_nums == 40:
hero.image = pygame.image.load("./feiji/hero_blowup_n4.png")
elif hero_nums > 50:
break
enemy.display()
enemy.test(hero.bullet_list)
if enemy.is_hit:
enemy_nums += 1
if enemy_nums == 10:
enemy.image = pygame.image.load("./feiji/enemy0_down1.png")
elif enemy_nums == 20:
enemy.image = pygame.image.load("./feiji/enemy0_down2.png")
elif enemy_nums == 30:
enemy.image = pygame.image.load("./feiji/enemy0_down3.png")
elif enemy_nums == 40:
enemy.image = pygame.image.load("./feiji/enemy0_down4.png")
elif enemy_nums > 50:
enemy = EnemyPlan(scree)
enemy_nums = 0
enemy.move() # 调用敌机的移动方法
enemy.fire() # 敌机发火
# 更新需要显示的内容
pygame.display.update()
# 键盘事件
key_control(hero)
time.sleep(0.05)
if name == "main":
main()