滚动时,为什么我的播放器小故障会伸到平台的顶部?
请注意,我是Pygame的新手。我想知道,当我的播放器在X轴上滚动时与他们相撞时,为什么播放器在平台上的故障。而且仅在滚动时,而不是相机静态时。同样,垂直碰撞效果很好:只有在水平移动时才会发生错误。我试图切换垂直和水平碰撞的顺序,但后来只是使垂直碰撞错误。当玩家与墙壁碰撞时,我还试图将播放器稍微移动到相反的方向,但问题仍然存在。我还试图通过滚动X速度将播放器移回,但这也没有用。我也试图将水平碰撞放在滚动X功能中,但这也失败了。有什么建议吗?
这是滚动X和Y函数:
def scroll_x(self):
player = self.player.sprite
player_x = player.rect.centerx
direction_x = player.direction.x
if player_x < screen_width / 4 and direction_x < 0:
self.world_shiftx = 8
player.speed = 0
elif player_x > screen_width - (screen_width / 4) and direction_x > 0:
self.world_shiftx = -8
player.speed = 0
else:
self.world_shiftx = 0
player.speed = 8
def scroll_y(self):
player = self.player.sprite
player_y = player.rect.y
direction_y = player.direction.y
if player_y < screen_height / 3 and direction_y < 0:
self.world_shifty = player.direction.y * -1
elif player_y > screen_height - (screen_height / 3) and direction_y > 0:
self.world_shifty = player.direction.y * -1
self.vertical_movement_collision()
else:
self.world_shifty = 0
碰撞函数:
def horizontal_movement_collision(self):
player = self.player.sprite
player.rect.x += player.direction.x * player.speed
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(player.rect):
if player.direction.x < 0:
player.rect.left = sprite.rect.right
elif player.direction.x > 0:
player.rect.right = sprite.rect.left
def vertical_movement_collision(self):
player = self.player.sprite
player.apply_gravity()
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(player.rect):
if player.direction.y > 0:
player.rect.bottom = sprite.rect.top
player.direction.y = 0
elif player.direction.y < 0:
player.rect.top = sprite.rect.bottom
player.direction.y = 0
执行功能的顺序:
def run(self):
player = self.player.sprite
self.tiles.update(self.world_shiftx, self.world_shifty)
self.tiles.draw(self.display_surface)
self.scroll_x()
self.scroll_y()
self.player.update()
player.rect.y += player.direction.y + self.world_shifty
self.vertical_movement_collision()
self.horizontal_movement_collision()
self.player.draw(self.display_surface)
如果需要,则是播放器重力函数:
def apply_gravity(self):
if not self.direction.y < -16:
self.direction.y += self.gravity
elif self.direction.y < -16:
self.direction.y = -16
以及播放器跳跃功能:
def jump(self):
if self.jumping == "false":
if self.direction.y == 0:
self.jumping = "true"
self.direction.y = self.jump_speed
else:
self.jumping = "false"
预先感谢!
Take note that I'm new to pygame. I was wondering why my player glitches on top of my platforms when it collides with them while scrolling on the x axis. And it's only while scrolling, not when the camera is static. Also, the vertical collisions work perfectly fine: it's only when moving horizontally that the bug happens. I've tried to switch the order of the vertical and horizontal collisions, but then it just makes the vertical collisions bug. I've also tried to move the player a bit to the opposite direction when it collides with a wall, but the problem remained. I also tried to move the player back by the scroll x speed, but that also didn't work. I tried to put the horizontal collisions in the scroll x function too, but that also failed. Any advice please?
Here's the scroll x and y functions:
def scroll_x(self):
player = self.player.sprite
player_x = player.rect.centerx
direction_x = player.direction.x
if player_x < screen_width / 4 and direction_x < 0:
self.world_shiftx = 8
player.speed = 0
elif player_x > screen_width - (screen_width / 4) and direction_x > 0:
self.world_shiftx = -8
player.speed = 0
else:
self.world_shiftx = 0
player.speed = 8
def scroll_y(self):
player = self.player.sprite
player_y = player.rect.y
direction_y = player.direction.y
if player_y < screen_height / 3 and direction_y < 0:
self.world_shifty = player.direction.y * -1
elif player_y > screen_height - (screen_height / 3) and direction_y > 0:
self.world_shifty = player.direction.y * -1
self.vertical_movement_collision()
else:
self.world_shifty = 0
The collision functions:
def horizontal_movement_collision(self):
player = self.player.sprite
player.rect.x += player.direction.x * player.speed
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(player.rect):
if player.direction.x < 0:
player.rect.left = sprite.rect.right
elif player.direction.x > 0:
player.rect.right = sprite.rect.left
def vertical_movement_collision(self):
player = self.player.sprite
player.apply_gravity()
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(player.rect):
if player.direction.y > 0:
player.rect.bottom = sprite.rect.top
player.direction.y = 0
elif player.direction.y < 0:
player.rect.top = sprite.rect.bottom
player.direction.y = 0
And the order the functions are executed:
def run(self):
player = self.player.sprite
self.tiles.update(self.world_shiftx, self.world_shifty)
self.tiles.draw(self.display_surface)
self.scroll_x()
self.scroll_y()
self.player.update()
player.rect.y += player.direction.y + self.world_shifty
self.vertical_movement_collision()
self.horizontal_movement_collision()
self.player.draw(self.display_surface)
And if you need it, here's the player gravity function:
def apply_gravity(self):
if not self.direction.y < -16:
self.direction.y += self.gravity
elif self.direction.y < -16:
self.direction.y = -16
As well as the player jump function:
def jump(self):
if self.jumping == "false":
if self.direction.y == 0:
self.jumping = "true"
self.direction.y = self.jump_speed
else:
self.jumping = "false"
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
scroll_y
函数会导致vertical_movement_collision
函数在瓷砖的位置根据滚动移动后执行,但在播放器的位置移动之前。向下滚动时,这可能会导致玩家的顶部在其中一个平台内无意间。由于玩家的Y方向也是正的(因为玩家的跌倒需要滚动),因此满足vertical_movement_collision
功能的所有条件,以将播放器放置在平台的顶部。可以通过不调用
vertical_movement_collision
来解决此问题。这可以通过以下解决方案之一来实现:self.vertical_movement_collision()
从scroll_y
函数的行中删除。player.Rect.y + = player.Direction.y + self.world_shifty
line放置在self.scroll_y
呼叫之前。self.tiles.update(self.world_shiftx,self.world_shifty)放置
呼叫。以上任何功能都可能导致程序中其他任何地方都出现意外错误。如果是这样,请尝试其他解决方案。
The
scroll_y
function causes thevertical_movement_collision
function to execute after the tiles' position is shifted according to scroll, but before the player's position is shifted. When scrolling downwards, this can cause the top of the player to be unintentionally inside of one of the platforms. As the player's y direction is also positive (because the player's fall requires scrolling), all conditions are met for thevertical_movement_collision
function to place the player at the top of the platform.This problem can be solved by not calling
vertical_movement_collision
before the player's position is shifted. This can be implemented by one of the following solutions:self.vertical_movement_collision()
line from thescroll_y
function.player.rect.y += player.direction.y + self.world_shifty
line before theself.scroll_y
call.self.tiles.update(self.world_shiftx, self.world_shifty)
andself.tiles.draw(self.display_surface)
lines after theself.scroll_y
call.It is possible that any of the above functions causes an unexpected error anywhere else in the program. If so, try another solution.