Python开发转盘小游戏
Python开发转盘小游戏
一 原理分析
- Python开发一个图形界面
- 有12个选项和2个功能键
- 确定每个按钮的位置
每个按钮的间隔相同 - 点击开始时转动,当前选项的背景颜色为红色,其他为白色
二 知识点与难点
2.1 使用的package
- tkinter
Python内置,实现GUI的主要package - threading
实现多线程实现循环 - time
用来延时
2.2 注意点
- 判断类型时使用isinstance函数,不要用type,因为type不区分子类
- 判断变量是否为TRUE,FALSE,None时要用is不要用==
- 循环时记得处理自增时超过下标时的归零
- tkinter下面的按钮属性可以通过键值赋值改变属性
- 全局变量
三 代码
- import tkinter
- import threading
- import time
- root = tkinter.Tk()
- # 设置窗口名字
- root.title('香港男神')
- # 设置窗口大小
- root.minsize(300, 300)
- # 按钮
- btn1 = tkinter.Button(root, text='成龙', bg='red')
- btn1.place(x=20, y=20, width=50, height=50)
- btn2 = tkinter.Button(root, text='张国荣', bg='white')
- btn2.place(x=90, y=20, width=50, height=50)
- btn3 = tkinter.Button(root, text='郭富城', bg='white')
- btn3.place(x=160, y=20, width=50, height=50)
- btn4 = tkinter.Button(root, text='黎明', bg='white')
- btn4.place(x=230, y=20, width=50, height=50)
- btn5 = tkinter.Button(root, text='刘德华', bg='white')
- btn5.place(x=230, y=90, width=50, height=50)
- btn6 = tkinter.Button(root, text='梁朝伟', bg='white')
- btn6.place(x=230, y=160, width=50, height=50)
- btn7 = tkinter.Button(root, text='张学友', bg='white')
- btn7.place(x=230, y=230, width=50, height=50)
- btn8 = tkinter.Button(root, text='周润发', bg='white')
- btn8.place(x=160, y=230, width=50, height=50)
- btn9 = tkinter.Button(root, text='周星驰', bg='white')
- btn9.place(x=90, y=230, width=50, height=50)
- btn10 = tkinter.Button(root, text='谢霆锋', bg='white')
- btn10.place(x=20, y=230, width=50, height=50)
- btn11 = tkinter.Button(root, text='张家辉', bg='white')
- btn11.place(x=20, y=160, width=50, height=50)
- btn12 = tkinter.Button(root, text='古天乐', bg='white')
- btn12.place(x=20, y=90, width=50, height=50)
- # 按钮列表
- herolist = [btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12]
- # 开启的标志
- isloop = False
- # 停止的标志
- isstop = False
- # 停止时的ID
- stopid = None
- # 定义一个函数,实现循环功能
- def round():
- # 设为全局变量,在此处做的改动可应用在其他函数中
- global isloop
- global stopid
- # 设置ID开始值
- i = 1
- # 已经在循环中
- if isloop is True:
- return
- if isinstance(stopid, int):
- i = stopid
- # 开始循环
- while True:
- # 睡眠
- time.sleep(0.03)
- # 设置每个按钮的背景色
- # 可以通过键值得方式设置按钮属性
- for e_btn in herolist:
- e_btn['bg'] = 'white'
- # 当前按钮背景色设置为红色
- herolist[i]['bg'] = 'red'
- i += 1
- print('当前i为', i)
- # 当i的值大于选项个数时,归零
- if i >= 12:
- i = 0
- # 当停止按钮被激活时,停止循环
- if isstop is True:
- isloop = False
- stopid = i
- break
- # 定义停止函数,只是将停止标志设置为True
- def my_stop():
- global isstop
- # 已经是停止状态时,不变
- if isstop is True:
- return
- isstop = True
- # 定义开始函数
- def newtask():
- global isloop
- global isstop
- isstop = False
- # 使用线程
- t = threading.Thread(target=round)
- t.start()
- # 开启循环标志
- isloop = True
- # 设置开始按钮
- btn_start = tkinter.Button(root, text='开始', command=newtask)
- btn_start.place(x=90, y=125, width=50, height=50)
- # 设置停止按钮
- btn_stop = tkinter.Button(root, text='停止', command=my_stop)
- btn_stop.place(x=160, y=125, width=50, height=50)
- root.mainloop()

