简体   繁体   English

pygame的/ Python的。 左移动似乎比右移动快。 为什么?

[英]Pygame/Python. Left seems to move faster than right. Why?

I've coded a bit c++ and java during the past few years and now i've started with python. 在过去的几年中,我已经对c ++和java进行了一些编码,现在我开始使用python。 The goal is to make a game. 目标是制作游戏。 I've noticed, however, that at the basic animation part of a player moving left and right, my left "speed" is faster than the right "speed". 但是,我注意到,在玩家左右移动的基本动画部分,我的左“速度”比右“速度”快。 Although my "speed" values are exactly the same. 尽管我的“速度”值完全相同。

The update function: 更新功能:

def update(self,dt,game):
    last=self.rect.copy()


    self.time+=dt


    if self.left:
       self.rect.x-=300*dt
    if self.right:
       self.rect.x+=300*dt

I've noticed that if i change the left speed to 250, instead of 300, it will run the same. 我注意到,如果我将左速度更改为250,而不是300,它将运行相同。 For example if i press both left and right keys the player will stay on the exact same spot. 例如,如果我同时按下左右键,则播放器将停留在完全相同的位置。 However if i have the left and right speed both at 300. The player will move slowly to the left. 但是,如果我的左右速度均为300。播放器将缓慢向左移动。

My game loop: 我的游戏循环:

while 1:
        dt=clock.tick(60)
        if dt>1000/60.0:
            dt=1000/60.0

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                return
            if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
                return
            if event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
                self.player.left=True
                self.player.image=self.player.leftimages[1]
            if event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
                self.player.right=True
                self.player.image=self.player.rightimages[1]
            if event.type==pygame.KEYUP and event.key==pygame.K_LEFT:
                self.player.left=False
                self.player.image=self.player.leftimages[0]
            if event.type==pygame.KEYUP and event.key==pygame.K_RIGHT:
                self.player.right=False
                self.player.image=self.player.rightimages[0]

        self.tilemap.update(dt/1000.0,self)
        screen.fill((0,0,0)) 
        self.tilemap.draw(screen)
        pygame.display.update()

I'm using a tmx library from Richard Jones to be able to import my map made in tiled map editor. 我正在使用Richard Jones的tmx库,以导入在平铺地图编辑器中制作的地图。 I hope this is sufficient information to answer my weird question. 我希望这是足够的信息,可以回答我的怪异问题。 I've asked my other programming friend and he finds it weird. 我问过我的另一个编程朋友,他觉得很奇怪。

Some code might be off, beacause i've just copy pasted from my original code that has some more stuff into it. 某些代码可能已关闭,因为我刚刚从原始代码中复制并粘贴了一些其他内容。 But i've extracted the most basic parts that control the movement. 但是我提取了控制运动的最基本的部分。

Thank you for your time! 感谢您的时间!

Have you tried printing the value of dt from your update function? 您是否尝试过通过update功能打印dt的值? If the speed is different when you move left from when you move right, then that is what must be changing (backed up by you saying that holding left and right at the same time results in no movement). 如果左移时的速度与右移时的速度不同,则这是必须改变的(通过说同时按住左和右将导致不动)来支持。

Based on my own testing, it looks like pygame.time.Clock.tick() returns an integer value representing the number of milliseconds that have passed since the last call. 根据我自己的测试,看起来pygame.time.Clock.tick()返回一个整数值,表示自上次调用以来已过去的毫秒数。 Where you attempt to correct this value ( dt=1000/60.0 ), you will be getting a floating point value. 在尝试更正此值( dt=1000/60.0 )的地方,将获得浮点值。 I suspect that for some reason, the timer is off when either pressing left or right, and your attempts to correct it manually are causing a different dt value to be passed in to the update function during these situations. 我怀疑由于某种原因,在按向左或向右时,计时器会关闭,而您尝试手动进行校正的原因是,在这些情况下,会将不同的dt值传递给update功能。

The pygame documentation suggests pygame.time.Clock.tick_busy_loop() if you need a more accurate (but CPU intensive) timer. pygame文档建议pygame.time.Clock.tick_busy_loop()如果您需要更准确的计时器(但pygame.time.Clock.tick_busy_loop()大量CPU))。

http://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick_busy_loop http://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick_busy_loop

Instead of calculating the change in time since the last frame and then basing how far the player will move off of that, you should call pygame.time.Clock.tick once a frame. 与其计算自上一帧以来的时间变化,然后根据玩家将移开该帧的时间, pygame.time.Clock.tick ,您应该每帧调用一次pygame.time.Clock.tick Then you can assume that the time between frames will be the same every time, so dt will always be the same. 然后,您可以假设帧之间的时间每次都相同,因此dt将始终相同。

As of now if there is a drop in the fps, everything else in your program will slow down, but your player will start to jump ahead faster than everything else. 截至目前,如果fps下降,则程序中的其他所有内容都将减慢速度,但播放器将比其他所有内容开始更快地跳跃。 Rather than relying on time elapsed, it's usually best to depend on the fps. 最好不要依赖于经过的时间,而是最好取决于fps。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的pygame精灵的左走比右走快? - why does left go faster than right for my pygame sprite? pygame - 为什么玩家的移动速度比相机快? - pygame - Why player move faster than camera? 为什么我在 pygame 中向左移动更快而向右移动非常慢? - why am i moving faster to the left and very slow to the right in pygame? 为什么我的立方体向左移动比向右移动更快? - Why is my cube moving faster to the left than to the right? pygame 旋转时船舶上下左右移动快于上下 - Ship moves up and left faster than down and right when rotating in pygame Pygame 角色不会向左或向右移动 - Pygame Character wont move left or right 在 Python 中删除二叉树节点。 为什么 self != parent.left 或 parent.right? - Deleting a binary tree node in Python. Why self != parent.left or parent.right? 从上到下,从左到右对页面(2D 平面)上的框进行排序。 (Python) - Sorting Boxes on a Page(2D Plane) top to bottom, left to right. (Python) 在 python 中使用 fpdf 创建 pdf。 无法循环向右移动图像 - create pdf with fpdf in python. can not move image to the right in a loop 无法在 pygame 中左右移动船舶的“功能” - Can't get 'function' to move Ship left and right in pygame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM