0

I am pretty new to pygame and this is my first basic game code. I am making a platformer game and I have everything rendered, tiles, background, sprite and sprite walking animations. Now I need the physics and I am having a pretty hard time understanding how to go about this. I know you can use rect but from what I see, most youtubers are telling me how to use rect with one sprite image, not multiple. Maybe I could try a different route, what do you guys suggest I do?

import pygame
pygame.init()

class Player(object):
    def __init__(self,x,y):
        self.velocity=4
        self.x = x
        self.y = y
        self.jumping=False
        self.right=False
        self.left=False
        self.jumptotal=10
        self.walkcount=0
        self.player_hitbox=(self.x+1,self.y,60,60)

    def hitbox(self,window):
        self.player_hitbox = (self.x, self.y, 47, 83)
        pygame.draw.rect(window,(255,0,0),self.player_hitbox,2)



running=True

player=Player(20,600)

def draw_game():
    global player
    window.blit(background, (0, 0))
    if player.walkcount + 1 > 45:
        player.walkcount = 0
    if player.right:
        window.blit(walk_right[player.walkcount // 3], (player.x, player.y))
        player.walkcount += 1
    elif player.left:
        window.blit(walk_left[player.walkcount // 3], (player.x-50, player.y))
        player.walkcount += 1
    else:
        window.blit(idle, (player.x, player.y))
    player.hitbox(window)


    for event in pygame.event.get():
      if event.type==pygame.QUIT:
        running=False

    key_press=pygame.key.get_pressed()
    if key_press[pygame.K_LEFT] and player.x>player.velocity:
        player.x-=player.velocity
        player.right=False
        player.left=True
    elif key_press[pygame.K_RIGHT] and player.x<2300-player.x-player.velocity:
        player.x+=player.velocity
        player.right = True
        player.left = False
    else:
        player.right=False
        player.left=False
        player.walkcount=0

    if key_press[pygame.K_UP]:
       player.jumping=True

    if player.jumping:
        if player.jumptotal>=-10:
            jumpside=1
            if player.jumptotal<0:
              jumpside=-1

            player.y-=(player.jumptotal**2)*0.3*jumpside
            player.jumptotal-=1
        else:
          player.jumping=False
          player.jumptotal=10

    pygame.display.update()


pygame.quit()
Amir Yolo
  • 21
  • 2
  • This is not related with the question but you should use ```elif``` in your loop that updates your background, It should statistically be faster since it will skip all the other conditions if one is found to be true. – Yash Mar 01 '21 at 23:45

1 Answers1

2

I think you are looking for:

pygame.sprite.collide_rect()
pygame.sprite.collide_circle()

The documentation: https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect

This returns a boolean value and checks wether two hitboxes overlap

pygame.sprite.collide_rect(sprite1, sprite2)

Edit: It is also considered good practice to use Pygame groups https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group

pygame.sprite.Group

As @pavel pointed out this allows you to use:

 pygame.sprite.spritecollideany()

Which checks if the sprite hit any sprite in the group

Yash
  • 391
  • 3
  • 14