微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 Trinket.io/Python 中创建随机彩色文本?

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

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

解决方法

当您点击 turtle.onclick(function,mouse_button) 时,您可以使用 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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。