简体   繁体   English

(Python)Platformer-角色跳过/放置不正确

[英](Python) Platformer - character skips over/incorrectly placed

I have a character (whose (x, y) position is stored in bodyc), and a bunch of platforms. 我有一个角色(其(x,y)位置存储在bodyc中)和一堆平台。 These platforms are represented in a variable "plist", and are stored in the fashion [[x, y], pygame.Surface instance]. 这些平台以变量“ plist”表示,并以[[x,y],pygame.Surface实例]的形式存储。 The character jumps with velocity. 角色以速度跳跃。

This is my current algorithm: 这是我当前的算法:

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

The problem with this algorithm is that when the character is touching the platform, even if the bottom is not on the platform, the code will put the character onto the platform anyways. 该算法的问题在于,当角色接触平台时,即使底部不在平台上,代码也仍会将角色放置在平台上。

I've tried making it so the hitbox is very small (1 or 3 pixels high), but the character doesn't touch the platform at all, because the velocity makes the character skip over the platform. 我尝试过使它的命中盒很小(高1或3像素),但是角色根本不接触平台,因为速度使角色跳过了平台。 Setting it bigger, like 5 or 7 pixels, has the same problem as described above. 将其设置为更大(如5或7像素)具有如上所述的相同问题。

Is there an easy way of fixing this? 有解决此问题的简便方法吗?

I found out the answer (it actually took about 20 minutes to do it). 我找到了答案(实际上花了大约20分钟的时间)。

prevcoords are the previous coordinates of bodyc , the coordinates of the sprite, yvelocity is the y-velocity of the character. prevcoords是以前的坐标bodyc ,精灵的坐标, yvelocity是字符的yvelocity。

For future programmers who are also stuck with the "platforming problem": You'll have to modify this program if you want to make a multi-screen platformer, because this is really inefficient. 对于也陷入“平台问题”的未来程序员:如果要制作多屏平台游戏,则必须修改此程序,因为这确实效率低下。 Also, this may not work with asymmetric sprites. 而且,这可能不适用于非对称子画面。

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[:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM