Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法让我的代码更容易理解和简化?_Python_Optimization_Pygame - Fatal编程技术网

Python 有没有办法让我的代码更容易理解和简化?

Python 有没有办法让我的代码更容易理解和简化?,python,optimization,pygame,Python,Optimization,Pygame,我想知道是否有一种方法可以优化我代码的这一部分。关于路径系统和重画部分。我发现很难坚持到底。即使把整个事情改变成一种更容易理解的形式也适合我。多谢各位 class wolf(object): #creating the wolf class wolf_right = ['pics/WR.png'] + ['pics/WR' + str(i) + '.png' for i in range(2, 18)] #identifies the right wolf pics wolf_

我想知道是否有一种方法可以优化我代码的这一部分。关于路径系统和重画部分。我发现很难坚持到底。即使把整个事情改变成一种更容易理解的形式也适合我。多谢各位

class wolf(object): #creating the wolf class

    wolf_right = ['pics/WR.png'] + ['pics/WR' + str(i) + '.png' for i in range(2, 18)] #identifies the right wolf pics
    wolf_left = ['pics/WL.png'] + ['pics/WL' + str(i) + '.png' for i in range(2, 18)] #identifies the left wolf pics

    run_right = [pg.transform.smoothscale(pg.image.load(img), (280,160)) for img in wolf_right] #loads the right wolf pics
    run_left  = [pg.transform.smoothscale(pg.image.load(img), (280,160)) for img in wolf_left] #load the left wolf pics

    def __init__(self, x, y, width, height, finish): #initialising the object wolf
        self.x = x #x coord of wolf
        self.y = y #y coord of wolf
        self.width = width #width of wolf
        self.height = height #height of wolf
        self.path = [x, finish]  # This part here determines the movement limits of the wolf, back and forth
        self.run_distance = 0 #wolf is initially did not move
        self.velocity = 9 #speed of wolf moving
        self.collision_box = (self.x + 60, self.y, 280, 160)#the arguments inside are the coordinates designating the sides of the box
                                                            #and then the width and the height of the box


    def pathing(self): #pathing system
        if self.velocity > 0:  # If wolf moving to the right
            if self.x < self.path[1] + self.velocity: #ensures that it keeps moving if wolf is not at finish
                self.x += self.velocity #allows wolf to move

            else: #if the finish is reached then go backwards
                self.velocity = self.velocity * -1 #where velocity goes negative
                                                   #according to displacement, a particle with -ve velocity goes backwards
                self.x += self.velocity #allows wolf to move


        else: # If wold is going to the left
            if self.x > self.path[0] - self.velocity: #ensures that it keeps moving if the wolf is not at finish
                self.x += self.velocity #allows wolf to move

            else: #if the finish is reached then go backwards
                self.velocity = self.velocity * -1 #where velocity goes negative
                                                   #according to displacement, a particle with -ve velocity goes backwards
                self.x += self.velocity #allows wolf to move

    def got_hit(self): #function if the wolf takes damage from the ninja
        print("Congrats, you have hit the wolf!")


    def redraw(self, win):#just like for the ninja we do the same steps
        self.pathing()
        if self.run_distance + 1 >= 51: #This time I am running 17 sprites thus, 17 * 3 (where 3 sprites per second)
            self.run_distance = 0

        if self.velocity < 0: #if velocity is increasing meaning movement, links left images with left movement
            win.blit(self.run_left[self.run_distance//3], (self.x,self.y))
            self.run_distance += 1

        else:  #else linking right images with right movement
            win.blit(self.run_right[self.run_distance//3], (self.x,self.y))
            self.run_distance += 1


        #pg.draw.rect(win, (0,200,0), self.collision_box,2) #this will draw a green box around the wolf of lines thickness 2
        self.collision_box = (self.x + 50 , self.y, 200, 150) # ensures the box is drawn and is updated alongside motion
class-wolf(对象):#创建wolf类
wolf_right=['pics/WR.png']+['pics/WR'+str(i)+'.png'表示范围(2,18)内的i]#标识右wolf图片
wolf_left=['pics/WL.png']+['pics/WL'+str(i)+'.png'表示范围(2,18)内的i]#标识左wolf图片
右运行=[pg.transform.smoothscale(pg.image.load(img),(280160))用于wolf中的img\u right]#加载右wolf图片
在wolf中运行img左=[pg.transform.smoothscale(pg.image.load(img),(280160)]#加载左wolf图片
定义初始化(self、x、y、宽度、高度、光洁度):#初始化对象
self.x=x#x狼的合作伙伴
self.y=y#y狼的坐标
self.width=宽度#狼的宽度
self.height=身高#狼的身高
self.path=[x,finish]#这部分决定了狼来回的移动限制
self.run_distance=0#wolf最初没有移动
self.velocity=9#狼的移动速度
self.collision_box=(self.x+60,self.y,280,160)#里面的参数是指定框边的坐标
#然后是盒子的宽度和高度
def路径(自):#路径系统
如果self.velocity>0:#如果wolf向右移动
如果self.xself.path[0]-self.velocity:#确保它在狼未到达终点时继续移动
self.x+=self.velocity允许wolf移动
否则:#如果达到终点,则后退
self.velocity=self.velocity*-1#其中速度为负值
#根据位移,速度为-ve的粒子向后移动
self.x+=self.velocity允许wolf移动
def命中(自身):#如果狼受到忍者的伤害,则该功能可用
打印(“恭喜,你击中了狼!”)
def redraw(self,win):#就像忍者一样,我们也采取同样的步骤
self.pathing()
如果self.run_distance+1>=51:#这次我运行17个精灵,因此,17*3(其中每秒3个精灵)
self.run\u距离=0
如果self.velocity<0:#如果速度增加意味着移动,则将左图像与左移动链接
win.blit(self.run\u left[self.run\u distance//3],(self.x,self.y))
自我跑步距离+=1
else:#else将正确的图像与正确的移动链接起来
win.blit(self.run\u right[self.run\u distance//3],(self.x,self.y))
自我跑步距离+=1
#pg.draw.rect(win,(0200,0),self.collision_box,2)#这将在线厚2的狼周围绘制一个绿色框
self.collision_box=(self.x+50,self.y,200150)#确保绘制框并与运动一起更新

代码没有那么复杂,但这是我的看法。我通读了一遍,很容易理解。只有一件事,
self.x+=self.velocity
是在
pathing
中的4种情况的末尾完成的。只需在路径设置的末尾执行一次即可,而不是在每种情况下单独执行。在
重画
中,可以对self.run\u distance+=1执行类似操作:

class-wolf(对象):#创建wolf类
# [...]
def路径(自):#路径系统
如果self.velocity>0且self.x>=self.path[1]+self.velocity或\
self.velocity<0和self.x=51:#这次我运行17个精灵,因此,17*3(其中每秒3个精灵)
self.run\u距离=0
如果self.velocity<0,则run\u surf=self.run\u left,否则self.run\u right
win.blit(run\u surf[self.run\u distance//3],(self.x,self.y))
自我跑步距离+=1
#pg.draw.rect(win,(0200,0),self.collision_box,2)#这将在线厚2的狼周围绘制一个绿色框
self.collision_box=(self.x+50,self.y,200150)#确保绘制框并与运动一起更新

无论如何,在pygame中,它打算使用,并且。
每个精灵都应该具有属性
.rect
.image
以及方法。精灵应包含在组中。可以绘制()和更新()组。
这使得代码易于阅读、简短、易于理解和扩展。e、 g.:
(通常应使用大写字母约定。)

狼类(pygame.sprite.sprite):
wolf_right=['pics/WR.png']+['pics/WR'+str(i)+'.png'表示范围(2,18)内的i]#标识右wolf图片
wolf_left=['pics/WL.png']+['pics/WL'+str(i)+'.png'表示范围(2,18)内的i]#标识左wolf图片
右运行=[pg.transform.smoothscale(pg.image.load(img),(280160))用于wolf中的img\u right]#加载右wolf图片
在wolf中运行img左=[pg.transform.smoothscale(pg.image.load(img),(280160)]#加载左wolf图片
定义初始(自我、x、y、完成):
超级()
self.image=run_left[0]
self.rect=pygame.rect(x,y,280,160)