简体   繁体   中英

Turtle colour doesn't change with variable and list

So this is my code right now and the problem should be somwehere between Line 18 and 37. What I want to do is that when the user clicks on the top left turtle, it changes to the next colour. The turtle in the top left is only optical and has no other function than telling the user where to click (at least that's the idea). The problem is that it doesn't do that and I don't really get why

import turtle
from turtle import *

# def on_click_handler(x,y):
# print("Clicked: " , [x,y])
# t1.goto(x,y)

sc = Screen()
sc.setup(400, 400, 10, 10)
sc.setworldcoordinates(-300, -300, 300, 300)
# sc.onscreenclick(on_click_handler)

t1 = Turtle("turtle")
t1.shape("circle")
t1.shapesize(0.25, 0.25)
t1.speed(-1)

#changing turtle colour
tcolour = Turtle("turtle")
tcolour.penup()
tcolour.shape("square")
tcolour.shapesize(1, 1.5)
tcolour.setpos(-275, 285)
colour_list = ["#000000", "#0101DF", "#04B404", "#FF0000", "#FF8000", "B40486"] #black, blue, green, red, orange, purple
n = 0

def colourchange(x, y):
    if (x < -275 and x > -300 and y > 284 and y < 300):
        global n
        n += 1


turtle.onscreenclick(colourchange, 1)
turtle.listen()

t1.color(colour_list[0+n])

def dragging(x, y):
    t1.ondrag(None)
    t1.setheading(t1.towards(x, y))
    t1.goto(x, y)
    t1.ondrag(dragging)


def clickright(x, y):
    t1.clear()


def main():
    turtle.listen()

    t1.ondrag(dragging)
    turtle.onscreenclick(clickright, 3)

    sc.mainloop()


main()

And also I don't think I'm allowed to import tkinter, atleast I think that's what our prof said

Whenever you import the same module two different ways, you're already in trouble:

import turtle
from turtle import *

You're having problems distinguishing between the turtle and the screen -- using the above facilitates those problems. I recommend instead that you only use turtle's object-oriented interface and turn off the functional interface:

from turtle import Screen, Turtle

You're trying to use a turtle as a button, which is fine, but you put the click handler on the screen and had the handler test if you clicked the turtle. Instead, put the click handler on the turtle so you needn't test. (Again, part of the problem of not being clear about what the turtle does and what the screen does.)

Though you call it twice, listen() doesn't apply here as were not dealing with keyboard input (yet).

Finally, your color change handler updated the color index but never actually changed any colors. Below is my rework and simplification of your code to address the above issues:

from turtle import Screen, Turtle

COLOUR_LIST = ['black', 'blue', 'green', 'red', 'orange', 'purple']

colour_index = 0

def colour_change(x, y):
    global colour_index

    colour_index = (colour_index + 1) % len(COLOUR_LIST)
    t1.color(COLOUR_LIST[colour_index])

def dragging(x, y):
    t1.ondrag(None)
    t1.setheading(t1.towards(x, y))
    t1.goto(x, y)
    t1.ondrag(dragging)

def click_right(x, y):
    t1.clear()

t1 = Turtle('circle')
t1.shapesize(0.5)
t1.speed('fastest')
t1.color(COLOUR_LIST[colour_index])
t1.ondrag(dragging)

# changing colour turtle
tcolour = Turtle('square')
tcolour.penup()
tcolour.shapesize(1, 1.5)
tcolour.setpos(-275, 285)
tcolour.onclick(colour_change, 1)

screen = Screen()
screen.onclick(click_right, btn=3)
screen.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM