Posts: 33
Threads: 11
Joined: Aug 2019
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
Posts: 1,838
Threads: 2
Joined: Apr 2017
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.
Posts: 33
Threads: 11
Joined: Aug 2019
Sep-14-2019, 03:47 PM
(This post was last modified: Sep-14-2019, 03:48 PM by Help_me_Please.)
After I do that I now get this error:
GREEN = color('#00FF00')
TypeError: 'module' object is not callable
Posts: 1,838
Threads: 2
Joined: Apr 2017
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.
Posts: 33
Threads: 11
Joined: Aug 2019
I thought it was used in pygame which is already imported
Posts: 1,838
Threads: 2
Joined: Apr 2017
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.
Posts: 479
Threads: 86
Joined: Feb 2018
Sep-14-2019, 07:59 PM
(This post was last modified: Sep-14-2019, 07:59 PM by SheeppOSU.)
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"
Posts: 544
Threads: 15
Joined: Oct 2016
Sep-15-2019, 11:21 AM
(This post was last modified: Sep-15-2019, 11:22 AM by Windspar.)
FYI. pygame has over 400+ builtin colors.
import pygame
print(pygame.Color("green"))
print(pygame.Color("gold"))
print(pygame.Color("pink"))
99 percent of computer problems exists between chair and keyboard.
Posts: 72
Threads: 14
Joined: Jul 2019
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.
Posts: 544
Threads: 15
Joined: Oct 2016
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)
99 percent of computer problems exists between chair and keyboard.
|