bg.png请到👇下面网址的最上面幸运大转盘-CSDN博客https://blog.csdn.net/alexwang211/article/details/135272113?spm=1001.2014.3001.5502图片在下面👇,可供参考。
今天我们来做大转盘,总共也不超过 130 行代码,这个是一个非常常用的一个作品,可以用来抽奖,编程也不难。 只是用 Python 的 turtle 库而已。 需要往开发环境里导入一张图片,需要将其名设为“bg.png”。
from turtle import *
from random import *
import time
sc = Screen()
sc.bgpic("bg.png")
sc.tracer(0)
t = Turtle()
t.hideturtle()
t.pensize(8)
#绘制箭头
def zhizhen():
t.forward(100)
t.left(135)
t.forward(20)
t.penup()
t.left(180)
t.pendown()
t.forward(20)
t.right(90)
t.forward(20)
t.penup()
t.goto(0,0)
t.left(135)
t.pendown()
#绘制大转盘
a = Turtle()
a.hideturtle()
def shanxing(angle,kk):#参数是扇形的角度、个数kk
a.setheading(0)
# colorlist = ["honeydew","aliceblue","lightyellow","linen","lavender","peachpuff","moccasin","lightcyan","lavenderblush","lemonchiffon"]
c1 = (240,147,147)
c2 = (250,247,203)
c3 = (125,202,241)
c4 = (183,249,241)
c5 = (231,193,218)
c6 = (204,172,177)
c7 = (243,174,204)
c8 = (130,200,194)
c9 = (162,218,247)
c10 = (252,224,189)
colorlist = [c1,c5,c4,c3,c2,c6,c7,c8,c9,c10]
for i in range(kk):
h = Turtle()#绘制数字
h.hideturtle()
h.penup()
h.right(angle/2 + i *angle)
h.forward(150)
h.write(i+1,font=["黑体",40])
h.goto(0,0)
a.color(colorlist[i])
#print(colorlist[i])
a.begin_fill()
a.forward(255)
a.left(90)
a.circle(255,angle)
a.left(90)
a.forward(255)
a.end_fill()
a.left(180)
#大转盘
def zhuanpan(m):
for i in range(m):
shanxing(360/m,m)
#判断最后的结果,返回值
def lucky(a):
ad = 0
t.setheading(0)
if a > 10:
a = 10
zhuanpan(a)
#指针旋转
n = randint(250, 350)
#print("旋转的次数:",n)
sum = 0
for i in range(int(n/2)):
t.clear()
t.right(15)#旋转的速度,总角度数用到了
zhizhen()
t.pendown()
sc.update()
ad += 15
for i in range(int(n/2)):
t.clear()
t.right(10)#旋转的速度,总角度数用到了
zhizhen()
t.pendown()
sc.update()
ad += 10
for i in range(int(n/2)):
t.clear()
t.right(5)#旋转的速度,总角度数用到了
zhizhen()
t.pendown()
sc.update()
ad += 5
for i in range(int(n/2)):
t.clear()
t.right(1)#旋转的速度,总角度数用到了
zhizhen()
t.pendown()
sc.update()
ad += 1
ad = ad % 360
afd = 360/a
for i in range(1, a+1):
if ad < afd * i:
turtle.done()
return i
if __name__ == '__main__':
# 修改lucky()中的数字,可以改变选项数量
# 写5,转盘变成5份,写6,转盘变为6份
print("终端")
num = lucky(6)
# 修改print()中的内容,改变选项吧~
# 注意if条件的数量要和lucky()中一样哦
if num == 1:
print("特等奖")
if num == 2:
print("一等奖")
if num == 3:
print("二等奖")
if num == 4:
print("三等奖")
if num == 5:
print("幸运奖")
if num == 6:
print("感谢参与")
运行效果: