Python3 Pygame:draw.circle 的元组类型错误

Python3 Pygame: Tuple Type Error for draw.circle

我是初学者,如有不全之处还请见谅。

我试图通过调用“粒子”对象的显示方法用 pygame 画一个圆;我需要这个对象,因为我将创建其中的许多对象。我得到的错误(“TypeError:需要一个整数(得到类型元组)”)指的是行“pygame.draw.circle”。

由于我真的无能为力,所以我在下面包含了完整的代码。

代码:

import pygame, sys
pygame.init()

# COLORS
BLACK = (0,0,0)
WHITE = (255,255,255)

# SCREEN
scr_width = 1400
scr_height = 600
scr_bckgr_color = WHITE
screen = pygame.display.set_mode((scr_width,scr_height))
pygame.display.set_caption("Animation")
screen.fill(scr_bckgr_color)

#PARTICLE CLASS
class Particle:
    def __init__(self, position, radius, color):
        self.x = position[0],
        self.y = position[1],
        self.radius = radius,
        self.color = color,
        self.thickness = 0

    def display(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius, self.thickness)

# DRAW PARTICLES
Particle1 = Particle((200, 200), 15, BLACK)
Particle1.display()

#pygame.draw.circle(screen, BLACK, (200,200), 15, 0)
pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

您需要删除行尾的 ,

self.x = position[0],
self.y = position[1],
self.radius = radius,
self.color = color,

当你在一行的末尾放置一个 , 时,就会构造一个元组对象。元组不是由逗号运算符组成的。参见 Python - 5.3. Tuples and Sequences

删除逗号以修复您的代码:

class Particle:
    def __init__(self, position, radius, color):
        self.x = position[0]
        self.y = position[1]
        self.radius = radius
        self.color = color
        self.thickness = 0