From cabdf1654a41bb320288a2ea6d8b83324c0cfe06 Mon Sep 17 00:00:00 2001 From: 206530309 <932780660@qq.com> Date: Tue, 15 Jun 2021 15:33:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 半成.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 6 deletions(-) diff --git a/半成.py b/半成.py index 5361e3b..a8b2aa6 100644 --- a/半成.py +++ b/半成.py @@ -22,9 +22,88 @@ class Bullet(object): return True #返回true正确 else: return False #返回false错误 - - - - - - +#玩家飞机类 +class Aircraft_obj(object): + #构造方法,初始化飞机对象的属性 + def __init__(self, screen_temp): + self.x = 190 #飞起起始x坐标 + self.y = 708 #飞机起始y坐标 + self.screen = screen_temp #窗口 + self.image = pygame.image.load("C:/work/youxi/1.png") #创建一个飞机图片 + self.bullet_list = [] #存储发射出去的子弹对象 +#显示飞机图片的方法(这里包括了显示子弹的图片) + 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) #删除子弹 + #飞机左移方法 + def move_left(self): + if self.x < 10: #x坐标小于10(移动距离) + pass #不做任何事 + else: + self.x -= 10 #X坐标自减少10 + #飞机右移方法 + def move_right(self): + if self.x > 480-100-10: #X坐标大于(窗口宽度-飞机宽度-移动距离)的值 + pass #不做任何事 + else: + self.x += 10 #坐标自增加10 + #存储发射子弹的方法 + def fire(self): + self.bullet_list.append(Bullet(self.screen, self.x, self.y))#将发射的子弹对象存储到bullet_list中 +#敌机类 +class EnemyPlane(object): + #构造方法,初始化敌机对象的属性 + def __init__(self, screen_temp): + self.x = 0 #敌机的起始x坐标 + self.y = 0 #敌机的起始y坐标 + self.screen = screen_temp #窗口 + self.image = pygame.image.load("C:/work/youxi/2.png") #创建一个敌机图片 + self.direction = "right" #用来存储飞机移动方向,默认向右移动 + #爆炸效果用的属性 + self.hit = False #表示是否要爆炸 + self.bomb_lists = [] #用来存储爆炸时需要的图片 + self.__crate_images() #调用这个方法向bomb_lists中添加图片 + self.image_num = 0 #用来记录while循环的次数,当次数达到一定值时才显示一张爆炸的图,然后清空 + self.image_index = 0 #用来记录当前要显示的爆炸效果的图片的序号 + #敌机移动方法 + def move(self): + if self.hit == True: #如被击中,敌机不移动 + pass + else: + if self.direction=="right": #如果是向右移动 + self.x += 5 #x坐标自增加5 + elif self.direction=="left": #如果是向左移动 + self.x -= 5 #x坐标自减少5 + if self.x>480-50: #如x坐标大于窗口减去敌机宽度的值 + self.direction = "left" #移动方向改为左 + elif self.x<0: #如果x坐标小于0 + self.direction = "right" #移动方向改为右 + #将爆炸需要的图片添加到self.bomb_lists中 + def __crate_images(self): + self.bomb_lists.append(pygame.image.load("C:/work/youxi/zha.png")) + self.bomb_lists.append(pygame.image.load("C:/work/youxi/zha.png")) + self.bomb_lists.append(pygame.image.load("C:/work/youxi/zha.png")) + self.bomb_lists.append(pygame.image.load("C:/work/youxi/zha.png")) + #判断爆炸的方法,x1为子弹最左侧的横坐标,x2为子弹最右侧的横坐标,y为子弹当前的纵坐标 + def blast(self,x1,x2,y): + #判断子弹能命中敌机的三种情况(51是敌机图片的宽度,39是敌机图片的高度) + if ((x1>=self.x and x2<=self.x+51) or x2==self.x or x1==self.x+51) and y<39: + self.hit = True + def display(self): #显示敌机的方法 + #如果被击中,就显示爆炸效果,否则显示普通的飞机效果 + if self.hit == True: #如果满足爆炸条件,就显示爆炸的图片 + self.screen.blit(self.bomb_lists[self.image_index], (self.x, self.y)) + self.image_num+=1 #统计循环次数,为了使玩家看清爆炸效果 + if self.image_num == 7: #如果循环次数达到7次 + self.image_num=0 #将循环次数改为0次 + self.image_index+=1 #图片显示序号加1,换为另一张图 + if self.image_index>3: #如果图片序号大于3(一共4张图片) + time.sleep(1) #暂停一秒 + exit() #调用exit退出游戏 + else: #否则显示正常的敌机图片 + self.screen.blit(self.image,(self.x, self.y)) \ No newline at end of file