【问题标题】:Speed of an object in pygame?pygame中对象的速度?
【发布时间】:2019-06-28 21:06:50
【问题描述】:

我正在编写一个简单的 pygame 程序,它只包含在屏幕上移动一个框。盒子移动得很快,我想知道如何控制速度。在我的代码中,更新后的位置移动了 1 而不是更小,因为如果数字不是整数,它会使事情变得更复杂。

import os, sys
import pygame
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()

WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption("Box")

BLACK = (0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
size1 = 20
size2 = 2
#character = pygame.Rect(30, 30, 20, 30)
player = pygame.Surface((40,40))




pos1 = 100
pos2 = 100


MOVESPEED = 6

x = 1

while True:
    if pos1 == WINDOWWIDTH - 40 and pos1 > 0:
        pos1 -= 1
        x += 1
    elif pos1 < WINDOWWIDTH - 40 and x == 1:
        pos1 += 1
    elif x ==2:
        pos1 -= 1


    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:

                pos1 -= 5
            if event.key == K_RIGHT:
                pos1 += 4


    windowSurface.fill(WHITE)

    #screen.blit(character)



    windowSurface.blit(player, (pos1, pos2))
    pygame.display.update()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您应该将以下代码放入“while True:”循环中的某处:

    clock.tick([insert fps here])
    

    并将其放在循环之前的某个位置:

    clock=pygame.time.Clock()
    

    这将不允许循环运行超过您每秒输入的次数,并有望减慢立方体的速度。

    【讨论】:

    • 这是唯一真正有用的答案。为什么移动一个小于一个像素的对象,这没有意义?添加 FPS 限制肯定会解决问题。
    【解决方案2】:

    只是不要在循环的每次迭代中改变位置。

    代替

    while True:
      if ... :
        pos1 += 1
      ...
    

    像这样使用一些东西:

    tmp = 0
    
    while True:
      if ... :
        tmp += 1
        if tmp == 10:
          pos1 += 1
          tmp = 0
      ... 
    

    tmp = 0
    
    while True:
      if ... and not tmp % 10:
          pos1 += 1
      ... 
    

    您将10 调整为适合您的值。

    此外,您可能希望使用Clock 限制程序的帧速率以获得(或多或少)恒定帧速率。

    【讨论】:

    • 是的,如果您想在不同的计算机上获得相似的速度,并且不想浪费整个处理器来移动那个盒子,那么使用时钟绝对是您想要做的事情
    【解决方案3】:

    毕竟您可以使用浮点数来存储位置。将 while 循环中的更新值更改为更小的值,例如pos1 += 0.25。然后只需确保 blit 整数:windowSurface.blit(player, (int(pos1), int(pos2)))

    【讨论】:

      【解决方案4】:

      我通常也对大多数位置使用整数,但是当我说 pygame 将我的对象/精灵/...绘制到屏幕上时,我总是将位置除以 10,这样我就有 10 步的值,因为对象移动了一步屏幕上。

      组织起来并不难。

      【讨论】:

        【解决方案5】:

        您可以拥有的最小速度是 1,但有一种方法可以通过控制时间来进一步减慢速度。

        例如,每 100 毫秒而不是每帧以速度 1 移动框会使移动看起来明显变慢。

        参见下面的实现:

        # initialize clock object outside of main loop
        clock = pygame.time.Clock()
        time = 0
        
        while 1:
        
            time = time + clock.get_time()
            if time >= 100:
        
                # Your movement implementation here
        
                time = 0 # Reset timer at the end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-21
          • 2015-07-24
          • 2020-05-14
          • 2019-05-09
          • 1970-01-01
          • 1970-01-01
          • 2019-05-09
          • 1970-01-01
          相关资源
          最近更新 更多