Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using color in python
I am creating a game and I want to include colour and I am setting variables to their appropriate value, I here 'color' has to be spelt like that. But I get the error message:

GREEN = color('#00FF00')
NameError: name 'color' is not defined

#colours being assigned
GREEN = color('#00FF00')
BLUE = color('#1E90FF')
RED = color('#FF0000')
YELLOW = color('#FFFF00')
BLACK = color('#000000')
WHITE = color('#FFFFFF')
ORANGE = color('#FFA500')
GOLD = color('#FFD700')
PINK = color('#FF69B4')
Any help is appreciated, thanks
Where is that color function supposed to come from? Your code doesn't have an import statement for it and it isn't a built-in function that's available by default, hence the error.
After I do that I now get this error:

GREEN = color('#00FF00')
TypeError: 'module' object is not callable
Given it's a function, it needs to be imported from the relevant module. There isn't a color module in the standard library, so I'm still not sure where you think this function is meant to come from. If you're using a third-party library, then consult its documentation.
I thought it was used in pygame which is already imported
Why aren't you showing the whole code then? It wastes people's time if they have to guess. If it's in the pygame module and that's imported, then you need to qualify the function with its module name (i.e. pygame.color) or use from (i.e. from pygame import color), as I already suggested.
You can do from pygame import Color or you can just call it pygame.Color. Also the reason you get that error is because it's "Color" and not "color"
Age Doesn't Matter, Your Passion Does
- Shreya Jain
FYI. pygame has over 400+ builtin colors.
import pygame
nilamo and metulburr like this post
99 percent of computer problems exists between chair and keyboard.
You really don't need that 'color' thing. You could also just do it like that:

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BROWN = (106, 55, 5)
DARKGREEN = (0, 100, 0, 255)
ORANGE = (255,100,10)

Then if you want to use the colour somewhere, e.g.


That's the way I am doing it in my game.
metulburr likes this post
I learn to work with what is supplied.
Why take all that time to define colors ? When there is predefine colors.

If you don't like working with strings. You can always wrap it.
import pygame

class PygameColors:
    def __init__(self):

    def add(self, name, r, g, b, a=255):
        if name not in vars(self).keys():
            setattr(self, name, pygame.Color(r, g, b, a))
            print("Color already exists:", name)

color = PygameColors()

print("white:", color.white)
print("darkgray:", color.darkgray)
print("darkgrey:", color.darkgrey)
print("gray16:", color.gray16)
print("yellow:", color.yellow)
print("brown:", color.brown)
print("darkgreen:", color.darkgreen)
Malt, metulburr, adt like this post
99 percent of computer problems exists between chair and keyboard.

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)