0

I've spent around 2 hours trying to find away but I cant. I'm probably just dumb but can some one help me. What I need is to know how to change the hitbox of my images because they are big and I want to make them smaller so that the game is easier and more playable. The thing is the the function I'm using is too make the image the hitbox but it's to big and I want to make it smaller. I don't know if I should use a different function to make the hitboxes or I can keep the one I have but change the size of the hitbox. This is some code that might be important:

class Player():
    def __init__(self, x, y):
        self.reset(x, y)

    def update(self, game_over):
        dx = 0
        dy = 0
        walk_cooldown = 13

        if game_over == 0:
            #get key presses
            global jumps
            jumps = 0
            key = pygame.key.get_pressed()
            if key[pygame.K_SPACE] and self.jumped == False and self.in_air == False:   #JUMP
                self.vel_y = -9.5
                self.jumped = True
                jumps += 1
            if key[pygame.K_SPACE] == False:
                self.jumped = False
            if key[pygame.K_a]:    # LEFT
                dx -= 3
                self.counter += 2
                self.direction = -1
            if key[pygame.K_d]:    # RIGHT
                dx += 3
                self.counter += 2
                self.direction = 1
            if key[pygame.K_a] == False and key[pygame.K_d] == False:
                self.counter = 0
                self.index = 0
                if self.direction == 1:
                    self.image = self.images_right[self.index]
                if self.direction == -1:
                    self.image = self.images_left[self.index]
            #handle animation
            if self.counter > walk_cooldown:
                self.counter = 0
                self.index += 1
                if self.index >= len(self.images_right):
                    self.index = 0
            if self.direction ==  1:
                self.image = self.images_right[self.index]
            if self.direction ==  -1:
                self.image = self.images_left[self.index]
            if jumps > 2:
                self.in_air = True
            #add gravity
            self.vel_y += 0.4
            if self.vel_y > 20:
                self.vel_y = 20
            dy += self.vel_y

            #check for collision
            self.in_air = True 
            for tile in world.tile_list:
                #check for collision in x direction
                if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
                    dx = 0
                #check for collision in y direction
                if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
                    #check if below the ground i.e. jumping
                    if self.vel_y < 0:
                        dy = tile[1].bottom - self.rect.top
                        self.vel_y = 0
                    #check if above the ground i.e. falling
                    elif self.vel_y >= 0:
                        dy = tile[1].top - self.rect.bottom
                        self.vel_y = 0
                        self.in_air = False

            #check for collisions with enemy
            if pygame.sprite.spritecollide(self, virus_group, False):
                game_over = -1
        
            #check for collisions with water
            if pygame.sprite.spritecollide(self, water_group, False):
                game_over = -1

            #check for collisions with exit
            if pygame.sprite.spritecollide(self, exit_group, False):
                game_over = 1

            #update player coordiantes
            self.rect.x += dx
            self.rect.y += dy

        elif game_over == -1:
            self.image = self.dead_image
            draw_text("GAME OVER", font, blue, (320), (335))
        #draw player onto screen
        win.blit(self.image, self.rect)
        #player hitbox
        pygame.draw.rect(win, (255, 255, 255), self.rect, 2)

        return game_over


    def reset(self, x, y):
        self.images_right = []
        self.images_left = []
        self.index = 0
        self.counter = 0
        for num in range(1, 8):
            robot_right = pygame.image.load(f"img/robot_chrc{num}.png")
            robot_right = pygame.transform.scale(robot_right, (45, 50))
            robot_left = pygame.transform.flip(robot_right, True, False)
            self.images_right.append(robot_right)
            self.images_left.append(robot_left)
            self.dead_image = pygame.image.load("img/death.png")
        self.image = self.images_right[self.index]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        self.vel_y = 0
        self.jumped = False
        self.direction = 0
        self.in_air = True

0 Answers0