Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
ValueError: argumento de color no válido en mi código pygame

Escribí el código para mi proyecto final. Es un código que hace un rebote en la pared. Quería hacer dos bolas, así que creé una clase y creé varias funciones. Pero creo que mi código no funciona.

 ValueError: invalid color argument in my pygame code

 import pygame import random pygame.init() screensize = (800, 600) screen = pygame.display.set_mode(screensize) pygame.display.set_caption("Final project") WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) radius = 25 # 공의 크기 color_list = [RED, BLUE, GREEN] x = 0 y = 0 x1 = 0 y1 = 0 dx = 2 dy = 2 color = color_list class ball: def create_ball(self): self.x = random.randint(radius, screensize[0]-radius) self.y = random.randint(radius, screensize[1]-radius) self.x1 = random.randint(radius, screensize[0]-radius) self.y1 = random.randint(radius, screensize[1]-radius) self.color = random.choice(color_list) def move_ball(self): self.x = x + dx self.y = y + dy if not radius < x < screensize[0]-radius: dx = -dx color = random.choice(color_list) elif not radius < y < screensize[1]-radius: dy = -dy color = random.choice(color_list) def move_ball1(self): self.x1 = x1 + dx self.y1 = y1 + dy if not radius < x1 < screensize[0]-radius: dx = -dx color = random.choice(color_list) elif not radius < y1 < screensize[1]-radius: dy = -dy color = random.choice(color_list) b = ball() print(b) go = True while go: clock = pygame.time.Clock() clock.tick(150) for event in pygame.event.get(): if event.type == pygame.QUIT: go = False screen.fill(WHITE) pygame.draw.circle(screen, color, (x, y), radius) pygame.draw.circle(screen, color, (x1, y1), radius) pygame.display.flip()
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

De acuerdo con los documentos de Pygame , la función pygame.draw.circle() toma el argumento de color como int o tuple , no como list . Así que debería verse como:

 pygame.draw.circle(screen, BLUE, (x, y), radius)
over 4 years ago · Santiago Trujillo Report

0

El color debe ser un valor de color. Por lo tanto, debe seleccionar un color aleatorio de la lista de colores:

color = color_list

 color = random.choice(color_list)

Lea acerca de las clases y los objetos de instancia y utilícelos de la manera correcta. Solo necesita 1 clase de Ball con 1 move y 1 método de draw . Sin embargo, puede crear tantas instancias de esta clase como desee (p. ej., b1 y b2 ):

 import pygame import random pygame.init() screensize = (800, 600) screen = pygame.display.set_mode(screensize) pygame.display.set_caption("Final project") WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) radius = 25 # 공의 크기 color_list = [RED, BLUE, GREEN] class Ball: def __init__(self): self.x = random.randint(radius, screensize[0]-radius) self.y = random.randint(radius, screensize[1]-radius) self.dx = 2 self.dy = 2 self.color = random.choice(color_list) def move(self): self.x += self.dx self.y += self.dy if not radius < self.x < screensize[0]-radius: self.dx = -self.dx self.color = random.choice(color_list) elif not radius < self.y < screensize[1]-radius: self.dy = -self.dy self.color = random.choice(color_list) def draw(self, surf): pygame.draw.circle(surf, self.color, (self.x, self.y), radius) b1 = Ball() b2 = Ball() go = True while go: clock = pygame.time.Clock() clock.tick(150) for event in pygame.event.get(): if event.type == pygame.QUIT: go = False b1.move() b2.move() screen.fill(WHITE) b1.draw(screen) b2.draw(screen) pygame.display.flip()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!