我正在寫一個簡單的pygame程序,只包括在屏幕上移動一個框。盒子移動速度非常快,我想知道如何控制速度。在我的代碼中,更新的位置被移動1而不是更小,因爲如果數字不是整數,它會使事情更加複雜。pygame中物體的速度?
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()
是的使用時鐘確實是你想要做的,如果你想在不同的計算機上類似的速度 - 如果你不想浪費整個處理器移動那個盒子 – kratenko