目标效果:
随即上升不同颜色的气球,点击气球会消失
代码:
##############引用数据库与函数##############
from turtle import *
from random import randrange,choice
##############定义变量##############
balloons=[] #气球队列
color_option=["red","blue","green","purple","pink","yellow","orange","black"] #颜色队列
size=50 #气球大小
##############定义函数##############
def line(x,y,a,b,line_width=1,color_name="black"): #默认气球线宽度为1,颜色为黑
up()
goto(x,y)
down()
color(color_name)
width(line_width)
goto(a,b)
def distance(x,y,a,b):
return ((a-x)**2+(b-y)**2)**0.5 #根据勾股定理,判断鼠标点击位置和气球坐标的距离
def tap(x,y):
for i in range(len(balloons)):
if distance(x,y,balloons[i][0],balloons[i][1])<(size/2): #判断是否点击气球队列中的其中一