python实现公司年会抽奖程序
程序员文章站
2023-11-16 12:51:58
本文实例为大家分享了python实现年会抽奖程序的具体代码,供大家参考,具体内容如下
发一下自己写的公司抽奖程序。
需求:公司年会要一个抽奖程序,转盘上的每一个人名是随...
本文实例为大家分享了python实现年会抽奖程序的具体代码,供大家参考,具体内容如下
发一下自己写的公司抽奖程序。
需求:公司年会要一个抽奖程序,转盘上的每一个人名是随机中奖的,中奖后的人不可以再次中奖,按住抽奖,就会一直在转,放开后,要再转一两圈才停。
刚好自己在学python cocos2d,就用这个刚学的东东,直接上源码
# coding:utf-8 # import sys # import os # sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..')) # 解决程序中要显示中文问题 reload(sys) sys.setdefaultencoding('utf8') from pyglet import image, font from pyglet.gl import * from pyglet.window import key from cocos.actions import * from cocos.director import director from cocos.layer import layer from cocos.layer import colorlayer from cocos.scene import scene from cocos.sprite import sprite from cocos.text import * from cocos.menu import * import random from cocos.audio.effect import effect consts_window = { "width": 680, "height": 700, "vsync": true, "resizable": true, 'audio_backend':'sdl' } def get_sprite_test( index ): d = tests[index] return scene( d( index ) ) class spritelayer( layer ): is_event_handler = true #: enable pyglet's events def __init__( self, index=1 ): super(spritelayer, self ).__init__() self.index = index self.top_text = "广州德瀚信息信息科技有限公司-年会抽奖" self.image = pyglet.resource.image('r1.png',0.01) # self.image = image.animationframe(image.load('r1.png'),0.1) self.image.anchor_x = self.image.width / 2 self.image.anchor_y = self.image.height / 2 self.rimage = pyglet.resource.image('r2.png',0.01) # self.rimage = image.animationframe(image.load('r2.png'),0.1) self.rimage.anchor_x = self.image.width / 2 self.rimage.anchor_y = self.image.height / 2 self.bgimage = pyglet.resource.image('bg1.png') self.bgimage.anchor_x = self.image.width / 2 self.bgimage.anchor_y = self.image.height / 2 self.pressbgimage = pyglet.resource.image('bg2.png') self.pressbgimage.anchor_x = self.image.width / 2 self.pressbgimage.anchor_y = self.image.height / 2 self.prizeimage = pyglet.resource.image('name.png') self.prizeimage.anchor_x = self.image.width / 2 self.prizeimage.anchor_y = self.image.height / 2 def on_key_release( self, keys, mod ): # left: go to previous scene # rigth: go to next scene # enter: restart scene if keys == key.left: self.index -= 1 if self.index < 1: self.index = len( tests ) elif keys == key.right: self.index += 1 if self.index > len( tests ): self.index = 1 if keys in (key.left, key.right): director.replace( get_sprite_test( self.index ) ) return true class prizemenu(menu): def __init__( self ): super( prizemenu, self ).__init__() self.menu_valign = bottom self.menu_halign = right self.font_item['color'] = (0,0,0,255) self.font_item_selected['color'] = (32,16,32,255) # print dir(self) # then add the items items = [ ( menuitem('一等奖', self.prize_go ) ), ( menuitem('二等奖', self.prize_go ) ), ( menuitem('三等奖', self.prize_go ) ), ( menuitem('参与奖', self.prize_go ) ), ] # self.create_menu( items, selected_effect=zoom_in(), # unselected_effect=zoom_out()) self.create_menu( items, shake(), shake_back()) def on_quit( self ): pyglet.app.exit() def prize_go( self ): s = self.parent if s.is_begin: s.top_notice.element.text="" else: # s.stop_num = 1 s.go_prize() # def main(): # pyglet.font.add_directory('.') # director.init( resizable=true) # director.run( scene( prizemenu() ) ) # if __name__ == '__main__': # main() class startprize( spritelayer ): def __init__( self,index ): super( startprize, self ).__init__(index) self.current_num = 0 #当前位置 self.is_begin = false #是否已经开始 self.prize_cycle = 0 #转动圈数 self.prize_speed = 0.05 #初始速度 self.prize_speed_slow = 0.3 #慢速度 self.stop_num = 0 #停止的位置 self.alread_get_prize = [] # 已经得奖的人 self.start_slow = false self.can_stop = false self.press_go = false self.sprite = sprite( self.image ) # self.sprite = sprite( image.animation([image.animationframe(image.load('r1.png'),0.001)] )) self.alread_prize_sprite = sprite( self.rimage ) self.bgsprite = sprite( self.bgimage ) self.pressbgsprite = sprite( self.pressbgimage ) self.prizesprite = sprite( self.prizeimage ) self.top_label = label( self.top_text ) # 注意是要有个element self.top_label.element.x = -250 self.top_label.element.y = 350 self.top_label.element.color = (0,0,0,255) self.top_label.element.font_size = 20 self.top_notice = label( "点击中间开始抽奖" ) self.top_notice.element.x = 120 self.top_notice.element.y = 300 self.top_notice.element.color = (255,0,0,255) self.top_notice.element.font_size = 20 self.pressbgsprite.do(hide()) self.alread_prize_sprite.do(hide()) # self.sprite.do(hide()) def on_enter( self ): super(startprize,self).on_enter() bgcolor = colorlayer(255,255,255,255, consts_window['width'], consts_window['height']) bgcolor.position = (-320,-320) # 背景颜色 self.add( bgcolor ) # 标题 self.add( self.top_label ) self.add( self.top_notice ) # 转动的背景图 self.add( self.sprite ,z=3) self.add( self.alread_prize_sprite ,z=3) # 人名图 self.add( self.prizesprite ,z=4) # 背景图 self.add( self.bgsprite,z=1 ) self.add( self.pressbgsprite,z=1 ) self.position = 320,320 # menu = prizemenu() # menu.position = (-320,-320) # self.add(menu) # self.sprite.do( repeat(rotate( 360, 4 ) )) def on_key_press( self, keys, mod ): super(startprize,self).on_key_release(keys, mod) if keys == key.enter: if self.is_begin: self.top_notice.element.text="正在抽奖中。。" else: self.press_go = true self.go_prize() return true def on_key_release( self, keys, mod ): super(startprize,self).on_key_release(keys, mod) if keys == key.enter: if self.press_go: self.prize_cycle = 0 self.can_stop = true self.press_go = false return true if keys == key.s: # self.stop_prize() return true def on_mouse_press (self, x, y, buttons, modifiers): px,py = director.get_virtual_coordinates (x, y) # print px,py if px > 188 and px<450 and py>188 and py<450: if self.is_begin: self.top_notice.element.text="正在抽奖中。。" else: self.press_go = true self.go_prize() def on_mouse_release (self, x, y, buttons, modifiers): px,py = director.get_virtual_coordinates (x, y) if self.press_go: self.prize_cycle = 0 self.can_stop = true self.press_go = false def rotate_select(self,dt): if (self.current_num >= 24): self.current_num = 0 self.prize_cycle += 1 if ( self.prize_cycle > 1 and self.can_stop): if( not self.start_slow ): # 减速 self.unschedule(self.rotate_select) self.schedule_interval(self.rotate_select, self.prize_speed_slow) self.start_slow = true # print self.alread_get_prize # print "stopnum" , self.stop_num # print "prize_cycle" , self.prize_cycle # print "current_num" , self.current_num # print self.current_num # 注意rotate_select是要两个参数的 self.sprite.rotation=self.sprite.rotation+15 self.alread_prize_sprite.rotation = self.alread_prize_sprite.rotation+15 if self.current_num in self.alread_get_prize: self.sprite.do(hide()) self.alread_prize_sprite.do(show()) else: self.alread_prize_sprite.do(hide()) self.sprite.do(show()) effect = effect('1.wav') effect.play() if ( self.prize_cycle > 2 and self.stop_num == self.current_num and self.can_stop): self.stop_prize() return true self.current_num += 1 def get_random(self): r = random.randint(0,23) if r in self.alread_get_prize: r = self.get_random() return r def go_prize(self): self.current_num = 0 self.sprite.rotation=0 self.alread_prize_sprite.rotation = 0 self.prize_cycle = 0 self.stop_num = self.get_random() # self.stop_num = 0 if self.stop_num in self.alread_get_prize: self.top_notice.element.text="error, alread get prize" return false self.top_notice.element.text="正在抽奖中。。" self.start_slow = false self.can_stop = false self.is_begin = true # 定时器 self.schedule_interval(self.rotate_select, self.prize_speed) # self.schedule(self.rotate_select) self.bgsprite.do(hide()) self.pressbgsprite.do(show()) def stop_prize(self): self.alread_get_prize.append(self.current_num) self.is_begin = false effect = effect('2.wav') effect.play() self.top_notice.element.text="" self.pressbgsprite.do(hide()) self.bgsprite.do(show()) self.unschedule(self.rotate_select) tests = { 1: startprize, } def main(): director.init(**consts_window) # director.show_fps = true director.run( get_sprite_test( 1 ) ) if __name__ == '__main__': main()
prizemenu这个本来是想要显示要抽哪个奖的,后来需求中不需要了,就没有继续完善。
开发用的是python cocos2d, 还要装pyglet, 最坑爹的是还要装pygame, cocos2d的音效竟然是用pygame的。
因为是name.png是公司同事的姓名,所以就涂黑了,尊重隐私
下载链接:
dehan_prize_run下的prize.exe就可以运行,用py2exe打包成exe的。。。
prize_src.zip是源码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: php实现json编码的方法