0

In the move function, I am passing through the x and y coordinates of each ghost(inky, blinky, pinky and clyde), but they are not being added on constantly, even though I put a +=. The values are resetting and going back to the original value every time they try to move.

import pygame
import random


class Enemy:
    def __init__(self, board):
        self.board = board
        self.count = 0
        self.spawn_count = 0
        self.timer = 0
        self.actions = ['L', 'R', 'U', 'D']
        self.location_list = [(0, 0), (0, 0), (0, 0), (0, 0)]
        self.colour_list = [(178, 225, 255), (255, 200, 50), (0, 0, 255), (255, 95, 95)]

    def enemy_spawn(self):
        while self.spawn_count < 1:
            while self.count < 4:
                spawn_point = random.choice(self.board.enemy_spawn)
                if spawn_point not in self.location_list:
                    self.location_list[self.count] = spawn_point
                    self.count += 1
            pygame.draw.circle(self.board.window, (self.colour_list[0]), (self.location_list[0]), 8)
            self.x_inky = 607
            self.y_inky = 276
            pygame.draw.circle(self.board.window, (self.colour_list[1]), (self.location_list[1]), 8)
            self.x_blinky = 607
            self.y_blinky = 276
            pygame.draw.circle(self.board.window, (self.colour_list[2]), (self.location_list[2]), 8)
            self.x_pinky = 607
            self.y_pinky = 276
            pygame.draw.circle(self.board.window, (self.colour_list[3]), (self.location_list[3]), 8)
            self.x_clyde = 607
            self.y_clyde = 276
            self.spawn_count += 1
        self.timer += 1
        pygame.display.update()

    def enemy_movement(self):
        self.inky()
        self.blinky()
        self.pinky()
        self.clyde()

    def moves(self, x, y, location, colour, direction):
        if direction == 'L':
            x -= self.board.cell_width
            self.location_list[location] = (x, y)
            pygame.draw.circle(self.board.window, (self.colour_list[colour]), (self.location_list[location]), 8)
        if direction == 'R':
            x += self.board.cell_width
            self.location_list[location] = (x, y)
            pygame.draw.circle(self.board.window, (self.colour_list[colour]), (self.location_list[location]), 8)
        if direction == 'U':
            y -= self.board.cell_height
            self.location_list[location] = (x, y)
            pygame.draw.circle(self.board.window, (self.colour_list[colour]), (self.location_list[location]), 8)
        if direction == 'D':
            y += self.board.cell_height
            self.location_list[location] = (x, y)
            pygame.draw.circle(self.board.window, (self.colour_list[colour]), (self.location_list[location]), 8)
        pygame.display.update()

    def inky(self):
        direction = random.choice(self.actions)
        if self.timer >= 1 and self.spawn_count == 1:
            self.location_list[0] = (self.x_inky, self.y_inky)
            pygame.draw.circle(self.board.window, (self.colour_list[0]), (self.location_list[0]), 8)
            self.spawn_count += 1
        elif self.timer >= 1:
            self.moves(self.x_inky, self.y_inky, 0, 0, direction)

    def blinky(self):
        direction = random.choice(self.actions)
        if self.timer >= 30 and self.spawn_count == 2:
            self.location_list[1] = (607, 276)
            pygame.draw.circle(self.board.window, (self.colour_list[1]), (self.location_list[1]), 8)
            self.spawn_count += 1
        elif self.timer >= 30:
            self.moves(self.x_blinky, self.y_blinky, 1, 1, direction)
        pygame.display.update()

    def pinky(self):
        direction = random.choice(self.actions)
        if self.timer >= 60 and self.spawn_count == 3:
            self.location_list[0] = (607, 276)
            pygame.draw.circle(self.board.window, (self.colour_list[0]), (self.location_list[0]), 8)
            self.spawn_count += 1
        elif self.timer >= 60:
            self.moves(self.x_pinky, self.y_pinky, 2, 2, direction)
        pygame.display.update()

    def clyde(self):
        direction = random.choice(self.actions)
        if self.timer >= 120 and self.spawn_count == 4:
            self.location_list[0] = (607, 276)
            pygame.draw.circle(self.board.window, (self.colour_list[0]), (self.location_list[0]), 8)
            self.spawn_count += 1
        elif self.timer >= 120:
            self.moves(self.x_clyde, self.y_clyde, 3, 3, direction)
        pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 2
    You'll might get better response if you can create a minimal example of your question that isolates the problem. – Eric Wilson Oct 03 '19 at 14:21
  • 1
    The issue may be that you are misunderstanding how passing in a value to a function works. Updating the value of x within the function won't update it globally or in the scope it was called from. See https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – Zev Oct 03 '19 at 14:56
  • You have a desing problem, if i get it right, every `inky, blinky, pinky and clyde` **is** a `Enemy` object? – stovfl Oct 03 '19 at 15:26
  • The way the class is designed, it seems to be intended as a manager for all enemies. But I agree with @stovfl: it would be better to have a class for a single enemy and create an instances of that class for each enemy on the screen. – Valentino Oct 03 '19 at 15:29
  • @Valentino Are u saying I create a super class ghost and have the each ghost as a sub class? – George Dimitriou Oct 03 '19 at 19:05
  • 1
    If you are speaking of inerithance, no. Just create an `Enemy` class which represens a single enemy. Then in your main loop you may create as many enemies as you want, by creating different instances of that class. – Valentino Oct 03 '19 at 19:10

0 Answers0