代码之家  ›  专栏  ›  技术社区  ›  Dips

corona sdk从一个颜色数组中随机选择4种颜色

  •  0
  • Dips  · 技术社区  · 7 年前

    我如何从12种颜色的数组中随机选择4种颜色,然后存储所选的4种颜色,以便游戏可以告诉用户找到4种颜色中的1种作为记忆游戏的一部分。

    例如,如果数组列表中有R、G、B、Y、P、O颜色

    然后游戏选择R,Y,B,O并显示它们

    玩家需要找到的颜色不能是绿色例如,它只能是随机选择的4种颜色。

    我真的很难搞清楚,任何帮助都会很好

    1 回复  |  直到 7 年前
        1
  •  2
  •   brianolive    7 年前

    以下是许多可能的方法之一:

    allColors = {"black", "white", "red", "orange", "yellow", "green",
        "blue", "indigo", "violet", "gold", "silver", "bronze"}
    chosenColors = {}
    
    while #chosenColors < 4 do
        n = math.random(1, #allColors)
        table.insert(chosenColors, allColors[n])
        table.remove(allColors, n)
    end
    

    然后:

    for k, v in pairs(chosenColors) do
        print(k, v)
    end
    

    这将打印,例如:

    1 white
    2 indigo
    3 red
    4 gold
    
    推荐文章