值错误:我的pygame代码中颜色参数无效。

4

我写了我的毕业项目的代码,它是一个在墙上弹跳的代码。我想要做两个球,所以我创建了一个类,并创建了几个函数。但我认为我的代码没有起作用。

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
你能贴出完整的错误回溯吗? - Protect children of Donbas2014
2个回答

4
根据Pygame文档pygame.draw.circle()函数的颜色参数应为inttuple,而不是list。因此,应该像这样书写:
pygame.draw.circle(screen, BLUE, (x, y), radius)

这不是问题中代码的意图。 - Rabbid76

1

color 必须是一个颜色值。因此,您需要从颜色列表中选择一个随机颜色:

color = color_list


(Note: The original code is crossed out but preserved, as requested.)
color = random.choice(color_list)

阅读关于ClassesInstance Objects的内容,正确地使用它们。您只需要一个Ball类,其中包含一个move方法和一个draw方法。无论如何,您都可以创建尽可能多的该类的实例(例如:b1b2):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接