Without sharing your code its difficult to say what may be going wrong, but I can provide a working example, in the following code there are two boxes, the one on the left is the player and the one on the right is an enemy. Pressing spacebar will fire a bullet to the right from the player, colliding with and removing both the enemy and the bullet.
import pyglet
from pyglet.window import key
class Example(pyglet.window.Window):
def __init__(self):
super(Example, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Example")
self.clear()
#key input buffer
self.key_input = []
#add objects to be updated
self.objects = []
#add player
self.objects.append(player(5,40,self.objects))
#add enemy
self.objects.append(enemy(80,40,self.objects))
#call main window update function
pyglet.clock.schedule_interval(self.update, .01)
def update(self,dt):
#update objects
for a in self.objects:
a.update(self.key_input)
#check collision of objects
for a in xrange(0,len(self.objects),1):
for b in xrange(0,len(self.objects),1):
if a != b and len(self.objects) > 1:
result = self.collide(self.objects[a],self.objects[b])
#if collision occurred, remove both objects from update list
if result == True:
self.objects.remove(self.objects[a])
self.objects.remove(self.objects[b-1])
#clear key input buffer
self.key_input = []
#draw screen
self.draw()
def draw(self):
self.clear()
for a in self.objects:
a.draw()
def on_key_press(self,symbol,modifiers):
if symbol == key.ESCAPE:
self.close()
self.key_input.append(key.symbol_string(symbol) + " press")
def collide(self, a, b):
if a.y + a.height< b.y:
return False
if a.y > b.y + b.height:
return False
if a.x + a.width < b.x:
return False
if a.x > b.x + b.width:
return False
return True
class player(object):
def __init__(self,x,y,objects):
self.x = x
self.y = y
self.width = 32
self.height = 32
self.objects = objects
def update(self,key):
#fire a bullet, which adds it to the main objects list to be updated
if 'SPACE press' in key:
self.objects.append(bullet(self.x+self.width+2,self.y+(self.height/2),self.objects))
def draw(self):
pyglet.graphics.draw(8, pyglet.gl.GL_LINES, ("v2f", (self.x,self.y,self.x+self.width,self.y,
self.x+self.width,self.y+self.height,self.x+self.width,self.y,
self.x+self.width,self.y+self.height,self.x,self.y+self.height,
self.x,self.y+self.height,self.x,self.y)))
class enemy(object):
def __init__(self,x,y,objects):
self.x = x
self.y = y
self.width = 32
self.height = 32
self.objects = objects
def update(self,key):
pass
def draw(self):
pyglet.graphics.draw(8, pyglet.gl.GL_LINES, ("v2f", (self.x,self.y,self.x+self.width,self.y,
self.x+self.width,self.y+self.height,self.x+self.width,self.y,
self.x+self.width,self.y+self.height,self.x,self.y+self.height,
self.x,self.y+self.height,self.x,self.y)))
class bullet(object):
def __init__(self,x,y,objects):
self.x = x
self.y = y
self.width = 6
self.height = 6
self.objects = objects
def update(self,key):
self.x += 1
#if bullet eceeds the right side of the window, remove it from update list
if self.x > 640:
self.objects.remove(self)
def draw(self):
pyglet.graphics.draw(8, pyglet.gl.GL_LINES, ("v2f", (self.x,self.y,self.x+self.width,self.y,
self.x+self.width,self.y+self.height,self.x+self.width,self.y,
self.x+self.width,self.y+self.height,self.x,self.y+self.height,
self.x,self.y+self.height,self.x,self.y)))
if __name__ == '__main__':
window = Example()
pyglet.app.run()