开发者

Reading colors from a config file with ConfigParser to use with Pygame

开发者 https://www.devze.com 2022-12-28 04:25 出处:网络
In the config file I have the variable defined as BackgroundColor = 0,0,0 Which should work for the screen.fill settings for Pygame or any color argument for that matter. Where I can just do screen

In the config file I have the variable defined as

BackgroundColor = 0,0,0

Which should work for the screen.fill settings for Pygame or any color argument for that matter. Where I can just do screen.fill(0,0,0)

The problem I think is with this is that for integers read through 开发者_运维问答a configfile I have to put int() to convert the string to an int. For something like colors int doesnt work and I have no idea what should be used.

TypeError: invalid color argument

That's the error from python.


You've got a string representing the color, e.g. '0,0,0'. Use split(',') to split it into separate fields, then convert each one.

e.g.

color = '255, 255, 255'
red, green, blue = color.split(',')
red = int(red)
green = int(green)
blue = int(blue)

Or if you want to do it in one step and the comprehensions don't bother you:

color = '128, 128, 128'
red, green, blue = [int(c) for c in color.split(',')]
0

精彩评论

暂无评论...
验证码 换一张
取 消