Python PyGame碰撞响应错误
我的问题: 当我在PyGame中处理碰撞响应时,一切正常,直到我与某个对角碰撞(xvel和yvel!=0)。如果我在检查各自轴时有一个print(“x”)和print(“y”)语句,我会得到如下结果: 很明显,这个bug是由中间的一个随机的“X”引起的,这使得代码的行为就像X轴上的字符碰撞时,实际上它是Y轴。这就是我的问题,为什么会这样 以下是我的碰撞响应函数:Python PyGame碰撞响应错误,python,pygame,2d,collision,Python,Pygame,2d,Collision,我的问题: 当我在PyGame中处理碰撞响应时,一切正常,直到我与某个对角碰撞(xvel和yvel!=0)。如果我在检查各自轴时有一个print(“x”)和print(“y”)语句,我会得到如下结果: 很明显,这个bug是由中间的一个随机的“X”引起的,这使得代码的行为就像X轴上的字符碰撞时,实际上它是Y轴。这就是我的问题,为什么会这样 以下是我的碰撞响应函数: def collide(self, direction): hits = pg.sprite.spritecollide(
def collide(self, direction):
hits = pg.sprite.spritecollide(self, all_sprites_wall, False, rectconverter)
if direction == "x":
if hits:
print("x")
if self.vx > 0:
self.hitboxrect.right = hits[0].hitboxrect.left
self.x = self.hitboxrect.right - self.rect.width - camera.camera.x
if self.vx < 0:
self.hitboxrect.left = hits[0].hitboxrect.right
self.x = self.hitboxrect.left - self.rect.width + self.hitboxrect.width - camera.camera.x
self.rect.x = self.x
if direction == "y":
if hits:
print("y")
if self.vy > 0:
self.hitboxrect.bottom = hits[0].hitboxrect.top
self.y = self.hitboxrect.bottom - self.rect.height - camera.camera.y
if self.vy < 0:
self.hitboxrect.top = hits[0].hitboxrect.bottom
self.y = self.hitboxrect.top - self.rect.height + self.hitboxrect.height - camera.camera.y
self.rect.y = self.y
def update(self):
self.move("x")
self.hitboxrect.x = self.x + camera.camera.x
self.rect.x = self.x
self.collide("x")
self.move("y")
self.hitboxrect.y = self.y + camera.camera.y + self.rect.height - self.hitboxrect.height
self.rect.y = self.y
self.collide("y")
def move(self, direction):
if direction == "x":
self.x += self.vx * dt
if direction == "y":
self.y += self.vy * dt
玩家移动功能:
def collide(self, direction):
hits = pg.sprite.spritecollide(self, all_sprites_wall, False, rectconverter)
if direction == "x":
if hits:
print("x")
if self.vx > 0:
self.hitboxrect.right = hits[0].hitboxrect.left
self.x = self.hitboxrect.right - self.rect.width - camera.camera.x
if self.vx < 0:
self.hitboxrect.left = hits[0].hitboxrect.right
self.x = self.hitboxrect.left - self.rect.width + self.hitboxrect.width - camera.camera.x
self.rect.x = self.x
if direction == "y":
if hits:
print("y")
if self.vy > 0:
self.hitboxrect.bottom = hits[0].hitboxrect.top
self.y = self.hitboxrect.bottom - self.rect.height - camera.camera.y
if self.vy < 0:
self.hitboxrect.top = hits[0].hitboxrect.bottom
self.y = self.hitboxrect.top - self.rect.height + self.hitboxrect.height - camera.camera.y
self.rect.y = self.y
def update(self):
self.move("x")
self.hitboxrect.x = self.x + camera.camera.x
self.rect.x = self.x
self.collide("x")
self.move("y")
self.hitboxrect.y = self.y + camera.camera.y + self.rect.height - self.hitboxrect.height
self.rect.y = self.y
self.collide("y")
def move(self, direction):
if direction == "x":
self.x += self.vx * dt
if direction == "y":
self.y += self.vy * dt
不知怎的,我自己找到了答案: 问题在于,我在加和减相机值,所以通过删除相机的x和y值,错误消失了。老实说,我真的不知道为什么这会解决它,但我会接受它。对于好奇的人来说,下面是代码的外观:
def collide(self, direction):
hits = pg.sprite.spritecollide(self, all_sprites_wall, False, rectconverter)
if direction == "x":
if hits:
print("x")
if self.vx > 0:
self.x = hits[0].hitboxrect.left - self.rect.width
self.hitboxrect.right = hits[0].hitboxrect.left
if self.vx < 0:
self.x = hits[0].hitboxrect.right - self.rect.width + self.hitboxrect.width
self.hitboxrect.left = hits[0].hitboxrect.right
self.rect.x = self.x
if direction == "y":
if hits:
print("y")
if self.vy > 0:
self.y = hits[0].hitboxrect.top - self.rect.height
self.hitboxrect.bottom = hits[0].hitboxrect.top
if self.vy < 0:
self.y = hits[0].hitboxrect.bottom - self.rect.height + self.hitboxrect.height
self.hitboxrect.top = hits[0].hitboxrect.bottom
self.rect.y = self.y
def碰撞(自身,方向):
hits=pg.sprite.spritecollide(self、all\u sprite\u wall、False、rectconverter)
如果方向==“x”:
如果点击:
打印(“x”)
如果self.vx>0:
self.x=hits[0]。hitboxrect.left-self.rect.width
self.hitboxrect.right=点击[0]。hitboxrect.left
如果self.vx<0:
self.x=hits[0].hitboxrect.right-self.rect.width+self.hitboxrect.width
self.hitboxrect.left=点击[0]。hitboxrect.right
self.rect.x=self.x
如果方向==“y”:
如果点击:
打印(“y”)
如果self.vy>0:
self.y=hits[0].hitboxrect.top-self.rect.height
self.hitboxrect.bottom=点击[0]。hitboxrect.top
如果self.vy<0:
self.y=hits[0].hitboxrect.bottom-self.rect.height+self.hitboxrect.height
self.hitboxrect.top=点击[0]。hitboxrect.bottom
self.rect.y=self.y
当你到达x时,到底发生了什么?@suppressionlayer这里有一张发生了什么的gif:@rabbi76你是对的,决定把问题重写得更清楚一点,忘了删除另一篇帖子,现在就删除了,我的错@Rabbid76添加了一些可能是问题所在的代码,还有什么需要添加的吗?