I'm trying to make a game of "pick the color not the word" in Python. There is a list of 9 different colors:
我正试图在Python中制作一个“选择颜色而不是单词”的游戏。有9种不同颜色的列表:
cnames = [" green ", " blue ", " yellow ", " gray ", " pink ", " orange ", " purple ", " red ", " brown "]
I have written a function wich randomizes these colors:
我写了一个随机化这些颜色的函数:
def randomize():
import random
cnames=["green", "blue","yellow", "grey", "pink", "orange", "Purple", "red", "brown"]
color=randint(0, len (cnames)-1)
cname=randint(0, len(cnames)-1)
from random import sample
other_cnames=random.sample(range(len(cnames)),6)
return (color, cname, other_cnames)
The code for the game window is:
游戏窗口的代码是:
win=GraphWin("New_game", 800,800)
color, cname, other_names=randomize()
rcname=cnames[cname]
rcname=rcname [0].upper() + rcname[1:]
target=Text(Point(300,100),rcname)
target.setOutline(cnames[color])
target.setStyle ("bold")
target.setSize(24)
target.draw(win)
Boxes=[]
k=0
How do I make a loop that fills the list boxes with text objects, where each text object is representing a color and the number of the color (0 to 8)?
如何创建一个用文本对象填充列表框的循环,其中每个文本对象表示颜色和颜色的数量(0到8)?
the result should look like this: enter image description here
结果应如下所示:在此处输入图像描述
2 个解决方案
#1
0
You could use a for statement to iterate code a number of times to fill boxes like:
您可以使用for语句多次迭代代码以填充以下框:
for x in range(*insert number of boxes*): *put other code here*
for x in range(* insert number of boxes *):*将其他代码放在这里*
This will fill it up if you just put the range in
如果您只是放入范围,这将填补它
#2
0
Not exactly sure what you have asked for, assuming you want to return color and color list without the color selected.
假设您想要在没有选择颜色的情况下返回颜色和颜色列表,则不确定您要求的内容。
cnames = ['green', 'blue'...]
to pick one color from list use random.choice
从列表中选择一种颜色使用random.choice
color = random.choice(cnames)
to get other colors init new list and pop by index
获取其他颜色初始化新列表并按索引弹出
other_colors = list(cnames)
other_colors.pop(colors.index(color))
return selected color and others
返回选定的颜色和其他
return color, other_colors
in graphwin
color, other_colors = randomize()
ucolor = '{0}{1}'.format(color[0].upper(), color[1:])
target = Text(Point(300,100),ucolor)
target.setOutline(color)
...
Boxes = other_colors #
#1
0
You could use a for statement to iterate code a number of times to fill boxes like:
您可以使用for语句多次迭代代码以填充以下框:
for x in range(*insert number of boxes*): *put other code here*
for x in range(* insert number of boxes *):*将其他代码放在这里*
This will fill it up if you just put the range in
如果您只是放入范围,这将填补它
#2
0
Not exactly sure what you have asked for, assuming you want to return color and color list without the color selected.
假设您想要在没有选择颜色的情况下返回颜色和颜色列表,则不确定您要求的内容。
cnames = ['green', 'blue'...]
to pick one color from list use random.choice
从列表中选择一种颜色使用random.choice
color = random.choice(cnames)
to get other colors init new list and pop by index
获取其他颜色初始化新列表并按索引弹出
other_colors = list(cnames)
other_colors.pop(colors.index(color))
return selected color and others
返回选定的颜色和其他
return color, other_colors
in graphwin
color, other_colors = randomize()
ucolor = '{0}{1}'.format(color[0].upper(), color[1:])
target = Text(Point(300,100),ucolor)
target.setOutline(color)
...
Boxes = other_colors #