I have a character (whose (x, y) position is stored in bodyc), and a bunch of platforms. These platforms are represented in a variable “plist”, and are stored in the fashion [[x, y], pygame.Surface instance]. 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. Setting it bigger, like 5 or 7 pixels, has the same problem as described above.
Is there an easy way of fixing this?
I found out the answer (it actually took about 20 minutes to do it).
prevcoordsare the previous coordinates ofbodyc, the coordinates of the sprite,yvelocityis the y-velocity of the character.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.