如何在 Trinket.io/Python 中创建随机颜色的文本?

How to create randomly colored text in Trinket.io/Python?

我正在制作一款名为 Gravity code 的游戏,该游戏的 objective 是捕捉掉落的物品,这些物品会改变乌龟的颜色、速度、大小或形状。我需要一个随机颜色的标题。

# --- imports ---

import turtle
from random import * 

# --- variables ---

font_setup = ("Verdana", 25, "normal")
screen = turtle.Screen()

# --- main ---

title.speed("fastest")
title.hideturtle()
title.penup()
title.goto(-62, 60)
title.write("Gravity", font = font_setup)
title.setpos(-45, 30)
title.write("Code", font = font_setup)

# --- events ---

screen.mainloop()

点击button即可使用turtle.onclick(function, mouse_button)执行功能。

onclick 只需要没有 () 的函数名(所谓的“回调”)

on_button_click 必须获取两个值 - 鼠标位置。

import turtle

# --- functions ---

def on_button_click(x, y):
    print('button clicked:', x,y)
    button.hideturtle()
    
# --- main ---
    
screen = turtle.Screen()
button = turtle.Turtle()

button.speed("fastest")
#screen.addshape("icons8-button-100.png") #It's the second button.
#button.shape("icons8-button-100.png") #https://icons8.com/icons/set/button
button.left(90) # The second image is the button I'm using.
button.penup() #It can be resized afer you click download to...
button.goto(0, -120) # 70 x 70 pixels.

button.onclick(on_button_click, 1)

screen.mainloop()