I’m having trouble with getting collisions to work with scene. I have a player sprite that is controlled with up/down/left/right arrows and trying to prevent the player form being able to walk through the walls I have placed. Hopefully one of you can help me spot the problem. I’m thinking (hoping) its just a small error that is causing it. I have tried it several different ways and will try to get them all in here.
ground = Node(parent=self)
x = 0
y = 870
while x <= self.size.w + 50:
xwall = SpriteNode('plc:Dirt_Block', position=(x, 0))
xwall2 = SpriteNode('plc:Dirt_Block', position=(x, 850))
ground.add_child(xwall)
self.wall_list.append(xwall)
ground.add_child(xwall2)
self.wall_list.append(xwall2)
if x >= 150 and x <= 1200:
xwall3 = SpriteNode('plc:Dirt_Block', position=(x, 125))
ground.add_child(xwall3)
self.wall_list.append(xwall3)
There is the basic creation of the wall nodes for the display and then adding each one to the list to iterate through during update when attempting to move. Here is that piece:
def update(self):
for touch in self.touches.values():
if touch.location in self.left_button.bbox:
new_x = self.player1.position.x - 3
if new_x >= 50 and new_x <= 1150:
for wall in self.wall_list:
if new_x in wall.bbox:
pass
else:
self.player1.position = (new_x, self.player1.position.y)
if touch.location in self.right_button.bbox:
new_x = self.player1.position.x + 3
if new_x >= 0 and new_x <= 1150:
for wall in self.wall_list:
if self.player1.bbox.intersects(wall.frame) == True:
pass
else:
self.player1.position = (new_x, self.player1.position.y)
if touch.location in self.up_button.bbox:
new_y = self.player1.position.y + 3
if new_y >= 0 and new_y <= 800:
for wall in self.wall_list:
if self.player1.position(self.player1.position.x, new_y) in wall.bbox:
pass
else:
self.player1.position = (self.player1.position.x, new_y)
if touch.location in self.down_button.bbox:
new_y = self.player1.position.y - 3
if new_y >= 50 and new_y <= 800:
for wall in self.wall_list:
if self.player1.bbox.intersects(wall.bbox):
pass
else:
self.player1.position = (self.player1.position.x, new_y)
I have each of the buttons worded a little different on purpose so that you all could see the different ways I have tried to find that collision.Any suggestions on how to prevent my little sprite from running through walls??