python實(shí)現(xiàn)公司年會(huì)抽獎(jiǎng)程序
本文實(shí)例為大家分享了python實(shí)現(xiàn)年會(huì)抽獎(jiǎng)程序的具體代碼,供大家參考,具體內(nèi)容如下
發(fā)一下自己寫(xiě)的公司抽獎(jiǎng)程序。
需求:公司年會(huì)要一個(gè)抽獎(jiǎng)程序,轉(zhuǎn)盤上的每一個(gè)人名是隨機(jī)中獎(jiǎng)的,中獎(jiǎng)后的人不可以再次中獎(jiǎng),按住抽獎(jiǎng),就會(huì)一直在轉(zhuǎn),放開(kāi)后,要再轉(zhuǎn)一兩圈才停。
剛好自己在學(xué)python cocos2d,就用這個(gè)剛學(xué)的東東,直接上源碼
# coding:utf-8
#
import sys
# import os
# sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..'))
# 解決程序中要顯示中文問(wèn)題
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 = "廣州德瀚信息信息科技有限公司-年會(huì)抽獎(jiǎng)"
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('一等獎(jiǎng)', self.prize_go ) ),
( MenuItem('二等獎(jiǎng)', self.prize_go ) ),
( MenuItem('三等獎(jiǎng)', self.prize_go ) ),
( MenuItem('參與獎(jiǎng)', 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 #當(dāng)前位置
self.is_begin = False #是否已經(jīng)開(kāi)始
self.prize_cycle = 0 #轉(zhuǎn)動(dòng)圈數(shù)
self.prize_speed = 0.05 #初始速度
self.prize_speed_slow = 0.3 #慢速度
self.stop_num = 0 #停止的位置
self.alread_get_prize = [] # 已經(jīng)得獎(jiǎng)的人
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 )
# 注意是要有個(gè)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( "點(diǎn)擊中間開(kāi)始抽獎(jiǎng)" )
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 )
# 標(biāo)題
self.add( self.top_label )
self.add( self.top_notice )
# 轉(zhuǎn)動(dòng)的背景圖
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="正在抽獎(jiǎng)中。。"
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="正在抽獎(jiǎng)中。。"
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是要兩個(gè)參數(shù)的
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="正在抽獎(jiǎng)中。。"
self.start_slow = False
self.can_stop = False
self.is_begin = True
# 定時(shí)器
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這個(gè)本來(lái)是想要顯示要抽哪個(gè)獎(jiǎng)的,后來(lái)需求中不需要了,就沒(méi)有繼續(xù)完善。
開(kāi)發(fā)用的是python cocos2d, 還要裝pyglet, 最坑的是還要裝pygame, cocos2d的音效竟然是用pygame的。
因?yàn)槭莕ame.png是公司同事的姓名,所以就涂黑了,尊重隱私
下載鏈接:年會(huì)抽獎(jiǎng)
dehan_prize_run下的prize.exe就可以運(yùn)行,用py2exe打包成exe的。。。
prize_src.zip是源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
windows下Anaconda的安裝與配置正解(Anaconda入門教程)
最近很多朋友學(xué)習(xí)python,很多朋友也推薦使用anaconda這個(gè)工具,但安裝以后也不會(huì)使用,這里腳本之家小編就為大家整理一下比較詳細(xì)的教程,方便自己也方便需要的朋友,希望大家以后多多支持腳本之家2018-04-04
numpy中hstack vstack stack concatenate函數(shù)示例詳解
這篇文章主要為大家介紹了numpy中hstack vstack stack concatenate函數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Python自動(dòng)提取項(xiàng)目中導(dǎo)入的庫(kù)及其版本信息
在我們有時(shí)需要遷移或部署項(xiàng)目時(shí),需要知道項(xiàng)目所依賴的三方包和版本,本文就來(lái)介紹一下Python自動(dòng)提取項(xiàng)目中導(dǎo)入的庫(kù)及其版本信息,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
python如何實(shí)現(xiàn)常用的五種排序算法詳解
排序有很多種實(shí)現(xiàn)方法,比如冒泡排序、選擇排序、歸并排序、希爾排序、快速排序、插入排序、堆排序、基數(shù)排序等,這篇文章主要給大家介紹了關(guān)于python如何實(shí)現(xiàn)常用的五種排序算法,需要的朋友可以參考下2021-08-08
詳解如何在Python中替換文件路徑和要讀取的行號(hào)
這篇文章主要為大家詳細(xì)介紹了如何在Python中替換文件路徑和要讀取的行號(hào),文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2007-02-02
Python turtle實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了Python turtle實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06

