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

我怎么知道在我点击的地方获得了星星?

如何解决我怎么知道在我点击的地方获得了星星?

我想在点击屏幕时绘制星星。就像我有点明白我需要怎么做,但是我尝试过的一次没有奏效,它给出了一个错误

AttributeError: 模块 'turtle' 没有属性 'onScreenClick'

有时它说我需要在其中添加“乐趣”?

#2.7 Tähtikirkas yö paranneltu versio.

#Tähtikirkas yö
import turtle as t
from random import randint,random

def draw_star(points,size,col,x,y):
    t.speed(80)
    t.penup()
    t.goto(x,y)
    t.pendown
    angle = 180 - (180 / points)
    t.color(col)
    t.begin_fill()
    for i in range(points):
        t.forward(size)
        t.right(angle)
    t.end_fill()

#pääohjelma
t.Screen().bgcolor('light yellow')

while True:
    ranPts = randint(5,5) * 2 + 1
    ranSize = randint(20,50)
    ranCol = (random(),random (),random())
    ranX = randint(-350,300)
    ranY = randint(-250,250)
    draw_star(ranPts,ranSize,ranCol,ranX,ranY)

解决方法

screen 的方法是onclick(),或者全局函数onscreenclick()。方法/函数的参数是用户单击窗口时要调用的函数之一的名称。您的函数应采用 xy 参数。下面是对您的代码进行的修改,以允许用户通过点击而不是随意点击来照亮天空:

from turtle import Screen,Turtle
from random import randint,random

def draw_star(points,size,color,x,y):
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()

    angle = 180 - (180 / points)

    turtle.color(color)
    turtle.begin_fill()

    for _ in range(points):
        turtle.forward(size)
        turtle.right(angle)

    turtle.end_fill()

def click_sky(x,y):
    ranPts = randint(2,5) * 2 + 1
    ranSize = randint(20,50)
    ranColor = (random(),random(),random())

    draw_star(ranPts,ranSize,ranColor,y)

turtle = Turtle()
turtle.speed('fastest')
turtle.hideturtle()

screen = Screen()
screen.bgcolor('light yellow')
screen.onclick(click_sky)
screen.mainloop()

enter image description here

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