乌龟Python模块

时间:2014-03-17 13:41:25

标签: python

我想使用相同的键来循环显示三种颜色,现在我将颜色映射到三个不同的键,这使得事情非常不方便。我之前尝试过使相同的键有三种不同的颜色映射到它,认为按下按键的次数会改变颜色所以说如果我有一个三种颜色的列表并且按钮已被按下并且列表在第一种颜色上设置然后再次按下相同的按钮会将颜色更改为该列表中的第二种颜色。

from turtle import *
print("""Welcome to my Loopy mod,
there are some basic controls to your turtle and they are:
up arrow = forward
down arrow = back
left arrow = left
right arrow = right
space = reset/clear
a = penup
d = pendown
r = color red
g = color green
b = color blue
press the escape button at anytime to close the program:)""")
setup(800, 600)
Screen()
title("Move My Turtle!")
move = Turtle()
move.shape("turtle")
# All keys are defined here
def k1():
    move.forward(45)
def k2():
    move.left(45)
def k3():
    move.right(45)
def k4():
    move.back(45)
def k5():
    move.reset()
    move.fillcolor("black")
    move.pencolor("black")
def k6():
    sys.exit()
def k7():
    move.fillcolor("red")
    move.pencolor("red")
def k8():
    move.fillcolor("green")
    move.pencolor("green")
def k9():
    move.fillcolor("blue")
    move.pencolor("blue")
def k10():
    move.penup()
def k11():
    move.pendown()

# These are the controls
onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")
onkey(k5, "space")
onkey(k6, "Escape")
onkey(k10, "a")
onkey(k11, "d")

# These are my color keys
onkey(k7, "r")
onkey(k8, "g")
onkey(k9, "b")

listen()
mainloop()

1 个答案:

答案 0 :(得分:0)

def colorCycle():
    from itertools import cycle
    colors = ['black', 'red', 'green', 'blue']
    for color in cycle(colors):
        yield color
it = iter(colorCycle())
nextColor = it.next

def k7():
    c = nextColor()
    move.fillcolor(c)
    move.pencolor(c)