4

I wrote the code for my final project. It's a code that makes a bouncing on the wall. I wanted to make two balls, so I created a class and created several functions. But I think that my code is not working.

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() 
1

2 Answers 2

4

According to Pygame docs, pygame.draw.circle() function takes color argument as int or tuple, not list. So it should look like:

pygame.draw.circle(screen, BLUE, (x, y), radius)
1
  • This is not the intent of the code in the question.
    – Rabbid76
    Dec 16, 2021 at 7:56
1

The color needs to be a color value. Therefore you have to select a random color from the list of colors:

color = color_list

color = random.choice(color_list)

Read about Classes and Instance Objects and use them in the correct way. You just need 1 Ball class with 1 move and 1 draw method. However, you can create as many instances of this class as you want (e.g.: b1 and 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() 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.