首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化吡咯

优化吡咯
EN

Stack Overflow用户
提问于 2022-01-13 13:00:51
回答 1查看 54关注 0票数 0

在我的AH计算项目中,我正在重新创建我的Nidhogg版本。一切都进展顺利,直到血开始喷出来。我不太确定如何提高代码的效率,因为我对python还很陌生。

这是喷血的班级:

代码语言:javascript
复制
class bloodmaker():
def __init__(self,xv,yv,colour,x,y):
    self.gravity = 2
    self.air_resistance = 0.25
    self.xv = xv
    self.yv = yv
    self.x = x
    self.y = y
    self.pastx = 0
    self.pasty = 0
    self.colour = colour
def move(self):
    if self.y < 400:
        self.pastx = self.x
        self.pasty = self.y
        #so that it doesnt curve backwards
        if self.xv > 0:
            self.xv -= self.air_resistance
        self.yv += self.gravity
        
        self.x += self.xv
        self.y += self.yv
        #so that the drawn line doesnt go over the edge
        if self.y > 400:
            self.y = 400
    
        if self.colour is "o":
            py.draw.line(screen, (255, 165, 0), (self.pastx-backgroundx, self.pasty), (self.x-backgroundx, self.y),5)
        else:
            py.draw.line(screen, (255, 255, 0), (self.pastx-backgroundx, self.pasty), (self.x-backgroundx, self.y),5)
    else:
        global bloodgrid
        try:
            #the bloodgrid are squares 5 pixels wide, covering the bottom section, so we we divide by 5 to find where to put the blood
            bloodgrid[int(self.x/5)].insert(0,self.colour)
        except:
            pass
        #deleting this object as it is no longer required
        return True

这是一张喷血的图片(请原谅不完整的精灵) 1:https://i.stack.imgur.com/hXiAa.png

在下面有一层血,它使用一组堆栈工作,当血液到达地板时,这是上面的代码添加的。

代码语言:javascript
复制
bloodgrid = [[] for x in range(512)]

这是一个代码,用来摧毁飞行中的血液物体,并在屏幕上闪动血液地板。

代码语言:javascript
复制
def blood():
    for i in range(len(bloodarray)):
        killyourself = bloodarray[i].move()
        if killyourself is True:
            kill.append(i)
            #appends to a kill list as if i popped here the list would get shorter while the for loop stays the same giving an out of index error
    for i in range(len(kill)):
        bloodarray.pop(kill[0]-i)
        kill.pop(0)

    #printing the entire bloodgrid
        
    for i in range(512):
        for ii in range(len(bloodgrid[i])):
            try:
                if bloodgrid[i][ii] is "o":
                    py.draw.rect(screen, (255, 165, 0), ((i*5)-backgroundx, ii*5+400, 5, 5))
                else:
                    py.draw.rect(screen, (255, 255, 0), ((i*5)-backgroundx, ii*5+400, 5, 5))
            except:
                pass

我不认为只更新屏幕的一部分是不可能的,因为相机在移动,地板上的血迹也在移动。

随着更多的血液积聚在地板上,游戏的框架开始下降,当血液喷出时,它会变得特别的不稳定。有什么办法能让这个更有效率吗?我不想交一个波涛汹涌的游戏,但我真的很喜欢血的样子。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-01-13 17:42:27

有几点希望是有帮助的。

  1. (这与加速代码没有直接关系,但是.)尽量避免使用全局变量。把它们传递给你的职能。您发布的几个变量很难找到,因为您正在访问函数范围之外的变量。

  1. 的第一个原因是:您使用insert()在列表的开头插入新元素,并且在调用move()函数的循环中这样做。在列表中插入任何位置(除了末尾)都非常缓慢,原因超出了这篇文章的范围。您应该考虑在其中放置一个来自dequecollections,它可以有效地插入前面或后面。如果这令人望而生畏,您还可以“做一些数学计算”,并将其插入列表的末尾,然后从最后一个元素中倒排,这也是有效的。

  1. 也在相同的概念中,正如注释中提到的,从列表或数组中间提取pop的速度也非常慢。找出一种不同的方式,或者看不同的数据结构。在这一点上很难给出建议,因为bloodarray没有在您的代码中定义。

基本上:事情陷入困境是因为你选择了错误的数据结构。调整一下它们,你就会得到更好的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70697049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档