Python开发转盘小游戏的更多相关文章
- 教你用Python自制拼图小游戏,一起来制作吧
摘要: 本文主要为大家详细介绍了python实现拼图小游戏,文中还有示例代码介绍,感兴趣的小伙伴们可以参考一下. 开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Pyt ...
- 【转】利用 three.js 开发微信小游戏的尝试
前言 这是一次利用 three.js 开发微信小游戏的尝试,并不能算作是教程,只能算是一篇笔记吧. 微信 WeChat 6.6.1 开始引入了微信小游戏,初期上线了一批质量相当不错的小游戏.我在查阅各 ...
- 使用Laya引擎开发微信小游戏(上)
本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...
- 使用Laya引擎开发微信小游戏
在支持微信小游戏的游戏引擎中,Cocos,Egret,Laya都对小游戏的开发提供了很多强大的支持.前段时间正好抽空研究了一下这块的内容,现做一个总结,针对如何使用Laya引擎开发微信小游戏给大家做一 ...
- 用Python实现童年小游戏贪吃蛇
贪吃蛇作为一款经典小游戏,早在 1976 年就面世了,我最早接触它还是在家长的诺基亚手机中.
- python 小鸡飞行小游戏
python 小鸡飞行小游戏 用空格键控制小鸡飞行 代码 import pygame.freetype import sys import random pygame.init() screen = ...
- HTML5游戏开发,剪刀石头布小游戏案例
剪刀石头布,非常可爱的小游戏,相信大家都非常的怀念这款小游戏,小时候也玩过很多次,陪伴着我的童年的成长,现在是不是还会玩一下,剪刀石头布游戏的规则我们都知道是:剪刀剪布,石头砸剪刀,布包石头.跟朋友. ...
- 【h5-egret】如何快速开发一个小游戏
1.环境搭建 安装教程传送门:http://edn.egret.com/cn/index.php?g=&m=article&a=index&id=207&terms1_ ...
- 开发H5小游戏
Egret白鹭H5小游戏开发入门(一) 前言: 好久没更新博客了,以前很多都不会,所以常常写博客总结,倒是现在有点点经验了就懒了.在过去的几个月里,在canvas游戏框架方面,撸过了CreateJ ...
随机推荐
- python中fork()函数生成子进程分析-乾颐堂
python的os module中有fork()函数用于生成子进程,生成的子进程是父进程的镜像,但是它们有各自的地址空间,子进程复制一份父进程内存给自己,两个进程之 间的执行是相互独立的,其执行顺序可 ...
- Oracle RMAN-06023 和ORA-19693错误
在将一个0级备份的数据库还原到其它机器上时,首先遇到了RMAN-06023然后遇到ORA-19693错误,错误发生的环境和内容大致如下: 数据库版本: SQL> select * from v$ ...
- 1256 Anagram
题目链接: http://poj.org/problem?id=1256 题意: 根据自定义的字典序: 'A'<'a'<'B'<'b'<...<'Z'<'z' 和输 ...
- win10 64位 python3.6 django1.11 MysqlDB No module named 'MySQLdb' 安装MysqlDB报错 Microsoft Visual C++ 14.0 is required
在python3.6中操作数据库,再按python2.7安装MySQLdb进行数据库连接已经不可用了,我使用的是另外一个方法:PyMySQL,安装好之后还是不能直接连接MySQL的,启动项目后报No ...
- HDU 6007 Mr. Panda and Crystal (背包+spfa)
题意:你生活在一个魔法大陆上,你有n 魔力, 这个大陆上有m 种魔法水晶,还有n 种合成水晶的方式,每种水晶价格告诉你,并且告诉你哪些水晶你能直接造出来,哪些你必须合成才能造出来,问你n魔力最多能卖多 ...
- CodeForces 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...
- mysql - 索引的一些理解
推荐下这篇文章 http://blog.codinglabs.org/articles/theory-of-mysql-index.html 写的十分的不错.留着以后慢慢看,慢慢的理解.
- Introduction mybatis
项目地址 https://github.com/mybatis/mybatis-3 英文官网 http://mybatis.github.io/mybatis-3/ 中文官网 http://mybat ...
- Android新增的注解
环境 使用Android注解前需要导入相关的包 compile 'com.android.support:support-annotations:latest.integration' 注意:如果我们 ...
- Android 文字垂直居中
android中自定义控件,自己绘制文字canvas.drawText()的时候,怎样才能让文字垂直居中那? drawText()的方法说明 也就是使用paint画笔在(X,Y)处进行绘制,X为横向坐 ...