-2

I want to move some sprites for my Pygame over the display. Its a given route and I want to move them with vectors. So I got the positions of my route and so I got the magnitude and the heading. You can see the formulas and how I want to change the way of the spirts (with "if"). I put the vectors in tuples and I think thats not correctly. I get an error

TypeError: can't multiply sequence by non-int of type 'float' line 51

I think that is the way I mutliply the tuples. But I hope you get my idea behind this and you can help me

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
Skorab
  • 31
  • 1
  • 5

1 Answers1

0

You don't want to use tuples, as you've seen that tuples cannot be simply multiplied by a scalar. (Cf. Multiplying a tuple by a scalar)

There are workarounds, but the easiest solution would be to use numpy, which supports the mathematical operations you're trying to do. Simply model the vectors as a numpy.array. Then you can do your calculations this way:

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

I also assume you meant heading[0] in the last line, as adding the whole list doesn't seem to make sense.

Community
  • 1
  • 1
Isa
  • 751
  • 5
  • 6
  • Thank you for your reply. It worked. Now I wanna change the heading during the movement. Therefore I wanna make if-clauses saying that if route == magnitude[0]: /n position += heading[1] * speed – Skorab Jun 16 '16 at 15:40
  • The if clause you have in your example code should work without any modifications, so I didn't include it in my answer. Have you tried it? Did you run into any errors? – Isa Jun 16 '16 at 15:54
  • No it does not change directions. Maybe because the magnitude is not exactly the calculated route? – Skorab Jun 16 '16 at 16:03
  • I have not tried to understand your code semantically, but your statement `route == magnitude[0]` will only be true when `route` is exactly 509. Since you're adjusting `route` by the time that has passed, this is unlikely to ever be the case. You might therefore want to use a comparison such as `route >= magnitude[0]`. If you need further help, please edit your original question and describe what route, magnitude, heading are and what you're trying to achieve or open a new question. – Isa Jun 16 '16 at 16:08
  • I did it with comparsion. Magnitude is the absolute value of the vector. It means it in pixels. Thats the value of the way the sprite is going to move. And when it reaches the magnitude then it takes a new heading out of the list and counts a new line – Skorab Jun 16 '16 at 16:57
  • If this solved your problem, please mark the answer as accepted so others know your issue has been resolved. – Isa Jun 17 '16 at 09:13