我想在显示器上为我的Pygame移动一些精灵。它是一条给定的路线,我想用向量移动它们。所以我得到了我的路线的位置,所以我得到了幅度和标题。你可以看到公式以及我想如何改变spirts的方式(使用" if")。我把矢量放在元组中,我认为这不正确。我收到错误
TypeError:不能将序列乘以类型' float'的非int。第51行
我认为这是我多元化的方式。但我希望你能理解我的想法,你可以帮助我
clock = pygame.time.Clock()
speed = 25.
position = (30.0,50.0)
magnitude = [509, 141, 128, 409, 293, 330, 251, 532]
heading = [(0.7,0.73),(0.71,0.71),(0.78,0.63),(-0.52,0.65),(0.97,0.24),(0.58,-0.82),(-0.88,-0.48),(0.08,-0.99)]
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0.0,0.0))
screen.blit(x1, position)
screen.blit(x2, (300,30))
screen.blit(x3, (500,30))
screen.blit(x4,(800,30))
time_passed = clock.tick()
time_passed_seconds = time_passed / 1000.0
route = time_passed_seconds * speed
position += heading[0] * route
if route >= magnitude[0]:
route = 0
route1 = time_passed_seconds * speed
position += heading[1] * route1
if route1 >= magnitude[1]:
route2 = time_passed_seconds * speed
position += heading[2] *route2
if route2 >= magnitude[2]:
route3 = time_passed_seconds * speed
position += heading[3] * route3
if route3 >= magnitude[3]:
route4 = time_passed_seconds * speed
position += heading[4] * route4
答案 0 :(得分:0)
您不想使用元组,因为您已经看到元组不能简单地乘以标量。 (参见Multiplying a tuple by a scalar)
有一些解决方法,但最简单的解决方案是使用numpy
,它支持您尝试做的数学运算。只需将矢量建模为numpy.array
即可。然后你可以用这种方式进行计算:
import numpy
clock = pygame.time.Clock()
speed = 25.
position = numpy.array((30.0,50.0))
magnitude = [509, 141, 128, 409, 293, 330, 251, 532]
heading = numpy.array([(0.7,0.73),(0.71,0.71),(0.78,0.63),(-0.52,0.65),(0.97,0.24),(0.58,-0.82),(-0.88,-0.48),(0.08,-0.99)])
route = time_passed_seconds * speed
position += heading[0] * route
我还假设你在最后一行意味着heading[0]
,因为添加整个列表似乎没有意义。