Turtle Python Module

Forge

I want to use the same key to cycle through three colors, right now I have the colors mapped to three different keys which makes things very inconvenient. I have tried before to make the same key have three different colors mapped to it thinking that the amount of times the key was pressed would change the color so say if I had a list of three colors and the button was already pushed ones and the list was set on the first color then pushing that same button again would change the color to the second color in that list.

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()
M4rtini
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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related