I am trying to make collision detection between the walls of a room and my character, Michael Scarn. However, nothing is being detected. I know this because I told the program to print "Collide" when there is a collision, but nothing is being detected. Is it a small mistake or a bigger mistake in my code? Can anyone fix this?
class Scarn(object): # creates attributes for Michael Scarn (player)
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 8
self.walkCount = 0
self.standing = False
self.left = False
self.right = False
self.up = False
self.down = False
self.sleeping = True
self.scarn_hitbox = pygame.Rect(self.x, self.y + 11, 32, 64)
def draw(self, win): # draws Michael Scarn and his movements
if self.walkCount + 1 >= 3:
self.walkCount = 0
if self.sleeping:
win.blit(scarn_sleeping, (340, 150))
if not self.sleeping:
if self.left:
win.blit(scarn_left[self.walkCount // 1], (self.x, self.y))
self.walkCount += 1
elif self.right:
win.blit(scarn_right[self.walkCount // 1], (self.x, self.y))
self.walkCount += 1
elif self.up:
win.blit(scarn_up[self.walkCount // 1], (self.x, self.y))
self.walkCount += 1
elif self.down:
win.blit(scarn_down[self.walkCount // 1], (self.x, self.y))
self.walkCount += 1
else:
if self.left:
win.blit(scarn_left[0], (self.x, self.y))
elif self.right:
win.blit(scarn_right[0], (self.x, self.y))
elif self.up:
win.blit(scarn_up[0], (self.x, self.y))
elif self.down:
win.blit(scarn_down[0], (self.x, self.y))
elif self.standing:
win.blit(scarn_forward_standing[0], (self.x, self.y))
pygame.display.update()
def apartment_movement(): # movement in the apartment / collision detection
keys = pygame.key.get_pressed()
collision = False
for wall in apartment_walls:
collision = scarn.scarn_hitbox.colliderect(wall)
if collision:
break
if collision:
scarn.left = False
scarn.right = False
scarn.up = False
scarn.down = False
scarn.standing = False
scarn.sleeping = False
print("Collide")
if not collision:
if keys[pygame.K_LEFT] and scarn.x > 110 - scarn.width - scarn.vel: # allows the player to move left
scarn.x -= scarn.vel
scarn.left = True
scarn.right = False
scarn.up = False
scarn.down = False
scarn.standing = False
scarn.sleeping = False
elif keys[pygame.K_RIGHT] and scarn.x < 795 - scarn.width - scarn.vel: # allows the player to move right
scarn.x += scarn.vel
scarn.right = True
scarn.left = False
scarn.up = False
scarn.down = False
scarn.standing = False
scarn.sleeping = False
elif keys[pygame.K_UP] and scarn.y > 130 - scarn.height - scarn.vel:
scarn.y -= scarn.vel
scarn.up = True
scarn.right = False
scarn.left = False
scarn.down = False
scarn.standing = False
scarn.sleeping = False
elif keys[pygame.K_DOWN] and scarn.y < 540 - scarn.height - scarn.vel:
scarn.y += scarn.vel
scarn.down = True
scarn.right = False
scarn.left = False
scarn.up = False
scarn.standing = False
scarn.sleeping = False
else: # clarifies the player is not moving left or right
scarn.walkCount = 0
# apartment walls
apartment_walls = [pygame.Rect(243, 60, 8, 275),
pygame.Rect(510, 60, 8, 275),
pygame.Rect(243, 421, 215, 5),
pygame.Rect(243, 330, 220, 5),
pygame.Rect(510, 421, 145, 5),
pygame.Rect(510, 330, 145, 5),
pygame.Rect(700, 421, 57, 5),
pygame.Rect(700, 330, 57, 5),
pygame.Rect(43, 410, 120, 10),
pygame.Rect(510, 335, 5, 90),
pygame.Rect(460, 335, 5, 90),
pygame.Rect(700, 335, 5, 90),
pygame.Rect(650, 335, 5, 90)]
You have to call
colliderect
on each wall individually. Remember to stop checking and break out of the loop once there's a collision.