本帖最后由 hnyzcj 于 2022-4-1 20:48 编辑
【创意起源】
班级年级在一些节假日活动中存在一些抽奖、抽号的环节,为此我根据实际活动的需要将抽奖、抽号合并功能合并,用python制作了一个抽奖抽号的程序。
【使用的库】 本程序中使用了pygame、random、os 和 time库,其中【Pygame】是被设计用来写游戏的python模块集合,使用python可以导入pygame来开发具有全部特性的游戏和多媒体软件,本程序中我们使用了Pygame图形化界面。【random】是用于产生并运用随机数的标准库。Python中的主要使用是让其掌握在程序中运用随机数的能力。本程序中生产抽奖抽号的随机数字序列。【os】是与操作系统相关的标准库。如:文件,目录,执行系统命令等。【time】运行访问多种类型的时钟,这些时钟用于不同的场景。
【编程思路】 本程序主要由主界面、抽学号界面、抽奖界面构成,具体功能入下图4所示。
【配置编程环境】 打开Mind+软件,切换到Mind+软件的 Python 模式,Python 模式如下图5 切换到 Python 模式后,我们点击右侧文件目录,在“电脑中的文件” 中添加文件。点击位置如下图6数字序列①“添加本地文件夹”,我们在自己设定的路径下新建“原代码”文件夹用来存放 Python 程序和编程所需的素材。选中该文件夹后点击确定,建立一个python程序的编程环境。 之后,我们点击新建文件,即可在当前文件夹下创建 Python 程序 。我们这里创建了三个python程序,其名称为RunME_V1.1.py(主程序界面),choujiang_v20.1.py(抽奖程序界面),chouxuehao.py(抽学号程序界面)如图7所示 抽学号抽奖程序需要用的第三方库分别是【pygame】,安装方法如下: 在 Mind+软件的右上角点击库管理按钮,之后会看到的界面,在推荐库一栏提供了大部分常用第三方库,可以一键安装,我们点击“游戏”分类库下面的 【pygame】库进行安装,安装完毕会看到“已安装” 的提示字样,表明安装完成,如下图 8
【添加素材】 本程序中使用到了图片、声音等素材文件,要将其拷贝到“原文件”文件夹下。如下图9所示。其中FZYTK.TTF为字库文件,TT.wav为程序中音频文件,其余图片为背景图片及抽奖等级图片,name.txt为存储学生姓名的文档。
【制作主界面】 打开RunME_V1.1.py输入下列程序,注意chouxuehao():和choujiang():函数中我们写的是调用EXE文件,后续需要将相关文件由py格式打包成exe格式。
- <font size="4"><font size="4">import pygame
- import random
- import time
- import os
- pygame.init()
- from pygame.locals import *
- sc = pygame.display.set_mode((400,600))
- bg = pygame.image.load("bg.png")
- font = pygame.font.Font("FZYTK.ttf",40)
- font1 = pygame.font.Font("FZYTK.ttf",30)
- font_color=(0,0,0)
- sc.blit(bg,(0,0))
- pygame.display.update()
- text_1 = font1.render("选择您需要的功能:",True,font_color)
- sc.blit(text_1,(65,20))
- text_xuehao = font.render("抽序号",True,font_color)
- sc.blit(text_xuehao,(140,160))
- text_xuehao = font.render("抽奖",True,font_color)
- sc.blit(text_xuehao,(140,400))
- pygame.display.update()
- def chouxuehao():
- os.startfile("chouxuehao.exe")
- def choujiang():
- os.startfile("choujiang_v20.1.exe")
- while True:
- #sc.blit(num,(100,20))
- x,y = pygame.mouse.get_pos()
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if 130 < x < 270 and 150 < y <210:
- chouxuehao()
- if 130 < x < 230 and 390 < y <450:
- choujiang()</font></font>
复制代码
【制作抽学号界面】
打开chouxuehao.py输入下列程序,该部分程序运行顺序为:打开待抽学号学生信息文件,输入学生姓名,(如果不需要学生姓名信息忽略此步)如图10所示,进入设置抽取人数界面如图12所示,输入将要抽取的学生人数后点击界面进入抽学号界面,单击任意位置进行学号抽取如图13所示,若要对背景进行更换可在输入抽取人数位置,点击“换装”进行背景更换。
- <font size="4">import pygame
- import random
- import time
- import os
- pygame.init()
- from pygame.locals import *
- la = []
- sc = pygame.display.set_mode((400,600))
- bg = pygame.image.load("bg.png")
- pygame.display.set_caption("抽学号")
- font = pygame.font.Font("FZYTK.ttf",60)
- font1 = pygame.font.Font("FZYTK.ttf",30)
- font2 = pygame.font.Font("FZYTK.ttf",20)
- ifend = False
- ifnamed = False
- font_color = (0,0,0)
- sc.blit(bg,(0,0))
- pygame.display.update()
- iss = True
- a2 = ""
- n_num = 0
- name = font2.render("单击此处将打开记事本", True, font_color)
- name1 = font2.render("编辑序号对应的姓名", True, font_color)
- name2 = font2.render("每个姓名占一行", True, font_color)
- ifnonext = font2.render("如果无需姓名则立即下一步", True, font_color)
- ne_st = font2.render("单击回车下一步", True, font_color)
- clickto = font2.render("单击任意位置抽序号", True, font_color)
- namelist = []
- ifendname = False
- #encoding = "utf-8"
- while True: #读txt
- sc.blit(bg,(0,0))
- sc.blit(name,(120,100))
- sc.blit(name1,(120,130))
- sc.blit(name2,(120,160))
- sc.blit(ifnonext,(120,185))
- sc.blit(ne_st,(120,300))
- for event in pygame.event.get():
- x,y = pygame.mouse.get_pos()
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if 120<x<320 and 100<y<120:
- os.system("name.txt")
- ifnamed = True
- if event.type == KEYDOWN:
- if event.key == K_RETURN:
- if ifnamed == True:
- f = open("name.txt","r",encoding = "utf-8")
- for line in f:
- namelist.append(line)
- ifendname = True
- if ifnamed == False:
- ifendname = True
- pygame.display.update()
- if ifendname == True:
- #print(1)
- break
- sc.blit(bg,(0,0))
- while True:
- num = font1.render("请输入抽取人数:", True, font_color)
- text_changing = font1.render("换装",True,font_color)
- sc.blit(num,(100,20))
- sc.blit(text_changing,(0,0))
- x,y = pygame.mouse.get_pos()
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if 0<x<65 and 0<y<30:
- #sc1 = pygame.display.set_mode((400,600))
- text_1 = font1.render("选择您想使用的背景",True,font_color)
- text_nd = font1.render("红色",True,font_color)
- text_mid_a = font1.render("绿色",True,font_color)
- text_chny = font1.render("蓝色",True,font_color)
- text_child = font1.render("黄色",True,font_color)
- text_white = font1.render("无",True,font_color)
- text_few = font1.render("""敬请期待,单击此处返回""",True,font_color)
- while True:
- sc.blit(bg,(0,0))
- sc.blit(text_1,(65,5))
- sc.blit(text_nd,(65,40))
- sc.blit(text_mid_a,(65,75))
- sc.blit(text_chny,(65,110))
- sc.blit(text_child,(65,145))
- sc.blit(text_white,(65,180))
- sc.blit(text_few,(65,350))
- if ifend == True:
- sc.blit(bg,(0,0))
- pygame.display.update()
- break
- for event in pygame.event.get():
- x,y = pygame.mouse.get_pos()
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if ifend == False:
- if 65<x<395 and 350<y<380:
- ifend = True
- elif 65<x<155 and 40<y<70:
- bg = pygame.image.load("r.png")
- font_color = (0,0,0)
- elif 65<x<155 and 75<y<105:
- bg = pygame.image.load("g.png")
- font_color = (255,255,255)
- elif 65<x<125 and 110<y<140:
- bg = pygame.image.load("b.png")
- font_color = (255,0,0)
- elif 65<x<155 and 145<y<175:
- bg = pygame.image.load("y.png")
- font_color = (0,0,0)
- elif 65<x<100 and 180<y<210:
- bg = pygame.image.load("bg.png")
- font_color = (0,0,0)
-
- pygame.display.update()
- if event.type == KEYDOWN:
- if event.key == K_RETURN:
- a2 = a2
- sc.blit(bg,(0,0))
- pygame.display.update()
- #n_num = str(n_num) + a2
- n_num = 1
- else:
- a2 = a2+chr(event.key)
- text_1 = font.render(a2, True, font_color)
- sc.blit(text_1, (100, 80))
- if n_num == 1:
- break
- pygame.display.update()
- #start()
- sc.blit(bg,(0,0))
- pygame.display.update()
- for i in range(int(a2)+1):
- la.append(i)
- if ifendname == True and namelist == []:
- for i in range(int(a2)+1):
- namelist.append("")
- del la[0]
- num = font1.render("请输入序号数:", True, font_color)
- text_changing = font1.render("换装",True,font_color)
- while True:
- sc.blit(bg,(0,0))
- sc.blit(clickto,(110,200))
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if len(la) == 0:
- end = font.render("抽完了", False, font_color)
- sc.blit(end,(120,285))
- pygame.display.update()
- time.sleep(2)
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- xuehaoindex = random.randint(0,len(la)-1)
- xuehao = la[xuehaoindex]
- chosename = namelist[xuehaoindex]
- text_1 = font.render(str(xuehao)+"号", False, font_color)
- nameprinted = font.render(chosename, False, font_color)
- sc.blit(text_1, (140, 280))
- sc.blit(nameprinted,(120,345))
- pygame.display.update()
- time.sleep(1.5)
- sc.blit(bg,(0,0))
- pygame.display.update()
- del la[xuehaoindex]
- del namelist[xuehaoindex]
- pygame.display.update()</font>
复制代码
【制作抽奖界面】 打开choujiang_v20.1.py输入下列程序,该部分程序运行顺序为:输入各等级学生数量,(我们这里初始设置三个等级数量为5,输入值不应该超过5)如图14所示,进入抽奖界面,单击任意位置进行抽奖如图15所示,若要对背景进行更换可在输入等级人数位置,点击“换装”进行背景更换。 - <font size="4">import pygame
- import random
- import time
- pygame.init()
- from pygame.locals import *
- a1 = ""
- a2 = ""
- a3 = ""
- t1 = 0
- t2 = 0
- t3 = 0
- e1 = 0
- e2 = 0
- e3 = 0
- l1 = 0
- l2 = 0
- l3 = 0
- la = []
- n_num = 0
- iffirst3 = True
- iffirst2 = True
- iffirst1 = True
- sc = pygame.display.set_mode((400,600))
- #bg = pygame.image.load("Untitled.png")
- bg = pygame.image.load("bg.png")
- pygame.display.set_caption("抽奖")
- font = pygame.font.Font("FZYTK.ttf",30)
- font1 = pygame.font.Font("FZYTK.ttf",15)
- font_color=(50,155,20)
- #sc.blit(bg,(0,0))
- sc.fill((255,255,255))
- pygame.display.update()
- iss = True
- ifend = False
- text_changing = font1.render("换装",True,font_color)
- def sound():
- pygame.mixer.music.load("TT.wav")
- pygame.mixer.music.play(-1,0.0)
- sound()
- """================================num input================================"""
- while True:
- sc.blit(text_changing,(0,0))
- x,y = pygame.mouse.get_pos()
- if iss == False:
- break
- else:
-
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if 0<x<35 and 0<y<20:
- #sc1 = pygame.display.set_mode((400,600))
- text_1 = font.render("选择您想使用的背景",True,font_color)
- text_nd = font.render("红色",True,font_color)
- text_mid_a = font.render("绿色",True,font_color)
- text_chny = font.render("蓝色",True,font_color)
- text_child = font.render("黄色",True,font_color)
- text_white = font.render("无",True,font_color)
- text_few = font.render("""敬请期待,单击此处返回""",True,font_color)
- while True:
- sc.blit(bg,(0,0))
- sc.blit(text_1,(65,5))
- sc.blit(text_nd,(65,40))
- sc.blit(text_mid_a,(65,75))
- sc.blit(text_chny,(65,110))
- sc.blit(text_child,(65,145))
- sc.blit(text_white,(65,180))
- sc.blit(text_few,(65,350))
- if ifend == True:
- sc.blit(bg,(0,0))
- pygame.display.update()
- break
- for event in pygame.event.get():
- x,y = pygame.mouse.get_pos()
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- if ifend == False:
- if 65<x<395 and 350<y<380:
- ifend = True
- elif 65<x<155 and 40<y<70:
- bg = pygame.image.load("r.png")
- font_color = (0,0,0)
- elif 65<x<155 and 75<y<105:
- bg = pygame.image.load("g.png")
- font_color = (255,255,255)
- elif 65<x<125 and 110<y<140:
- bg = pygame.image.load("b.png")
- font_color = (255,0,0)
- elif 65<x<155 and 145<y<175:
- bg = pygame.image.load("y.png")
- font_color = (0,0,0)
- elif 65<x<100 and 180<y<210:
- bg = pygame.image.load("bg.png")
- font_color = (0,0,0)
- pygame.display.update()#changing(bg,False)
-
- if n_num == 0:
- start = font.render("请输入各奖项人数", True, font_color)
- text = font.render("三等奖:", True, font_color)
- sc.blit(start,(100,10))
- sc.blit(text,(100,50))
- if event.type == KEYDOWN:
- if event.key == K_RETURN:
- a3 = int(a3)
- n_num = n_num + 1
- else:
- a3 = a3+chr(event.key)
- text_1 = font.render(a3, True, font_color)
- sc.blit(text_1, (100, 80))
-
- elif n_num == 1:
- text = font.render("二等奖:",True,font_color)
- sc.blit(text,(100,155))
- if event.type == KEYDOWN:
- if event.key == K_RETURN:
- n_num = n_num + 1
- a2 = int(a2)
- else:
- a2 = a2+chr(event.key)
- text_1 = font.render(a2, True, font_color)
- sc.blit(text_1, (100, 185))
-
- elif n_num == 2:
- text = font.render("一等奖:",True,font_color)
- sc.blit(text,(100,255))
- if event.type == KEYDOWN:
- if event.key == K_RETURN:
- a1 = int(a1)
- iss = False
-
- else:
- a1 = a1+chr(event.key)
- text_1 = font.render(a1, True, font_color)
- sc.blit(text_1, (100, 285))
- pygame.display.update()
-
- """==================================list add============================"""
- for i in range(5):
- la.append("一等奖")
- for i in range(5):
- la.append("二等奖")
- for i in range(5):
- la.append("三等奖")
- #print(la)
- """=================================main start============================"""
- def end():
- sc.blit(bg,(0,0))
- text = font.render("结束",True,font_color)
- sc.blit(text,(170,285))
- pygame.display.update()
- time.sleep(1)
- pygame.display.quit()
- exit()
- def award_1():
- ta = "一等奖"
- wow = pygame.image.load("wow.png")
- award = font.render(ta,True,font_color)
- sc.blit(award,(155,325))
- sc.blit(wow,(130,10))
- #t2 = t2+1
- #l1 = l1+1
-
- def award_2():
- ta = "二等奖"
- cool = pygame.image.load("02.png")
- award = font.render(ta,True,font_color)
- sc.blit(award,(155,325))
- sc.blit(cool,(130,40))
- #t2 = t2+1
- #l2 = l2+1
-
- def award_3():
- ta = "三等奖"
- cool = pygame.image.load("03.png")
- award = font.render(ta,True,font_color)
- sc.blit(award,(155,325))
- sc.blit(cool,(130,40))
- #t3 = t3+1
- #l3 = l3+1
- while True:
-
- if la == []:
- sc.blit(bg,(0,0))
- text = font.render("结束",True,font_color)
- sc.blit(text,(170,285))
- pygame.display.update()
- time.sleep(1)
- pygame.display.quit()
- exit()
-
- if t1+t2+t3 == a1+a2+a3:
- pygame.display.quit()
- exit()
- #aw = random.randint(0,len(la)-1)
- #ta = la[aw]
- #pygame.init()
- #bg = pygame.image.load("Untitled.jpg")
- sc.blit(bg,(0,0))
- #pygame.display.update()
- text = font.render("点击抽奖",True,font_color)
- sc.blit(text,(140,285))
-
- #pygame.display.update()
- pygame.display.update()
- for event in pygame.event.get():
- x,y = pygame.mouse.get_pos()
- if event.type == QUIT:
- pygame.display.quit()
- exit()
- if event.type == MOUSEBUTTONDOWN:
- aw = random.randint(0,len(la)-1)
- ta = la[aw]
- if ta == "三等奖":
- if e3 == 0:
- if l3 == 3: #重复判断
- if e2 == 0:
- award_2()
- l2 = l2+1
- elif e1 == 0:
- award_1()
- l1 = l1+1
- elif e3 == 0:
- award_3()
- l3 = l3+1
- elif l3 == 0:
- if iffirst3 == True :
- award_3()
- l3 = l3+1
- iffirst3 = False
- elif iffirst3 == False:
- if e2 == 0:
- award_2()
- l2 = l2+1
- elif e1 == 0:
- award_1()
- l1 = l1+1
- else:
- award_3()
- l2 = l2+1
- if e3 != 0 :
- if e2 == 1 and e1 == 1:
- end()
- if e2 == 0:
- award_2()
- l2 = l2+1
- elif e1 == 0:
- award_1()
- l1 = l1+1
- elif ta == "二等奖":
- if e2 == 0:
- if l2 ==2: #重复2
- if e3 == 0:
- award_3()
- l3 = l3+1
- elif e1 == 0:
- award_1()
- l1 = l1+1
- elif e2 == 0:
- award_2()
- l2 = l2+1
- elif l2 == 0:
- if iffirst2 == True :
- award_2()
- l2 = l2+1
- iffirst2 = False
- elif iffirst2 == False:
- if e1 == 0:
- award_1()
- l1 = l1+1
- elif e3 == 0:
- award_3()
- l3 = l3+1
- else:
- award_2()
- l2 = l2+1
- if e2 != 0 :
- if e3 == 1 and e1 == 1:
- end()
- if e1 == 0:
- award_1()
- l1 = l1+1
- elif e3 == 0:
- award_3()
- l3 = l3+1
- elif ta == "一等奖":
- if e1 == 0:
- if l1 == 1:
- if e2 == 0:
- award_2()
- l2 = l2+1
- elif e3 == 0:
- award_3()
- l3 = l3+1
- elif e1 == 0:
- award_1()
- l1 = l1+1
- elif l1 == 0:
- if iffirst1 == True :
- award_1()
- l1 = l1+1
- iffirst1 = False
- elif iffirst1 == False:
- if e2 == 0:
- award_2()
- l2 = l2+1
- elif e3 == 0:
- award_3()
- l3 = l3+1
- else:
- award_1()
- l1 = l1+1
- if e1 != 0 :
- if e2 == 1 and e3 == 1:
- end()
- if e2 == 0:
- award_2()
- l2 = l2+1
- elif e3 == 0:
- award_3()
- l3 = l3+1
- pygame.display.update()
- time.sleep(1)
- if ta == "一等奖":
- t1 = t1+1
- print("picked1")
- if ta == "二等奖":
- t2 = t2+1
- print("picked2")
- if ta == "三等奖":
- t3 = t3+1
- print("picked3")
-
- if t1 == a1 and e1 == 0:
- la.remove("一等奖")
- #emp1 = font.render("一等奖抽完啦!",True,(0,0,0))
- #sc.blit(emp1,(95,5))
- #la.append(0)
- e1 = 1
- if t2 == a2 and e2 == 0:
- la.remove("二等奖")
- la.remove("二等奖")
- #emp2 = font.render("二等奖抽完啦!",True,(0,0,0))
- #sc.blit(emp2,(95,38))
- e2 = 1
- if t3 == a3 and e3 == 0:
- la.remove("三等奖")
- la.remove("三等奖")
- la.remove("三等奖")
- la.remove("三等奖")
- la.remove("三等奖")
- #emp3 = font.render("三等奖抽完啦!",True,(0,0,0))
- #sc.blit(emp1,(95,71))
- e3 = 1
- pygame.display.update()
- if la == []:
- sc.blit(bg,(0,0))
- text = font.render("结束",True,font_color)
- sc.blit(text,(170,285))
- pygame.display.update()
- time.sleep(1)
- pygame.display.quit()
- exit()
- if t1+t2+t3 == a1+a2+a3:
- pygame.display.quit()
- exit()</font>
复制代码
【py转换exe】 安装pyinstaller,打开cmd窗口,输入pip install pyinstaller,命令行输出successfully表示成功。运行cmd, 打开py文件所在的目录。如C:\code输入pyinstaller -F **.py即可。此时,我们可以看到,在code原始脚本的同级目录下,生成了build和dist文件夹,其中dist文件夹下存放我们想要的exe程序。
|