我正在编写一个简单的 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()