编写飞机大战,完成后相关代码如下:
"""
是否击中
1.敌机被我机的子弹击中
2.我机被敌机的子弹击中
敌机,
"""
import pygame
from pygame.locals import *
import time
import random
class BasePlane:
def init(self, scree,x,y, image_name):
self.x = x
self.y = y
self.scree = scree
self.image = pygame.image.load(image_name)
# 表示我机发射出的子弹
self.bullet_list = [] # 存储我机发射出的子弹,用于判断是否击中敌机
self.is_hit = False # 用于标记飞机是否被打中,默认是False
# 新增功能,表示本机是否被敌机的子弹击中
def test_hit(self, bullets_list):
for bullet in bullets_list:
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.scree.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 HeroPlane(BasePlane):
def __init__(self, scree, image_name="./feiji/hero1.png"):
super().__init__(scree,190,576,image_name)
# 飞机左移
def move_left(self):
self.x -= 5
# 飞机右移
def move_right(self):
self.x += 5
# 我机发射子弹
def fire(self):
self.bullet_list.append(Bullet(self.scree, self.x, self.y))
子弹的基类:
class BaseBullet:
def init(self, scree,x ,y, image_name):
self.x = x
self.y = y
self.scree = scree
self.image = pygame.image.load(image_name)
# 用于显示子弹
def display(self):
self.scree.blit(self.image, (self.x, self.y)) # 显示我机
创建我机的子弹
class Bullet(BaseBullet):
def __init__(self, scree,x ,y):
super().__init__(scree,x+40,y-20,image_name="./feiji/bullet.png")
# 子弹向上移动
def move(self):
self.y -= 20
# 判断子弹的 y值,是否超越边界
def judge(self):
if self.y < 0:
return True
else:
return False
敌机类:
class EnemyPlane(BasePlane):
def __init__(self, scree, image_name="./feiji/enemy0.png"):
super().__init__(scree, 0, 0, image_name)
self.direction = "right" # 该属性,用于控制敌机的方法
# 敌机移动
def move(self):
if self.direction == "right":
self.x += 5
elif self.direction == "left":
self.x -= 5
if self.x >= 480 -50:
self.direction = "left"
if self.x <= 0:
self.direction = "right"
# 敌机机发射子弹
def fire(self):
# 产生一个随机数:
ran_num = random.randint(1,100)
if ran_num == 50 or ran_num == 30:
self.bullet_list.append(EnemyBullet(self.scree, self.x, self.y))
创建敌机的子弹
class EnemyBullet(BaseBullet):
def __init__(self, scree,x ,y):
super().__init__(scree, x + 25, y +40, image_name="./feiji/bullet1.png")
# 子弹向下移动
def move(self):
self.y += 20
# 判断子弹的 y值,是否超越边界
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,高852
scree = pygame.display.set_mode((480, 700), 0, 32)
# 2.创建一个和窗口大小的图片,用来当背景图
background = pygame.image.load("./feiji/background.png")
# 创建我机
hero = HeroPlane(scree)
# 创建敌机
enemy = EnemyPlane(scree)
# 3.把背景图放到窗口中显示
hero_num = 0
enemy_num = 0
while True:
# 设定需要显示的背景图
scree.blit(background, (0, 0)) # 背景图的左上角,和窗口左上角重合
# 显示我机
hero.display()
# 检测我机是否被炸
hero.test_hit(enemy.bullet_list)
if hero.is_hit:
hero_num += 1
if hero_num == 10:
hero.image = pygame.image.load("./feiji/hero_blowup_n1.png")
elif hero_num == 20:
hero.image = pygame.image.load("./feiji/hero_blowup_n2.png")
elif hero_num == 30:
hero.image = pygame.image.load("./feiji/hero_blowup_n3.png")
elif hero_num == 40:
hero.image = pygame.image.load("./feiji/hero_blowup_n4.png")
elif hero_num == 50:
# 结束程序
break
enemy.display() # 显示敌机
# 检测敌机是否被炸
enemy.test_hit(hero.bullet_list)
if enemy.is_hit:
enemy_num += 1
if enemy_num == 10:
enemy.image = pygame.image.load("./feiji/enemy0_down1.png")
elif enemy_num == 20:
enemy.image = pygame.image.load("./feiji/enemy0_down2.png")
elif enemy_num == 30:
enemy.image = pygame.image.load("./feiji/enemy0_down3.png")
elif enemy_num == 40:
enemy.image = pygame.image.load("./feiji/enemy0_down4.png")
elif enemy_num == 50:
enemy = EnemyPlane(scree)
enemy_num = 0
enemy.move() # 敌机移动
enemy.fire() # 敌机发射子弹
key_control(hero)
# 更新需要显示的内容
pygame.display.update()
time.sleep(0.05)
if name == 'main':
main()