1

I have an object that needs to follow 4 points given by the coordinates in self.list. When I execute the code, I can see that the checks are passed and it augments the search coordinates to the next point but the object on screen only goes to the last one.

Working in Python 3 and in pygame.

How can I fix this?

class Enemy:
    def __init__(self, image):
        super().__init__()
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.rect.x = 1280
        self.rect.y = randrange(abs(720 - self.rect.height))                                                
        self.pattern = 2
        self.list = [(1100,360),(900,180),(700,360),(900,540)]
        self.tuple_dest = self.list[0]
        self.i = 0
        self.p=False


    def move(self, player):

        if self.tuple_dest[0] <= self.rect.x:
            self.rect.x -= 1
        elif self.tuple_dest[0] >= self.rect.x:
            self.rect.x += 1
        if self.tuple_dest[1] <= self.rect.y:
            self.rect.y -= 1
        elif self.tuple_dest[1] >= self.rect.y:
            self.rect.y += 1
        #check if arrived
        print(self.p)
        if self.tuple_dest[0] == self.list[self.i][0] and  self.tuple_dest[1] == self.list[self.i][1] and self.p == False:
            self.p = True

        if self.i < (len(self.list)-1) and self.p==True:
                print(self.p)
                self.i += 1
                print(self.i)
                self.tuple_dest = self.list[self.i]
                self.p = False
petezurich
  • 9,280
  • 9
  • 43
  • 57
Kobac
  • 11
  • 3
  • Is there an excutable code? Don't you want me to know it? – Haru Apr 02 '18 at 13:47
  • This is a part of a bigger project, the main code makes the object and calls upon the move function while inside a semi-infinite loop (user interrupted-infinite loop) – Kobac Apr 02 '18 at 13:49
  • Please upload a complete, verifiable example instead of describing the issue vaguely. – Mercury Platinum Apr 02 '18 at 15:33
  • The others are right, a good, runnable [mcve](https://stackoverflow.com/help/mcve) is immensely helpful. I've already found the mistake, but you could (or should) still edit your post for the others. – skrx Apr 02 '18 at 17:14
  • grammar, spelling – petezurich Apr 04 '18 at 15:23

1 Answers1

0

This is the problematic line:

if self.tuple_dest[0] == self.list[self.i][0] and self.tuple_dest[1] == self.list[self.i][1] and self.p == False:

You're just checking if the coordinates in the tuple are equal to the same tuple in the self.list, so the condition is immediately True.

You need to check if the current target coordinates are equal to the self.rect coordinates:

tx, ty = self.tuple_dest
if tx == self.rect.x and ty == self.rect.y and not self.p:
    self.p = True
skrx
  • 19,980
  • 5
  • 34
  • 48
  • I also suggest to use vectors or trigonometry to move the objects to the waypoints because that will look a lot better: https://stackoverflow.com/a/48105969/6220679 – skrx Apr 03 '18 at 04:33