Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using color in python
#1
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
Quote
#2
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.
Quote
#3
After I do that I now get this error:

GREEN = color('#00FF00')
TypeError: 'module' object is not callable
Quote
#4
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.
Quote
#5
I thought it was used in pygame which is already imported
Quote
#6
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.
Quote
#7
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
Quote
#8
FYI. pygame has over 400+ builtin colors.
import pygame
print(pygame.Color("green"))
print(pygame.Color("gold"))
print(pygame.Color("pink"))
nilamo and metulburr like this post
99 percent of computer problems exists between chair and keyboard.
hobby-programmer.com
Quote
#9
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.

BGCOLOR = DARKGREEN
screen.fill(BGCOLOR)

That's the way I am doing it in my game.
metulburr likes this post
Quote
#10
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):
        self.__dict__.update(pygame.color.THECOLORS)

    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))
        else:
            print("Color already exists:", name)

color = PygameColors()

print("white:", color.white)
print("black:", color.black)
print("darkgray:", color.darkgray)
print("darkgrey:", color.darkgrey)
print("gray16:", color.gray16)
print("green:", color.green)
print("red:", color.red)
print("yellow:", color.yellow)
print("brown:", color.brown)
print("darkgreen:", color.darkgreen)
print("orange:", color.orange)
Malt, metulburr, adt like this post
99 percent of computer problems exists between chair and keyboard.
hobby-programmer.com
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)