0

I'm having trouble trying to implement this feature where my player sprite dashes, or simply moves, to the current mouse position on click. I've tried implementing it using vectors and calculating slopes, but I can't get it to work. Here's my code so far.

https://github.com/nimapourjafar/zip

class Player(pygame.sprite.Sprite):
    def __init__(self,x,y,scale,ammo,weapon):
        pygame.sprite.Sprite.__init__(self)
        self.alive = True
        self.ammo = ammo
        self.speed = 10
        self.start_ammo = ammo
        self.shoot_cooldown = 0
        self.health = 100
        self.max_health = self.health
        self.direction = 0
        self.vel_y = 0
        self.jump = False
        self.in_air = True
        self.flip = False
        self.animation_list = []
        self.frame_index = 0
        self.action = 0
        self.update_time = pygame.time.get_ticks()
        animation_types = ['idle','run','jump']
        for animation in animation_types:
            temp_list = []
            num_frame = len(os.listdir(f'img/player/{animation}'))
            for i in range(num_frame):
                img = pygame.image.load(f'img/player/{animation}/{i}.png').convert_alpha()
                img = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
                temp_list.append(img)
            self.animation_list.append(temp_list)
        self.image = self.animation_list[self.action][self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.width = self.image.get_width()
        self.height = self.image.get_height()
    
    def move(self, moving_left, moving_right):
        screen_scroll = 0
        dx = 0
        dy = 0
        if moving_left:
            dx = -self.speed
            self.flip = True
            self.direction = -1
        if moving_right:
            dx = self.speed
            self.flip = False
            self.direction = 1
        if self.jump is True and self.in_air is False:
            self.vel_y = -11
            self.jump = False
            self.in_air = True
        self.vel_y += GRAVITY
        if self.vel_y >10:
            self.vel_y
        dy += self.vel_y

        

        for tile in world.obstacle_list:
            #check collision in the x direction
            if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
                dx = 0
            #check for collision in the 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:
                    self.vel_y = 0
                    dy = tile[1].bottom - self.rect.top
                #check if above the ground, i.e. falling
                elif self.vel_y >= 0:
                    self.vel_y = 0
                    self.in_air = False
                    dy = tile[1].top - self.rect.bottom
        if self.rect.left + dx < 0 or self.rect.right + dx > SCREEN_WIDTH:
            dx = 0
        self.rect.x +=dx
        self.rect.y +=dy

        #scroll
        if (self.rect.right > SCREEN_WIDTH - SCROLL_THRESH and bg_scroll < (world.level_length * TILE_SIZE) - SCREEN_WIDTH) or (self.rect.left < SCROLL_THRESH and bg_scroll > abs(dx)):
            self.rect.x-=dx
            screen_scroll = -dx
        return screen_scroll
    

    def update_animation(self):
        ANIMATION_COOLDOWN = 240
        self.image = self.animation_list[self.action][self.frame_index]
        if pygame.time.get_ticks() - self.update_time> ANIMATION_COOLDOWN:
            self.update_time = pygame.time.get_ticks()
            self.frame_index += 1
        if self.frame_index >= len(self.animation_list[self.action]):
            self.frame_index =0
    
    def update_action(self, new_action):
        if new_action != self.action:
            self.action = new_action
            self.frame_index = 0
            self.update_time = pygame.time.get_ticks()
    
    def draw(self):
        screen.blit(pygame.transform.flip(self.image, self.flip, False), self.rect)

    def update(self):
        self.update_animation()
        if self.shoot_cooldown > 0:
            self.shoot_cooldown -= 1

The following is my player object. I was thinking about adding a function that makes it dash and takes in the position of the mouse as a param, but I'm not too sure how to update the sprite after that. Another idea I had is to implement something in my update function and have a conditional statement to check if the mouse click is down, but I couldn't get that to work as well. Can anyone help?

(sorry for bad formatting, first time asking here)

  • 2
    Is all that code relevant to your question? Please try and condense your code down to a [mre]. Please also take the [tour], read [ask], and the [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Welcome to SO! – Pranav Hosangadi Apr 23 '21 at 03:04
  • @PranavHosangadi sorry, I wanted to have enough code for people to understand and have the resources to answer my question. I'll keep in mind for that next time! Do you have any idea of what I could do as well? – Nima Pourjafar Apr 23 '21 at 04:11
  • 1
    Unfortunately, the size of the code scared me off. It's late and I'm here for some light browsing, so I don't feel up to going through a whole bunch of code at this time. You'll find a lot of users are put off by the size of the code you post -- if it appears daunting at first glance, any user who's volunteering their time will think twice about whether they want to invest it in weeding out the unnecessary code from your example. This is why we ask for a [mre]. Here's a helpful link on debugging if you haven't tried that already: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Pranav Hosangadi Apr 23 '21 at 04:14

0 Answers0