介绍:使用pygame包开发的一个简易飞机大战游戏,主要用来熟悉python的面向对象编程。
Python代码:
飞机大战.py
import random
import pygame
import time
class BasePlane(object):
def __init__(self, screen_temp, x, y, image_name, hp):
self.x = x
self.y = y
self.hp = hp
self.screen = screen_temp
self.image = pygame.image.load(image_name)
self.bullet_list = [] # 存储发射出去的子弹对象引用
def display(self):
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.dispaly()
bullet.move()
if bullet.judge(): # 判断子弹是否越界
self.bullet_list.remove(bullet)
class HeroPlane(BasePlane):
def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 210, 600, "./feiji/hero1.png", 1000)
# 爆炸效果用的如下属性
self.hit = False # 表示是否要爆炸
self.bomb_list = [] # 用来存储爆炸时需要的图片
self.__crate_images() # 调用这个方法向bomb_list中添加图片
self.image_num = 0 # 用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0 # 用来记录当前要显示的爆炸效果的图片的序号
def display(self):
# 如果被击中,就显示爆炸效果,否则显示普通的飞机效果
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
self.image_num += 1
if self.image_num == 7:
self.image_num = 0
self.image_index += 1
if self.image_index > 3:
time.sleep(1)
print("你挂了...")
exit() # 调用exit让游戏退出
# self.image_index = 0
else:
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.dispaly()
bullet.move()
if bullet.judge(): # 判断子弹是否越界
self.bullet_list.remove(bullet)
def bomb(self):
showBulletVoice("./feiji/died.wav")
self.hit = True
def __crate_images(self):
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))
def move_left(self, param):
self.x -= param
def move_right(self, param):
self.x += param
def get_x(self):
return self.x
# def move_up(self, param):
# self.y -= param
#
# def move_down(self, param):
# self.y += param
def fire(self, enemy_temp, three):
self.bullet_list.append(Bullet(self.screen, self.x, self.y, enemy_temp))
if three:
self.bullet_list.append(Bullet(self.screen, self.x + 30, self.y + 40, enemy_temp))
self.bullet_list.append(Bullet(self.screen, self.x - 30, self.y + 40, enemy_temp))
class EnemyPlane(BasePlane):
"""敌机类"""
def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 0, 0, "./feiji/enemy0.png", 1000)
self.direction = "right" # 用来存储飞机默认的显示方向
# 爆炸效果用的如下属性
self.hit = False # 表示是否要爆炸
self.bomb_list = [] # 用来存储爆炸时需要的图片
self.__crate_images() # 调用这个方法向bomb_list中添加图片
self.image_num = 0 # 用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0 # 用来记录当前要显示的爆炸效果的图片的序号
def display(self):
# 如果被击中,就显示爆炸效果,否则显示普通的飞机效果
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
self.image_num += 1
if self.image_num == 7:
self.image_num = 0
self.image_index += 1
if self.image_index > 3:
time.sleep(1)
print("敌机已被击毁...")
exit() # 调用exit让游戏退出
# self.image_index = 0
else:
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.dispaly()
bullet.move()
if bullet.judge(): # 判断子弹是否越界
self.bullet_list.remove(bullet)
def bomb(self):
showBulletVoice("./feiji/died.wav")
self.hit = True
def __crate_images(self):
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down1.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down2.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down3.png"))
self.bomb_list.append(pygame.image.load("./feiji/enemy0_down4.png"))
def get_x(self):
return self.x
def move(self):
if self.direction == "right":
self.x += 5
else:
self.x -= 5
if self.x > 430:
self.direction = "left"
elif self.x < 0:
self.direction = "right"
def fire(self, hero):
random_num = random.randint(1, 100)
if random_num <= 10:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y, hero))
class BaseBullet(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)
def dispaly(self):
self.screen.blit(self.image, (self.x, self.y))
class Bullet(BaseBullet):
def __init__(self, screen_temp, x, y, enemy_temp):
BaseBullet.__init__(self, screen_temp, x + 40, y - 20, "./feiji/bullet.png")
self.enemy = enemy_temp
def move(self):
self.y -= 10
if self.y < 40:
if self.enemy.get_x() <= self.x <= self.enemy.get_x() + 51:
self.enemy.hp -= 1
print("敌机被击中,HP(%i/1000)" % self.enemy.hp)
showBulletVoice("./feiji/enemy.wav")
if self.enemy.hp <= 0:
self.enemy.bomb()
def judge(self):
if self.y < 0:
return True
else:
return False
class EnemyBullet(BaseBullet):
def __init__(self, screen_temp, x, y, hero__temp):
BaseBullet.__init__(self, screen_temp, x + 25, y + 40, "./feiji/bullet1.png")
self.hero = hero__temp
def move(self):
self.y += 10
if self.y > 620:
if self.hero.get_x() <= self.x <= self.hero.get_x() + 100:
self.hero.hp -= 1
print("hero被击中,HP(%i/1000)" % self.hero.hp)
showBulletVoice("./feiji/enemy.wav")
if self.hero.hp <= 0:
self.hero.bomb()
def judge(self):
if self.y > 650:
return True
else:
return False
def key_control(hero_temp, enemy_temp):
# 获取事件,比如按键等
for event in pygame.event.get():
# 判断是否是点击了退出按钮
if event.type == pygame.QUIT:
print("exit")
exit()
# 判断是否是按下了键
elif event.type == pygame.KEYDOWN:
# 检测按键是否是a或者left
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
print('left')
hero_temp.move_left(20)
# 检测按键是否是d或者right
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
print('right')
hero_temp.move_right(20)
elif event.key == pygame.K_w or event.key == pygame.K_UP:
print('up')
# hero_temp.move_up(20)
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
print('down')
# hero_temp.move_down(20)
# 检测按键是否是空格键
elif event.key == pygame.K_SPACE:
print('space')
showBulletVoice("./feiji/hero.wav")
hero_temp.fire(enemy_temp, False)
elif event.key == pygame.K_x:
print('x')
showBulletVoice("./feiji/hero.wav")
hero_temp.fire(enemy_temp, True)
def showHp(screen_temp, hp_temp, x, y):
pygame.init()
myfont = pygame.font.Font(None, 40)
red = 255, 0, 0
textImage = myfont.render("HP:%i/1000" % hp_temp, True, red)
screen_temp.blit(textImage, (x, y))
def showBulletVoice(voice):
pygame.mixer.init()
pygame.mixer.music.load(voice)
pygame.mixer.music.play()
def main():
# 1.创建一个窗口,又来显示内容
screen = pygame.display.set_mode((480, 852), 0, 32)
# 2.创建一个和窗口大小的图片,用来充当背景
background = pygame.image.load("./feiji/background.png")
# 3.创建一个飞机对象
hero = HeroPlane(screen)
# 4.创建一个敌机
enemy = EnemyPlane(screen)
while True:
# 设定需要显示的背景图
screen.blit(background, (0, 0))
hero.display()
showHp(screen, hero.hp, 0, 580)
enemy.display()
showHp(screen, enemy.hp, 0, 39)
enemy.move()
enemy.fire(hero) # 敌机开火
# 更新需要显示的内容
pygame.display.update()
key_control(hero, enemy)
time.sleep(0.01)
if __name__ == '__main__':
main()
全部代码及资源包已免费分享至资源信息中,大家可以学习下。
程序可能存在部分bug,欢迎交流指正。