import pygame # 引用第三方模块
import random # 随机产生数据
import time
from pygame.locals import *
'''
抽象出来一个飞机的基类
'''
class BasePlane(object):
def __init__(self, screen, imageName):
'''
:param screen: 主窗体对象
:param imageName: 加载的图片
'''
self.screen = screen
self.image = pygame.image.load(imageName)
self.bullet_list = [] # 存储的子弹
pass
def display(self):
self.screen.blit(self.image, (self.x, self.y))
# 完善子弹的展示逻辑
needDel = []
for item in self.bullet_list:
if item.judge():
needDel.append(item)
pass
# 删除越界子弹
for i in needDel:
self.bullet_list.remove(i)
pass
for bullet in self.bullet_list:
bullet.display() # 显示子弹位置
bullet.move()
pass
'''
抽象出一个子弹基类
'''
class ComBullet(object):
def __init__(self, x, y, screen, bulletType):
self.x = x
self.y = y
self.type = bulletType
self.screen = screen
if self.type == 'hero':
self.x += 13
self.y -= 20
self.imagePath = 'D:\图片\omoro_alien_05.png'
elif self.type == 'enemy':
self.x = x
self.y += 10
self.imagePath = 'D:\图片\omoro_alien_05.png'
self.imageName = pygame.image.load(self.imagePath)
def move(self):
'''
子弹移动
:return:
'''
if self.type == 'hero':
self.y -= 5
elif self.type == 'enemy':
self.y += 5
def display(self):
self.screen.blit(self.imageName, (self.x, self.y))
def judge(self):
'''
判断子弹是否越界
:return:
'''
if self.y > 500 or self.y < 0:
return True
else:
return False
'''
1.实现飞机的显示,并且可以控制飞机的移动【面向对象】
'''
class HeroPlane(BasePlane):
def __init__(self, screen):
'''
:param screen: 主窗体对象
'''
super(HeroPlane, self).__init__(screen, 'D:\图片\person_kintaro_01.png') # 调用父类构造方法
self.x = 150
self.y = 450
pass
def move_left(self):
'''
飞机左移
:return:
'''
if self.x > 0:
self.x -= 10
pass
def move_right(self):
'''
飞机右移
:return:
'''
if self.x < 350 - 40: # 因为图片占据40个像素
self.x += 10
pass
# 发射子弹
def shoot_bullet(self):
# 创建一个新的子弹对象
new_bullet = ComBullet(self.x, self.y, self.screen, 'hero')
self.bullet_list.append(new_bullet)
pass
'''
创建敌机类
'''
class EnemyPlane(BasePlane):
def __init__(self, screen):
super(EnemyPlane, self).__init__(screen, 'D:\图片\omoro_alien_06.png')
self.x = 0
self.y = 0
self.direction = 'right'
def shebullet(self):
'''
敌机随机发射子弹
:return:
'''
num = random.randint(1, 8)
if num == 3:
newBullet = ComBullet(self.x, self.y, self.screen, 'enemy')
self.bullet_list.append(newBullet)
def move(self):
'''
随机移动
:return:
'''
if self.direction == 'right':
self.x += 10
elif self.direction == 'left':
self.x -= 10
if self.x >= 350 - 20:
self.direction = 'left'
elif self.x < 0:
self.direction = 'right'
def key_control(HeroObj):
'''
定义普通函数,用来实现键盘检测
:param HeroObj: 可控制的对象
:return:
'''
# 获取键盘事件
eventList = pygame.event.get()
for event in eventList:
if event.type == QUIT: # 程序退出
print('退出')
exit()
elif event.type == KEYDOWN: # 是否按下按键
if event.key == K_a or event.key == K_LEFT:
HeroObj.move_left() # 调用函数实现左移动
print('飞机左移')
elif event.key == K_d or event.key == K_RIGHT:
HeroObj.move_right()
print('飞机右移')
elif event.key == K_SPACE:
HeroObj.shoot_bullet()
print('发射子弹')
def main():
# 首先创建一个窗口,用来显示内容
screen = pygame.display.set_mode((350, 500), depth=32) # 设定窗口大小
# 设定背景图片
background = pygame.image.load('D:\图片\preview (1).png') # 加载本地图片
# 设置游戏标题
pygame.display.set_caption('阶段总结,飞机大战')
# 载入玩家对象
hero = HeroPlane(screen)
# 创建敌机对象
enemyplane = EnemyPlane(screen)
while (True):
# 设定要显示的内容
screen.blit(background, (0, 0))
# 显示玩家图片
hero.display()
# 显示敌机
enemyplane.display()
# 敌机移动
enemyplane.move()
# 敌机发射
enemyplane.shebullet()
# 获取键盘事件
key_control(hero)
# 更新显示的内容
pygame.display.update()
pygame.time.Clock().tick(5) # 一秒内循环5次
pass
if __name__ == '__main__':
main()
标签:__,飞机,bullet,screen,大战,pygame,self,def From: https://www.cnblogs.com/xuexipythondaydayup/p/17909558.html