So far i have mare a platformer game with very simple collision detection. unfortunately the collision detection is so simple that when i jump onto block in the air i can get stuck in the middle of them. I have tried more complex ways of collision detection too. I have commented it out because if i use it i go right through the platform. My target is for my sprite to be able to detec all points of the platform. if i hit the platform from the side i want the collision to push me back off. If i jump on it then i wont fall thought. I've been really struggling with this because im a python noob Any help?
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width = 65, height = 35):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.hspeed,self.vspeed = 0,0
self.images=[]
r0 = pygame.image.load("Images\Player\i1.png")
r1 = pygame.image.load("Images\Player\i2.png")
r2 = pygame.image.load("Images\Player\i3.png")
r3 = pygame.image.load("Images\Player\i4.png")
self.hurt = pygame.image.load("Images\Player\Hurt.png")
self.images.append(r0)
self.images.append(r1)
self.images.append(r2)
self.images.append(r3)
self.rotatedimages = []
rr0 = pygame.transform.flip(r0 ,True, False)
rr1 = pygame.transform.flip(r1 ,True, False)
rr2 = pygame.transform.flip(r2 ,True, False)
rr3 = pygame.transform.flip(r3 ,True, False)
self.rotatedimages.append(rr0)
self.rotatedimages.append(rr1)
self.rotatedimages.append(rr2)
self.rotatedimages.append(rr3)
self.gravity = 0.25
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(self.x,self.y,width,height)
self.TimeNum=0
self.TimeTarget=10
self.Timer = 0
self.collision = False
def Level1PlatColl(self, BlockListGrass, TrapList, enemygroup, PowerUps):
PlatformCollision = pygame.sprite.spritecollide(self, BlockListGrass, False )
#for each_object in PlatformCollision:
#if self.vspeed > 0:
#self.rect.bottom = each_object.rect.top
#if self.vspeed < 0:
#self.rect.top = each_object.rect.bottom
#self.vspeed = 0
if PlatformCollision:
self.vspeed = 0
if not PlatformCollision:
self.vspeed -= self.gravity
self.rect.y -= self.vspeed
def Jump(self):
key = pygame.key.get_pressed()
if key[pygame.K_UP]:
self.vspeed = 8
self.rect.y -= self.vspeed