1
votes

VIDEO

I have 2 rects that move left and right when the rat reaches either of the sides of the rect it waits 100 secs then starts moving to the other side. My problem is I have 2 hitboxes if my player is colliding with the right one the enemy will move to the right and attack the player if its moving to the left side it will move to left side and attack the player but as you can see in the video below if I'm not colliding with the rat hitbox it stops moving. I want it to continue to move left and right and wait 100 secs.

VIDEO

This is how the rats are moving

if move:
    if move_timer < 200:
        move_timer += 1
    else:
        rat1.direction = "move2"
        rat1.x += 1


if move2:
    if move_timer2 < 200:
        move_timer2 += 1
    else:
        rat1.direction = "move"
        rat1.x -= 1

        
if rat1.rect.colliderect(rat11.rect):
    rat1.direction = "idle"
    move = True
    move2 = False
    move_timer2 = 0

    
elif rat1.rect.colliderect(rat22.rect):
    rat1.direction = "id"
    move = False
    move2 = True

    move_timer = 0

If the player collides with the right or left hitbox it should move the rat enemy to that direction the player is on sometimes when the enemy is moving towards the player and we hit the rect that moves our rat left and right it will cause glitch with the directions and it doesn't move.

elif playerman.rect.colliderect(rat1.hitbox):
    rat1.x += 3
    rat1.direction = "move2"

elif playerman.rect.colliderect(rat1.hitbox2):
    rat1.x -= 3
    rat1.direction = "move"

Here I made it so if the rat collides with the playerman hitbox then it should play the attack animation move and move2 should be false because we don't want the enemy to move left and right now it should stick to attacking the player.

if rat1.direction == "move2":
    if playerman.rect.colliderect(rat1.rect):
        move = False
        move2 = False
        rat1.direction = "at"
        

if rat1.direction == "move":
    if playerman.rect.colliderect(rat1.rect):
        move = False
        move2 = False
        rat1.direction = "att"

I'm not sure if I explained my problem well but what I'm trying to say is how can make my rat keep moving the direction that it kept moving if the player is not colliding with the hitbox that will move the rat towards the player enemy more and how can I stop the rect that moves my rat left and right when the enemy rat is attack the player.

1
Please don't edit you question to be an entirely new question. - Ted Klein Bergman
Also, you've currently asked 127 questions, which requires many to put down a lot of time to help you. To make it easier for the people helping you, please read through How to Ask and minimal reproducible example. Making a minimal reproducible example is especially helpful, as it makes it much easier to give good and accurate answers. - Ted Klein Bergman
I'm trying my best to ask questions sorry if I im messing up - Habib Ismail

1 Answers

1
votes

It is hard to follow your logic, but it seems that you need an additional condition. If neither move nor move2 is set and the rat does not collide with the player, the rat must keep moving:

if not move and not move2:
    if not playerman.rect.colliderect(rat1.rect):
    
        if rat1.direction == "at":
            move = True
            move2 = False
            move_timer = 200
            rat1.direction = "move"
        elif rat1.direction == "att":
            move = False
            move2 = True
            move_timer2 = 200
            rat1.direction = "move2"