1

I am trying to make a grid, and when any object on the grid is clicked on, it changes to a certain color based on whatever I need it to

When I do that, I get a ValueError, saying that my color argument is invalid
However, I'm using constants for my colors, and the constants are set to tuples, which seems to be the right type. I have looked online, but everyone's problems were that they were not passing in a tuple, but I am pretty sure I am

Here are my constants, then all the code

WIDTH = 800
WIN = pygame.display.set_mode((WIDTH, WIDTH))
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PURPLE = (128, 0, 128)
ORANGE = (255, 265, 0)
GREY = (128, 128, 128)
TURQUOISE = (64, 224, 208)
main(WIN, WIDTH)
def main(win, width):
   while run:
     draw(win, grid, ROWS, width)
def draw(win, grid, rows, width):
  for row in grid:
    for node in row:
      node.draw(win)
class Node:
  def __init__(self, row, col, width): #there are other arguments, but not needed
    self.row = row
    self.col = col
    self.x = row * width
    self.y = col * width
    self.width = width
    self.color = WHITE 
  def draw(self, win):
    pygame.draw.rect(win, (self.color), (self.x, self.y, self.width, self.width))
#There are functions that change the nodes color like these
  def make_closed(self):
    self.color = RED
Traceback (most recent call last):
  File "--", line 154, in <module>
    main(WIN, WIDTH)
  File "--", line 118, in main
    draw(win, grid, ROWS, width)
  File "--", line 93, in draw
    node.draw(win)
  File "--", line 58, in draw
    pygame.draw.rect(win, (self.color), (self.x, self.y, self.width, self.width))
ValueError: invalid color argument

I'm new to Stack Overflow, so any input on how to better ask questions would be greatly appreciated, and let me know if you need anything else! (My on-click function is a bit more complicated than needed for testing, but I can also put that code in there) There are file names, I removed them. Thanks for your help in advance!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Neel Kumar
  • 49
  • 5
  • 2
    Put a `print(self.color)` right before the `draw.rect` line, to see exactly what it is that you're passing as the color parameter. – jasonharper Mar 14 '21 at 02:34
  • @jasonharper thanks, this helped me figure it out. As you can see, one of my colors in the constants was not set correctly, as orange is set to 255, 265, 0, instead of 255, 165, 0. I feel very stupid now – Neel Kumar Mar 14 '21 at 18:01

0 Answers0