(Python)Platformer-字符跳过/放置不正确
我有一个角色(其(x,y)位置存储在bodyc中)和一堆平台。这些平台以变量“plist”表示,并以[[x,y],pygame.Surface实例]的形式存储。角色以速度跳跃 这是我目前的算法:(Python)Platformer-字符跳过/放置不正确,python,algorithm,pygame,Python,Algorithm,Pygame,我有一个角色(其(x,y)位置存储在bodyc中)和一堆平台。这些平台以变量“plist”表示,并以[[x,y],pygame.Surface实例]的形式存储。角色以速度跳跃 这是我目前的算法: def onplatform(self): for i in plist: if intersect(i[0]+list(i[1].get_size()), [bodyc[0], bodyc[1], 50, 50]): return True, plist
def onplatform(self):
for i in plist:
if intersect(i[0]+list(i[1].get_size()), [bodyc[0], bodyc[1], 50, 50]):
return True, plist.index(i)
return False, len(plist)
onplat=self.onplatform()
if yvelocity!=-13:
bodyc[1]-=yvelocity
if yvelocity>-12: yvelocity-=1
if yvelocity==-13 and not onplat[0]: yvelocity=-1
if onplat[0] and -13<yvelocity<-1:
yvelocity=-13
bodyc[1]=plist[onplat[1]][0][1]-50 #(y-value of platform)-50
if pressed[pygame.K_UP] and yvelocity==-13:
yvelocity=13
def-onplatform(self):
对于plist中的i:
如果相交(i[0]+列表(i[1].get_size()),[bodyc[0],bodyc[1],50,50]):
返回True,plist.index(i)
返回False,len(plist)
onplat=self.onplatform()
如果你是城市=-13:
bodyc[1]=yvelocity
如果yvelocity>-12:yvelocity-=1
如果yvelocity=-13且不在平台上[0]:yvelocity=-1
如果onplat[0]和-13我找到了答案(实际上花了大约20分钟)
prevcoords
是bodyc
之前的坐标,精灵的坐标,yvelocity
是角色的y速度
对于那些同样陷入“平台化问题”的未来程序员来说:如果你想制作一个多屏幕的平台,你必须修改这个程序,因为这是非常低效的。此外,这可能不适用于非对称精灵
def intersect(recta, rectb):
a=(rectb[1]+rectb[3]<recta[1]) or (recta[1]+recta[3]<rectb[1]) #up, down
b=(rectb[0]+rectb[2]<recta[0]) or (recta[0]+recta[2]<rectb[0]) #left, right
return not(a or b)
def onplatform(self):
for i in plist:
if intersect(i[0]+[i[1].get_width(), 1], [bodyc[0], bodyc[1]+47, 50, 3]):
return True, plist.index(i)
onplat=self.onplatform()
if yvelocity!=-13:
bodyc[1]-=yvelocity
for i in plist:
temp=i[0][0]<bodyc[0]+50<i[0][0]+i[1].get_width()
temp2=i[0][0]<bodyc[0]<i[0][0]+i[1].get_width()
if prevcoords[1]+50<=i[0][1]<=bodyc[1]+50 and (temp or temp2):
bodyc[1]=i[0][1]-50
yvelocity=-13
break
if yvelocity>-12: yvelocity-=1
if yvelocity==-13 and not onplat[0]: yvelocity=-1
if pressed[pygame.K_UP] and yvelocity==-13:
yvelocity=13
prevcoords=bodyc[:]
def intersect(recta,rectb):
a=(rectb[1]+rectb[3]