滚动时,为什么我的播放器小故障会伸到平台的顶部?

发布于 2025-02-01 17:59:39 字数 5012 浏览 4 评论 0原文

请注意,我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-02-08 17:59:39

scroll_y函数会导致vertical_movement_collision函数在瓷砖的位置根据滚动移动后执行,但在播放器的位置移动之前。向下滚动时,这可能会导致玩家的顶部在其中一个平台内无意间。由于玩家的Y方向也是正的(因为玩家的跌倒需要滚动),因此满足vertical_movement_collision功能的所有条件,以将播放器放置在平台的顶部。

可以通过不调用vertical_movement_collision来解决此问题。这可以通过以下解决方案之一来实现:

  1. 删除self.vertical_movement_collision()scroll_y函数的行中删除。
  2. player.Rect.y + = player.Direction.y + self.world_shifty line放置在self.scroll_y呼叫之前。
  3. self.tiles.update(self.world_shiftx,self.world_shifty)放置呼叫。

以上任何功能都可能导致程序中其他任何地方都出现意外错误。如果是这样,请尝试其他解决方案。

The scroll_y function causes the vertical_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 the vertical_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:

  1. Remove the self.vertical_movement_collision() line from the scroll_y function.
  2. Place the player.rect.y += player.direction.y + self.world_shifty line before the self.scroll_y call.
  3. Place the self.tiles.update(self.world_shiftx, self.world_shifty) and self.tiles.draw(self.display_surface) lines after the self.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文