在我的AH计算项目中,我正在重新创建我的Nidhogg版本。一切都进展顺利,直到血开始喷出来。我不太确定如何提高代码的效率,因为我对python还很陌生。
这是喷血的班级:
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
在下面有一层血,它使用一组堆栈工作,当血液到达地板时,这是上面的代码添加的。
bloodgrid = [[] for x in range(512)]这是一个代码,用来摧毁飞行中的血液物体,并在屏幕上闪动血液地板。
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我不认为只更新屏幕的一部分是不可能的,因为相机在移动,地板上的血迹也在移动。
随着更多的血液积聚在地板上,游戏的框架开始下降,当血液喷出时,它会变得特别的不稳定。有什么办法能让这个更有效率吗?我不想交一个波涛汹涌的游戏,但我真的很喜欢血的样子。谢谢。
发布于 2022-01-13 17:42:27
有几点希望是有帮助的。
insert()在列表的开头插入新元素,并且在调用move()函数的循环中这样做。在列表中插入任何位置(除了末尾)都非常缓慢,原因超出了这篇文章的范围。您应该考虑在其中放置一个来自deque的collections,它可以有效地插入前面或后面。如果这令人望而生畏,您还可以“做一些数学计算”,并将其插入列表的末尾,然后从最后一个元素中倒排,这也是有效的。pop的速度也非常慢。找出一种不同的方式,或者看不同的数据结构。在这一点上很难给出建议,因为bloodarray没有在您的代码中定义。基本上:事情陷入困境是因为你选择了错误的数据结构。调整一下它们,你就会得到更好的结果。
https://stackoverflow.com/questions/70697049
复制相似问题