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