0

I successfully got my player sprite to detect where this is a block and stop falling. However, i cant seem to do the same for the its colliding side of with a block. If i do the same thing i did with vspeed and to it to hspeed it messes up the game and the characters zooms off the screen. I was trying to get my sprite to recognize the collision and then not go through the block. Any help would be appreciated. Thanks.

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.speed = 2
    self.Jump = 10

    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.deadimages = [self.hurt]

    self.gravity = 0.35
    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.power_up_timer = 0

    self.running = False


def update(self):
    self.calcgravity()

    self.rect.x += self.hspeed
    self.rect.y += self.vspeed


def level_3_collisions(self, BlockListDirt2):
    PlatformCollision = pygame.sprite.spritecollide(self, BlockListDirt2, False )
    for each_block in PlatformCollision:
        if self.vspeed > 0:
            self.rect.bottom = each_block.rect.top
            self.vspeed = 0
        if self.vspeed <0:
            self.rect.top = each_block.rect.bottom
            self.vspeed = 0


 def move(self, hspeed, vspeed):
    self.hspeed += hspeed
    self.vspeed += vspeed


class Level3:
def __init__(self):

    self.level3 = [
    [1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1],
    [1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1],
    [1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1],
    [1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1],
    [1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1],
    [1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

    ]

    for y in range(0,len(self.level3)):
        for x in range(0,len(self.level3[y])):
            if self.level3[y][x] == 1:
                BlockListDirt2.add(Block(x*40,y*40))


def update(self):

    for block in BlockListDirt2:
        block.render2(screen)



def main3():
TrapList.empty()
TrapList.add(trap4)
player.hspeed = 0
player.rect.x,player.rect.y = 50,0
player.Timer = 0
bullet_list_v.empty()
bullet_list_h.empty()

FiringBullet = pygame.USEREVENT + 1
pygame.time.set_timer(FiringBullet, 1500)

GameExit = False

while GameExit==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.move(-player.speed,0)
            if event.key == pygame.K_RIGHT:
                player.move(player.speed,0)
            if event.key == pygame.K_UP:
                player.move(0,-player.Jump)
                JumpSound.play()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.move(player.speed,0)
            if event.key == pygame.K_RIGHT:
                player.move(-player.speed,0)
            if event.key == pygame.K_UP:
                player.move(0,0)

        if event.type == FiringBullet:
            bullet = VerticalBullets(400,200)
            bullet2 = VerticalBullets(500,200)
            bullet3 = VerticalBullets(625,200)
            hbullet = HorizontalBullets(700,400)
            bullet_list_v.add(bullet)
            bullet_list_v.add(bullet2)
            bullet_list_v.add(bullet3)
            bullet_list_h.add(hbullet)

    screen.fill(BLACK)
    level3.update()

    for eachbullet in bullet_list_v:
        bullet_list_v.draw(screen)
        bullet_list_v.update()

    for eachbullet in bullet_list_h:
        bullet_list_h.draw(screen)
        bullet_list_h.update()

    if player.rect.x > 600 and player.rect.y < 0:
        NextLevel3()

    TrapList.draw(screen)    
    trampoline.render()
    playergroup.update()
    playergroup.draw(screen)
    player.level_3_collisions(BlockListDirt2)
    pygame.display.update()
    clock.tick(60)
furas
  • 134,197
  • 12
  • 106
  • 148
Jim
  • 109
  • 9
  • maybe see ["1.4 Platformer Examples"](http://programarcadegames.com/index.php?chapter=example_code&lang=pl#section_38) (at the end of the page). – furas Jan 22 '16 at 17:50
  • thanks that helped me alot! – Jim Jan 22 '16 at 18:56

0 Answers0