【发布时间】:2019-12-07 06:03:13
【问题描述】:
我正在尝试在房间的墙壁和我的角色 Michael Scarn 之间进行碰撞检测。但是,没有检测到任何东西。我知道这一点,因为我告诉程序在发生碰撞时打印“碰撞”,但没有检测到任何东西。这是我的代码中的一个小错误还是一个更大的错误?任何人都可以解决这个问题吗?
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)]
【问题讨论】:
-
你传递的是一个矩形数组而不是一个矩形。
-
有没有办法让我保留列表并仍然修复错误?
-
你需要遍历它并单独调用
colliderect()。 -
我该怎么做?
-
欢迎来到 Stack Overflow。你能修复你的代码格式并创建一个minimal reproducible example吗?这将有助于其他人运行和测试您的代码并更好地理解您的问题。
标签: python python-3.x pygame pycharm