【发布时间】:2026-01-19 01:55:01
【问题描述】:
我正在为一款游戏尝试物理,并想尝试如何加快速度。
我当前的代码是这样的
def left(self):
self.x -= 1 * self.xvelocity
if not self.xvelocity == 10:
self.xvelocity += 1
def right(self):
self.x += 1 * self.xvelocity
if not self.xvelocity == 10:
self.xvelocity += 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_LEFT]:
player.left()
if keys_pressed[pygame.K_RIGHT]:
player.right()
如果释放了左右键,我想将 xvelocity 重置回 0。目前这还没有发生,所以如果你向右移动并加速,然后向左移动,你仍然有相同的速度。
我尝试通过添加以下内容来修复它:
def left(self, *args):
if args:
self.x -= 1 * self.xvelocity
if not self.xvelocity == 10:
self.xvelocity += 1
else:
self.xvelocity = 0
def right(self, *args):
if args:
self.x += 1 * self.xvelocity
if not self.xvelocity == 10:
self.xvelocity += 1
else:
self.xvelocity = 0
但是如果你改变方向,我无法获得必须再次加速的效果。我错过了什么吗?
【问题讨论】: