[Python] Enemies speed unexpectedly slows down when selecting a tower in my pygame TD Game...

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    Hello I am currently making a tower defence game in pygame and recently in my tower class i coded in a transparent circle which depicts the towers range in which this only shows up when the player left clicks on a placed tower. When testing it out I realised for some reason when i do left click on a tower the enemy which follows a set path slows down? And the amount it slows down increases with a towers range? I'm assuming this is occuring due to the transparent circle somehow blocking the waypoints the enemy follows through but I am not sure.

    This is the code used to create the circle which is inside the tower class:

    self.range is just a number i set for example 200

    # creates circle range around tower
    self.range_img = pg.Surface((self.range * 2,self.range *2 ))
    self.range_img.fill((0,0,0))
    self.range_img.set_colorkey((0,0,0))
    pg.draw.circle(self.range_img, "grey100",(self.range,self.range),self.range )
    self.range_img.set_alpha(100)
    self.range_rect = self.range_img.get_rect()
    self.range_rect.center = self.rect.center


    I output this then in a draw function where if a variable (which only turns true when the player left clicks on the tower) is true it does surface.blit(self.range_img,self.range_rect)

    The enemies move along a set of waypoints via the move function inside the enemies own class

    This uses a couple of variables being:

    self.waypoints = waypoints #list containing waypoints
    self.pos = Vector2(self.waypoints[0])
    self.target_waypoint = 1
    self.speed = 0.1


    Then these values are used in the move function :

    def move(self):
    #define a target waypoint
    if self.target_waypoint < len(self.waypoints):
    self.target = Vector2(self.waypoints[self.target_waypoint])
    self.movement = self.target - self.pos
    else:
    #enemy destoyed when reaching end
    self.kill()

    # distance to target
    distance = self.movement.length()
    # check for if the speed is more than distance need to cover
    if distance >= self.speed:
    self.pos += self.movement.normalize() * self.speed
    print(distance)
    else:
    if distance != 0:
    self.pos += self.movement.normalize() * distance
    self.target_waypoint += 1


    I then call this function through update() which i call during my main game loop

    Is there anything in creating the transparent circle and displaying it that causes something to change in my move() function causing the enemies to slow down?

    I tried to debug and understand what was occuring by printing the distance,speed,self.movement, self.movement.normaliza() but these values dont seem to change suddenly when i do select a tower even though the enemy does slow down still

    Continue reading...

Compartilhe esta Página