python泡泡摸奖系统_作者:李兴球

版权声明:所有文章皆为李兴球先生原创,转载请注明出处,否则保留权利,追究责任。 https://blog.csdn.net/avskya/article/details/82958869

泡泡摸奖系统封面

"""

某公司要举行一个小活动,为了活跃气氛,让参与的12个学生都来摸奖。
奖项设置为:一等奖1名,二等奖2名,三等奖3名,参与奖6名,希望每个学生都能中奖。

以下程序只是主程序的源代码,需要各个模块源代码,请联系李兴球

"""

#---------------------------------1、导入--------------------------
from winprize.screen import *
from winprize.pop import *
from winprize.girl import * 
from time import sleep


#---------------------------------2、音频--------------------------
pygame_normal = True
try:
    import pygame
    pygame.mixer.init()
except:
    pygame_normal = False

if pygame_normal:     
    bgmusic = "眉飞色舞.wav"
    pygame.mixer.music.load(bgmusic)
    pygame.mixer.music.play(-1,0)
    pop_sound = pygame.mixer.Sound("Pop.wav")
    
else:
    pop_sound = None
#-------------------------------3、动态舞台------------------------

imagelist = ["舞台" + str(i) + ".png" for i in range(7)]
s = Dynamic_screen(title='泡泡摸奖系统',piclist = imagelist,interval= 100)
screen = s.screen

#-------------------------------4、伴舞角色-------------------------
girl_images = ["qq女孩秀0.gif","qq女孩秀1.gif"]
[screen.addshape(image) for image in girl_images]   #注册到形状列表
dance_girl = Girl(girl_images)               #生成后,它会自己"跳舞"


#-------------------------------5、泡泡角色--------------------------
all_coordinates=[(-120,90),(-40,90),(40,90),(120,90),(-120,0),(-40,0),\
                 (40,0),(120,0),(-120,-90),(-40,-90),(40,-90),(120,-90)]
ac = all_coordinates                    #仅仅是为了缩短代码而定义的别名 
pop_image_list = ["泡泡造型1.gif","泡泡造型2.gif","泡泡造型3.gif","泡泡造型4.gif"]
[screen.addshape(image) for image in pop_image_list] #注册到形状列表,image为gif图片
pops = [ Pop(pop_image_list,x,y,pop_sound) for x,y in all_coordinates]
pop_amount = len(pops)
pops[0].prize = "一等奖"
pops[1].prize = "二等奖"
pops[2].prize = "二等奖"
pops[3].prize = "三等奖"
pops[4].prize = "三等奖"
pops[5].prize = "三等奖"
    
def pop_shuffle():
    """按空格键后对泡泡的位置进行随机调换,为了演示交换过程,没有使用shuffle命令"""
    old_delay = screen.delay()
    screen.delay(2)
    screen.onkeypress(None,"space")
    [pop.set_shake(False) for pop in pops]  #所有泡泡暂停摇摆
    sleep(0.5)
    for i in range(10):        
        index1 = randint(0,pop_amount-1)
        index2 = randint(0,pop_amount-1)
        while index1 == index2:index2 = randint(0,pop_amount-1)
        
        tmp = ac[index1]             #先保存index1索引号的数据(坐标)
        ac[index1] = ac[index2]      #设定index1索引号的数据为 index2指向的数据
        ac[index2] = tmp             #设定index2索引号的数据为原index1的数据
        
        pops[index1].initxy(ac[index1][0],ac[index1][1]) #重新设置为初始坐标
        pops[index2].initxy(ac[index2][0],ac[index2][1]) #重新设置为初始坐标
        
        pops[index1].goto(ac[index1])   #让索引为index1的泡泡到已交换位置的坐标        
        pops[index2].goto(ac[index2])   #让索引为index2的泡泡到已交换位置的坐标
 
        sleep(0.1)               
    
    [pop.set_shake(True) for pop in pops] #所有泡泡继续摇摆
    screen.delay(old_delay)
    screen.onkeypress(pop_shuffle,"space")

screen.onkeypress(pop_shuffle,"space")

Pop.wait_all_pop_explode()


#--------------------------------6、底部提示语------------------------
tip_turtle = Turtle(visible = False)
tip_turtle.penup()
tip_turtle.sety(-160)
def tip():
    """祝词"""
    r,g,b = randint(0,255),randint(0,255),randint(0,255)
    tip_turtle.pencolor(r,g,b)
    wd = "按空格键重排泡泡顺序,单击泡泡即是摸奖。\n 风火轮少儿编程祝小朋友们快乐!"
    tip_turtle.write(wd,align='center',font=("黑体",14,"normal"))
    tip_turtle.screen.ontimer(tip,1000)
tip()

#--------------------------------7、监听与事件循环------------------------
screen.listen()
screen.mainloop()

猜你喜欢

转载自blog.csdn.net/avskya/article/details/82958869
今日推荐