为什么我向左移动速度更快,而在pygame中向右移动?

发布于 2025-02-12 19:24:20 字数 997 浏览 0 评论 0原文

因此,每当我将播放器移到左侧时,我都会快速移动,但是当我向右移动时,我的移动速度非常慢,尽管速度相同,但我实现了Delat时间,但仍然相同?这是我的代码:

    if sticky_keys:
            if collide_sides == False:
                if move_left != '':
                    if kb.is_pressed(move_left):
                        rect[0].x -= speed * dt
                if move_right != '':
                    if kb.is_pressed(move_right):
                        rect[0].x += speed * dt 
                if move_up != '':
                    if kb.is_pressed(move_up):
                        rect[0].y -= speed * dt
                if move_down != '':
                    if kb.is_pressed(move_down):
                        rect[0].y += speed * dt
                if jump != '':
                    if jumped:
                        rect[0].y -= jump_velocity
                        jump_velocity -= 1
                        if jump_velocity < - 20:
                            jumped = False
                            jump_velocity = 20

so whenever i move my player to the left i move soo fast but when i move to the right i move very slow, although the speed is the same, i implemented delat time and it still is the same?? here is my code:

    if sticky_keys:
            if collide_sides == False:
                if move_left != '':
                    if kb.is_pressed(move_left):
                        rect[0].x -= speed * dt
                if move_right != '':
                    if kb.is_pressed(move_right):
                        rect[0].x += speed * dt 
                if move_up != '':
                    if kb.is_pressed(move_up):
                        rect[0].y -= speed * dt
                if move_down != '':
                    if kb.is_pressed(move_down):
                        rect[0].y += speed * dt
                if jump != '':
                    if jumped:
                        rect[0].y -= jump_velocity
                        jump_velocity -= 1
                        if jump_velocity < - 20:
                            jumped = False
                            jump_velocity = 20

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

世俗缘 2025-02-19 19:24:20

请参阅 pygame不允许我将float用于rect.move,但我需要它。因为 pygame.Rect 应该代表一个屏幕上的区域,pygame.Rect对象只能存储积分数据。

rect对象的坐标都是整数。 [...]

当动作添加到矩形的位置时,机芯的分数会丢失。结果,一个人向左移动的速度比向右移动的速度快:

int(10 - 1.5) == 8
int(10 + 1.5) == 11

将位置存储两次。一旦使用pos_xpos_y的浮点精度,以及rect [0]中的积分中的精度。更改pos_xpos_y,最后更新rect [0]带有圆形位置。

应用程序循环之前的初始化:

pos_x, pos_y = rect[0].topleft

在应用程序循环中:

if move_left != '':
    if kb.is_pressed(move_left):
        pos_x -= speed * dt
if move_right != '':
    if kb.is_pressed(move_right):
        pos_x += speed * dt 
if move_up != '':
    if kb.is_pressed(move_up):
        pos_y -= speed * dt
if move_down != '':
    if kb.is_pressed(move_down):
        pos_y += speed * dt
rect[0].topleft = (round(pos_x), round(pos_y))

See Pygame doesn't let me use float for rect.move, but I need it. Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data.

The coordinates for Rect objects are all integers. [...]

The fraction part of the movement gets lost when the movement is add to the position of the rectangle. As a result, one moves to the left faster than to the right:

int(10 - 1.5) == 8
int(10 + 1.5) == 11

Store the position twice. Once with floating point accuracy in pos_x and pos_y and in integral in rect[0]. Change pos_x and pos_y and finally update rect[0]with the rounded position.

Initialization before the application loop:

pos_x, pos_y = rect[0].topleft

In the application loop:

if move_left != '':
    if kb.is_pressed(move_left):
        pos_x -= speed * dt
if move_right != '':
    if kb.is_pressed(move_right):
        pos_x += speed * dt 
if move_up != '':
    if kb.is_pressed(move_up):
        pos_y -= speed * dt
if move_down != '':
    if kb.is_pressed(move_down):
        pos_y += speed * dt
rect[0].topleft = (round(pos_x), round(pos_y))
罪歌 2025-02-19 19:24:20

从这个代码来看,很难说,但是我认为这是pygame.Rect的舍入错误。 pygame.Rect只能存储int值,但是您很可能会提供浮点值。

From this code it is hard to tell, however I assume it's a rounding error with pygame.Rect. pygame.Rect can only store int values, but you're most likely providing float values.

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