今天用turtle制作一个抽签代码
依然用到的是turtle库和random库,在这里我就不再重复介绍turtle库和random库了,代码中还用到onkey按键点击事件,onkey可以按下设置的相应键位,从而继续运行程序。
import turtle
import random
screen = turtle.Screen()
screen.title("谁是下一个?")
screen.bgcolor("sky blue")
screen_width,screen_height = 720,720
screen.setup(screen_width,screen_height)
screen.setworldcoordinates(0,0,screen_width,screen_height)
pen = turtle.Pen()
pen.hideturtle()
def new_goto(x,y):
pen.penup()
pen.goto(x,y)
pen.pendown()
people = ["阿短","小可","绿豆","小光","小加"]
random.shuffle(people)
#写标题
pen.color("white")
new_goto(screen_width / 2, screen_height - 100)
pen.write('谁是下一个??',align="center",font=("FZY3JW.TTF",50,"bold"))
#显示帮助信息
pen.color("yellow")
new_goto(10, screen_height - 150)
pen.write('提示:按下空格键抽取下一位出场选手。', font=("FZY3JW.TTF", 20, ))
#参与抽签的人
pen.color("white")
new_goto(10, screen_height - 200)
pen.write('参与比赛的人有:', font=("FZY3JW.TTF", 30,"bold"))
new_goto(10,screen_height - 260)
pen.write(people,font=("FZY3JW", 24,"bold"))
random.shuffle(people)
#抽签结果
pen.color("black")
new_goto(10,screen_width - 350)
pen.write('出场顺序为:', font=("FZY3JW.TTF", 30, "bold"))
#M名字间隔
tot = len(people)
per = screen_width / (tot+1)
def rand_name():
if people:
name = people.pop()
n = tot - len(people)
space = n * per
new_goto(space,screen_height - 400)
pen.color("black")
pen.dot(90)
new_goto(space,screen_height - 420)
pen.color("white")
pen.write(name,align="center",font=("FZY3JW.TTF",30))
if len(people) == 0:
pen.pencolor("green")
new_goto(screen_width / 2,screen_height - 600)
pen.write("所有选手以出场!", align="center", font=("FZY3JW.TTF", 50,"bold"))
screen.onkey(rand_name, "space") #按下空格键可以继续运行程序
screen.listen()
turtle.done()
运行效果如下: