python实现公司年会抽奖程序


Posted in Python onJanuary 22, 2019

本文实例为大家分享了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是源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 中文字符串的处理实现代码
Oct 25 Python
Python yield 小结和实例
Apr 25 Python
编写Python爬虫抓取暴走漫画上gif图片的实例分享
Apr 20 Python
python字符串过滤性能比较5种方法
Jun 22 Python
Python程序运行原理图文解析
Feb 10 Python
DataFrame中去除指定列为空的行方法
Apr 08 Python
Python利用pandas计算多个CSV文件数据值的实例
Apr 19 Python
如何利用python查找电脑文件
Apr 27 Python
pandas使用apply多列生成一列数据的实例
Nov 28 Python
Python新手学习标准库模块命名
May 29 Python
Python 实现自动登录+点击+滑动验证功能
Jun 10 Python
python实现启动一个外部程序,并且不阻塞当前进程
Dec 05 Python
对python函数签名的方法详解
Jan 22 #Python
python实现大转盘抽奖效果
Jan 22 #Python
Python函数返回不定数量的值方法
Jan 22 #Python
python实现转盘效果 python实现轮盘抽奖游戏
Jan 22 #Python
Python Pillow Image Invert
Jan 22 #Python
python 通过类中一个方法获取另一个方法变量的实例
Jan 22 #Python
对Python 获取类的成员变量及临时变量的方法详解
Jan 22 #Python
You might like
用Socket发送电子邮件
2006/10/09 PHP
PHP 以POST方式提交XML、获取XML,解析XML详解及实例
2016/10/26 PHP
PHP生成推广海报的方法分享
2018/04/22 PHP
javascript标签在页面中的位置探讨
2013/04/11 Javascript
angularJS 中input示例分享
2015/02/09 Javascript
BootStrap的双日历时间控件使用
2017/07/25 Javascript
JavaScript闭包的简单应用
2017/09/01 Javascript
基于axios 解决跨域cookie丢失的问题
2018/09/26 Javascript
js实现全选反选不选功能代码详解
2019/04/24 Javascript
python基础教程之获取本机ip数据包示例
2014/02/10 Python
Python中import导入上一级目录模块及循环import问题的解决
2016/06/04 Python
Python编程实现删除VC临时文件及Debug目录的方法
2017/03/22 Python
Python中关于Sequence切片的下标问题详解
2017/06/15 Python
Python实现希尔排序算法的原理与用法实例分析
2017/11/23 Python
python实现两张图片拼接为一张图片并保存
2019/07/16 Python
django多个APP的urls设置方法(views重复问题解决)
2019/07/19 Python
Python hashlib模块加密过程解析
2019/11/05 Python
Python 实现Serial 与STM32J进行串口通讯
2019/12/18 Python
Django 返回json数据的实现示例
2020/03/05 Python
150行Python代码实现带界面的数独游戏
2020/04/04 Python
将pymysql获取到的数据类型是tuple转化为pandas方式
2020/05/15 Python
CAT鞋美国官网:CAT Footwear
2017/11/27 全球购物
Currentbody美国/加拿大:美容仪专家
2020/03/09 全球购物
标记环网Toke Ring IEEE802.5
2014/05/26 面试题
文员自我评价怎么写
2013/09/19 职场文书
幼儿园的门卫岗位职责
2014/04/10 职场文书
校园环保建议书
2014/05/14 职场文书
毕业生求职信
2014/06/10 职场文书
农民工讨薪标语
2014/06/26 职场文书
党性观念心得体会
2014/09/03 职场文书
党员群众路线整改措施及今后努力方向
2014/10/28 职场文书
2014年学习部工作总结
2014/11/12 职场文书
战马观后感
2015/06/08 职场文书
2019年最新感恩节祝福语(28句)
2019/11/27 职场文书
使用Docker容器部署rocketmq单机的全过程
2022/04/03 Servers
golang的文件创建及读写操作
2022/04/14 Golang