From 7115b0630265e53312b95d7f57053eb53ca49f70 Mon Sep 17 00:00:00 2001 From: 206530307 <2916911646@qq.com> Date: Mon, 14 Jun 2021 21:46:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'2.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2.py | 102 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/2.py b/2.py index 5361e3b..0a1fc59 100644 --- a/2.py +++ b/2.py @@ -1,30 +1,72 @@ -import pygame #导入pygame模块 -from pygame.locals import * #导入pygame.locals模块 -import time #导入time模块 - -#子弹类 -class Bullet(object): - #构造方法,用于初始化子弹对象的属性 - def __init__(self, screen_temp, x, y): - self.x = x+40 #子弹起始x坐标 - self.y = y-20 #子弹起始y坐标 - self.screen = screen_temp #窗口 - self.image = pygame.image.load("C:/work/youxi/zidan.png") #创建一个子弹图片 - #显示子弹图片的方法 - def display(self): - self.screen.blit(self.image, (self.x, self.y)) #显示子弹图片 - #子弹移动方法 - def move(self): - self.y-=10 #子弹y坐标自减10 - #判断子弹是否越界的方法 - def judge(self): - if self.y<0: #如果子弹的y坐标小于0 - return True #返回true正确 - else: - return False #返回false错误 - - - - - - +import pygame #导入pygame模块 +from pygame.locals import * #导入pygame.locals模块 +import time #导入time模块 + +#子弹类 +class Bullet(object): + #构造方法,用于初始化子弹对象的属性 + def __init__(self, screen_temp, x, y): + self.x = x+40 #子弹起始x坐标 + self.y = y-20 #子弹起始y坐标 + self.screen = screen_temp #窗口 + self.image = pygame.image.load("C:/work/youxi/zidan.png") #创建一个子弹图片 + #显示子弹图片的方法 + def display(self): + self.screen.blit(self.image, (self.x, self.y)) #显示子弹图片 + #子弹移动方法 + def move(self): + self.y-=10 #子弹y坐标自减10 + #判断子弹是否越界的方法 + def judge(self): + if self.y<0: #如果子弹的y坐标小于0 + 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 #用来记录当前要显示的爆炸效果的图片的序号 \ No newline at end of file