-1

I think i've worked out the calculations for the collision i just need to know how to send the positional co-ords of my sprites to the class where the collision is being checked. I also need to know how to check all of the sprites so it knows which one is being collided with.

class Enemy(object):
'''single enemy'''

def __init__(self, canvas):
    # access to canvas
    self.canvas = canvas
    self.radius = 12.5 # random
    self.color = random.choice( ('blue', 'red', 'green') )
    self.x = random.uniform(self.radius, RES_X-self.radius)
    self.y = random.uniform(self.radius, RES_Y-self.radius)
    self.x1 = self.x-self.radius
    self.y1 = self.y-self.radius
    self.x2 = self.x+self.radius
    self.y2 = self.y+self.radius
    self.oval = self.canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill=self.color, outline=self.color)
    self.moving = True
    self.start()


def start(self):
    '''start moving'''
    self.moving = True
    # move this enemy after random time
    random_time = random.randint(150, 3000)
    root.after(random_time, self.move)


def move(self):
    global enposx, enposy
    if self.moving: # to stop root.after
        direction = random.randint(1,4)
        if direction == 1: # up
            self.y -= self.radius
            self.y1 -= self.radius
            self.y2 -= self.radius
        elif direction == 2: # down
            self.y += self.radius
            self.y1 += self.radius
            self.y2 += self.radius
        elif direction == 3: # left
            self.x -= self.radius
            self.x1 -= self.radius
            self.x2 -= self.radius
        elif direction == 4: # right
            self.x += self.radius
            self.x1 += self.radius
            self.x2 += self.radius
        self.enposx = int(self.x1-self.radius)
        self.enposy = int(self.y1-self.radius)
        self.canvas.coords(self.oval, self.x1, self.y1, self.x2, self.y2)

The code above is the one i need to transfer self.enposx and self.enposy (the centre point of the circle) from

class Projectile(object):
'''single projectile'''
def __init__(self, parent, canvas):
    # access to canvas
    self.parent = parent
    self.repcount = 0
    self.canvas = canvas
    self.radius = 6 # random
    self.color = random.choice( ('blue', 'red', 'green', 'yellow', 'orange') )
    self.x1 = x + self.radius
    self.y1 = y + self.radius
    self.x2 = x - self.radius
    self.y2 = y - self.radius
    self.oval = self.canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill=self.color, outline=self.color)
    self.moving = True
    self.direction = shootdir
    self.start()


def start(self):
    global direction
    '''start moving'''
    self.moving = True
    # move this enemy after random time
    root.after(20, self.move)


def move(self):
    if self.repcount < 20:
        if self.moving: # to stop root.after
            if self.direction == 1: # up
                 self.y1 -= self.radius
                 self.y2 -= self.radius
            elif self.direction == 2: # down
                self.y1 += self.radius
                self.y2 += self.radius
            elif self.direction == 3: # left
                self.x1 -= self.radius
                self.x2 -= self.radius
            elif self.direction == 4: # right
                self.x1 += self.radius
                self.x2 += self.radius
            self.canvas.coords(self.oval, self.x1, self.y1, self.x2, self.y2)
            self.proposx, self.proposy = int(self.x1-self.radius), int(self.y1-self.radius)
            self.repcount += 1
            xlist, ylist = [self.proposx, self.enposx], [self.proposy, self.enposy]
            xmax, xmin = max(xlist), min(xlist)
            ymax, ymin = max(ylist), min(ylist)
            xval, yval = int(xmax - xmin), int(ymax - ymin)
        if xval < 12.5 and yval < 12.5:
            print("hit")
            self.parent.remove(self)
        else:
            root.after(20, self.move)
    else:
        print('done')
        self.parent.remove(self)

The code above is where i need it sent too but im not sure how to send it without it giving out an error

thanks

1 Answers1

0

From what I understand, you are creating some sort of space invaders-like game, and you are not certain how to compare enemies' (x,y) coordinates with (player's) projectiles' coordinates.

Before going any further, please take note that I have never programmed that kind of game, so what follows may (will) require optimisation.

I would suggest building a 3rd class built specifically to detect collisions between "enemies" and "projectiles":

class CollisionManager:
    def __init__(self, enemies, projectiles):
        self.enemies = enemies
        self.projectiles = projectiles
        self.pending_collisions = []

    def detect_collisions(self):
        # Your collision detection magic here
        pass

You might need to add a get_propertiesmethod / property to bundle all information you need to do your math (see this post for clarification).

The pending_collisionsattribute is merely a suggestion to help you iterate in a clean way over enemies and projectiles, then perform whatever operation you may need.

The reason behind this suggestion is that I don't think handling collision belongs inside a "projectile" class. If you are to destroy (remove all references to) enemy objects, you are probably going to end up doing stuff like all_enemies.remove(destroyed_enemy) and doing that from your projectile class will make your code very messy in no time.

Community
  • 1
  • 1
user768518
  • 114
  • 6