为什么我向左移动速度更快,而在pygame中向右移动?
因此,每当我将播放器移到左侧时,我都会快速移动,但是当我向右移动时,我的移动速度非常慢,尽管速度相同,但我实现了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅 pygame不允许我将float用于rect.move,但我需要它。因为
pygame.Rect
应该代表一个屏幕上的区域,pygame.Rect
对象只能存储积分数据。当动作添加到矩形的位置时,机芯的分数会丢失。结果,一个人向左移动的速度比向右移动的速度快:
将位置存储两次。一旦使用
pos_x
和pos_y
的浮点精度,以及rect [0]
中的积分中的精度。更改pos_x
和pos_y
,最后更新rect [0]
带有圆形位置。应用程序循环之前的初始化:
在应用程序循环中:
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, apygame.Rect
object can only store integral data.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:
Store the position twice. Once with floating point accuracy in
pos_x
andpos_y
and in integral inrect[0]
. Changepos_x
andpos_y
and finally updaterect[0]
with the rounded position.Initialization before the application loop:
In the application loop:
从这个代码来看,很难说,但是我认为这是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.