Somebody Out There
Somebody Out There

Reputation: 31

Pygame: TypeError: Invalid end position argument

While running this code:

for p in self.particles :
       pos1 = p.pos
       pos2 = p.pos[0]+2*p.vel[0], p.pos[1]+2*p.vel[1]
       pygame.draw.line(self.screen, p.color, pos1 , pos2 , 2)

I keep getting this error:

TypeError: Invalid end position argument`

So I use a print p.pos statement to see what p.pos is and it magically works!
But the only problem is that printing it out makes the game run at like 10 frames a second...
So how do I fix the error?

Upvotes: 1

Views: 5710

Answers (2)

Vegard J.
Vegard J.

Reputation: 307

If you mean you're actually calling the print() function or statement a lot, then you should remove it to speed up your program.
Otherwise you're just giving your computer too much work, and may want to optimize your code.

For instance I see you are saying for p in self.particles,
If you're drawing a lot of particles to the screen, try reducing the number - this is more of a trade off than an optimization, but its an option.

Make sure your code is efficient where it counts
ie. where you make a lot of calculations, or call a function many times.

You may want to say:

drawline = pygame.draw.line

and call drawline() instead, as this saves look-up time

Upvotes: 1

ninMonkey
ninMonkey

Reputation: 7511

docs ref: pygame.draw.line(Surface, color, start_pos, end_pos, width=1)

So we need to do:

import pygame
# This makes event handling, rect, and colors simpler.
# Now you can refer to `Sprite` or `Rect()` vs `pygame.sprite.Sprite` or `pygame.Rect()`
from pygame.locals import *
from pygame import Color, Rect, Surface

def render():
    p1 = (100,100)
    p2 = (p1[0] + 2*v[0], p2[1] + 2*v[1] )
    pygame.draw.line(screen, Color("white"), p1, p2, width=1)

Note: Your code can be simplified by using vectors.

(Whether from numpy, or euclid). Code becomes: Where pos2, pos1, velocity are all vectors.

pos2 = pos1 + 2* velocity

Upvotes: 1

Related Questions