0

So I have this project that I been working on for a while. I have most thing set up, except the physics of the sprite and objects around it. I was wondering, with my code, how to detect collision. I tried doing it myself but I was just confused. Can any1 help?

Code:

class Player(object):
    def __init__(self, img_path):
        self.image = pygame.transform.scale(pygame.image.load(img_path), (120,120))
        self.moving_right = False
        self.x = 0
        self.y = 0
        self.moving_left = False
        self.idling_left = False
        self.idling_right = True #default position
        self.running_frame = 0
        self.idling_frame = 0
        self.running = [pygame.image.load('1 Woodcutter\walk_01.png'),pygame.image.load('1 Woodcutter\walk_02.png'),\
                        pygame.image.load('1 Woodcutter\walk_03.png'),pygame.image.load('1 Woodcutter\walk_04.png'),\
                        pygame.image.load('1 Woodcutter\walk_05.png'),pygame.image.load('1 Woodcutter\walk_06.png')]
        self.idle = [pygame.image.load("1 Woodcutter\idle1.png"),pygame.image.load("1 Woodcutter\idle2.png"),\
                     pygame.image.load("1 Woodcutter\idle3.png"),pygame.image.load("1 Woodcutter\idle4.png")]
        self.player_rect = pygame.Rect(self.x, self.y, self.image.get_width(), self.image.get_height())
    def render(self, surface):
        self.y += 5
        if self.running_frame + 1 >= 60:
            self.running_frame = 0
        if self.idling_frame + 1 >= 60:
            self.idling_frame = 0
        if self.moving_right:
            surface.blit(pygame.transform.scale(self.running[self.running_frame// 10], (120,120)), (self.x, self.y))
            self.running_frame += 1
        elif self.moving_left:
            surface.blit(pygame.transform.scale(pygame.transform.flip(self.running[self.running_frame// 10], True, False), (120, 120)), (self.x, self.y))
            self.running_frame += 1
        elif self.idling_right:
            surface.blit(pygame.transform.scale(self.idle[self.idling_frame// 15], (120,120)), (self.x, self.y))
            self.idling_frame += 1
        elif self.idling_left:
            surface.blit(pygame.transform.flip(pygame.transform.scale(self.idle[self.idling_frame// 15], (120,120)), True, False), (self.x, self.y))
            self.idling_frame += 1
        self.player_rect.x = self.x
        self.player_rect.y = self.y

def load_level(self, level_layout):
        running = True
        while running:
            tiles = ["tile" + str(i) + ".png" for i in range(1,129)]
            collision_tiles = []
            y = 0
            for row in level_layout:
                x = 0
                for tile in row:
                    img = pygame.image.load(rf"PNG\Tiles\{tiles[int(tile) - 1]}")
                    TILE_SIZE = 80
                    self.screen.blit(pygame.transform.scale(img, (TILE_SIZE,TILE_SIZE)), (x * TILE_SIZE, y * TILE_SIZE))
                    tile = int(tile)
                    if tile not in [11,12,13,14,15,33,34,35,36,38,54,56,57,58,59]: #air tiles
                        collision_tiles.append(pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
                    x += 1
                y += 1
            if self.player.moving_right:
                self.player.x += 6
            if self.player.moving_left:
                self.player.x -= 6
                
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        running = False
                    if event.key == K_UP:
                        pass
                    if event.key == K_DOWN:
                        pass
                    if event.key == K_RIGHT:
                        self.player.moving_right = True
                        self.player.idling_right = False
                    if event.key == K_LEFT:
                        self.player.moving_left = True
                        self.player.idling_right = False
                if event.type == KEYUP:
                    if event.key == K_RIGHT:
                        self.player.moving_right = False
                        self.player.idling_right = True
                        self.player.idling_left = False
                    if event.key == K_LEFT:
                        self.player.moving_left = False
                        self.player.idling_left = True
                        self.player.idling_right = False
                    
            self.player.render(self.screen)
            pygame.display.update()
        self.mainClock.tick(60)

In the code above, I implemented a Player class and a Game class (which I didnt specify). The game class just has 3 attributes, self.screen = pygame.display.set_mode((1920, 800)), self.mainClock = pygame.time.Clock(), and self.player = Player(r'1 Woodcutter\Woodcutter.png'). All the collision and gravity tutorials I saw didnt help or they confused me. Can anyone else help me?

0 Answers0