我正在尝试在房间的墙壁和我的角色迈克尔·斯卡尔恩之间进行碰撞检测。但是,没有检测到任何东西。我之所以知道这一点,是因为我告诉程序在发生冲突时打印“碰撞”,但是没有检测到任何东西。在我的代码中是小错误还是大错误?谁能解决这个问题?
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():#公寓中的移动/碰撞检测
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)]
答案 0 :(得分:3)
您必须分别在每个墙上呼叫colliderect
。请记住,一旦发生冲突,请停止检查并退出循环。
collision = False
for wall in apartment_walls:
collision = scarn.scarn_hitbox.colliderect(wall)
if collision == True: break
if collision:
scarn.left = False
scarn.right = False
scarn.up = False
scarn.down = False
scarn.standing = False
scarn.sleeping = False
if not collision:
# etc