Reputation: 673
I'm introducing myself to python and the pygame library. I wanted to try a very basic particle emitter first, but I'm getting an error.
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
running = True
color = (128, 128, 128)
radius = 10
class Particle:
"""Represents a particle."""
def __init__(self, pos, vel):
self.pos = pos
self.vel = vel
def move(self):
self.pos += self.vel
def draw(self):
pygame.draw.circle(screen, color, self.pos, radius)
particles = list()
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
if len(particles) < 10:
randvel = random.randint(-5, 5), random.randint(-5, 5)
p = Particle((0, 0), randvel)
particles.append(p)
for p in particles:
p.move()
p.draw()
pygame.display.flip()
clock.tick(60)
To my understanding, this error says 2 arguments must be passed, not 4.
Traceback (most recent call last):
File "D:\data\eclipse\pythonscrap\main.py", line 44, in <module>
p.draw()
File "D:\data\eclipse\pythonscrap\main.py", line 24, in draw
pygame.draw.circle(screen, color, self.pos, radius)
TypeError: must be sequence of length 2, not 4
I suspect this is a common error with beginners, because it's easy to forget an extra set of ()
to make a tuple. But as I look at the documentation, I find that the method's signature is:
circle Found at: pygame.draw
pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect
draw a circle around a point
That's five arguments, with one given a default value. So what is going wrong here?
Upvotes: 0
Views: 2163
Reputation: 1121884
Your argument count is correct. It is the pos
tuple that is wrong, it has length 4 instead of 2.
By adding self.vel
you add new elements to the tuple, not sum the coordinates:
self.pos += self.vel
Sum the individual coordinates instead:
self.pos = (self.pos[0] + self.vel[0], self.pos[1] + self.vel[1])
A quick demo to illustrate the problem:
>>> pos = (0, 0)
>>> vel = (1, 1)
>>> pos += vel
>>> pos
(0, 0, 1, 1)
Upvotes: 4