python3实现小球转动抽奖小游戏


Posted in Python onApril 15, 2020

最近老师在讲 tkinter,所以我做了一个抽奖小游戏。

一、效果图

先上效果图。红色的小球会围绕蓝色小球做环形运动。我设置的四个角是奖品,其余的都是再接再厉。

python3实现小球转动抽奖小游戏

二、方法

基于tkinter中的button,text,PIL ,time.Canvas

drawPath():用于画蓝色的小球

Ball类 初始化画布、运动小球大小、运动的起点。

ball类-》draw() 控制小球的运动。这里用到一个方法叫canvas.coords。这个方法可以获取运动小球当前在画布上的坐标。并返回一个数组。比如 pos=self.canvas.coords 。左边:pos[0],右边pos[2],上边:pos[1],下边:pos[3].用if和pos 可以控制小球的上下左右运动。

self.canvas.move(self.id,self.x,self.y)
 #获取某个对象在画布的坐标,返回一个数组(两个坐标,左上角的坐标和右下角的两个坐标)
   pos = self.canvas.coords(self.id)
   getNowPoint(pos[0],pos[1],pos[2],pos[3])
   #打印获取的坐标
 
   #如果最上面的纵轴坐标在顶上,则往下移动一个像素
   if pos[1] <=30 and self.y==-80:
    self.x = 80
    self.y=0
    print("pos1" + str(self.x) + ":pos1:" + str(self.y))
   #如果最下面的纵轴坐标在底上,则向左移动
   elif pos[3] > 300 and self.x==0 and self.y==80:
    self.x = -80
    self.y=0
    print("pos3" + str(self.x) + ":pos3:" + str(self.y))
   #宽度控制#
   #如果在左边框了,那么向右边移动3像素
   elif pos[0] <30 and self.x== -80:
    self.x = 0
    self.y= -80
    print("pos0" + str(self.x) + ":pos0:" + str(self.y))
   #如果到右边框了,左移动3像素
   elif pos[2] > 300 and self.y==0:
 
    self.x = 0
    self.y=80
    print("pos2:" + str(self.x) + "pos2:" + str(self.y))

getNowPoint()当前红色运动小球的位置。

放图片的函数:

img44 = Image.open("px.jpg")
img_file44 = ImageTk.PhotoImage(img44)
canvas.create_image(200, 200, image=img_file44)(参数1,2 图片的位置x,y,参数3是图片)

三、遇到的问题

老师教的显示canvas上的内容要用mainloop(),所以一开始不知道怎么让小球动起来,最后查阅了很多资料发现。其实不用mainloop也行。可以使用tk.update() 刷新tk上的内容。所以这里我们要用一个while让小球每动一次窗体就刷新一次。time.sleep()控制小球运动速度。

四、代码

from tkinter import *
import random
import time
from PIL import Image, ImageTk
#
#创建一个类,这个类含有两个参数,一个是画布,一个是球的颜色
#
count = 0
#a = eval(input('time:'))
#b = a
global isStop
global num
isStop=0
 
def stopplay():
  global isStop
  isStop=1
def startplay():
 global isStop
 isStop = 0
class Ball:
 def __init__(self,canvas,color):
  self.canvas = canvas
  self.id = canvas.create_oval(0,0,35,35,fill=color)
  self.canvas.move(self.id,10,5)
  self.x = 80
  self.y = 0
 def draw(self):
  if isStop==0:
   self.canvas.move(self.id,self.x,self.y)
   #获取某个对象在画布的坐标,返回一个数组(两个坐标,左上角的坐标和右下角的两个坐标)
   pos = self.canvas.coords(self.id)
   getNowPoint(pos[0],pos[1],pos[2],pos[3])
   #打印获取的坐标
 
   #如果最上面的纵轴坐标在顶上,则往下移动一个像素
   if pos[1] <=30 and self.y==-80:
    self.x = 80
    self.y=0
    print("pos1" + str(self.x) + ":pos1:" + str(self.y))
   #如果最下面的纵轴坐标在底上,则向左移动
   elif pos[3] > 300 and self.x==0 and self.y==80:
    self.x = -80
    self.y=0
    print("pos3" + str(self.x) + ":pos3:" + str(self.y))
   #宽度控制#
   #如果在左边框了,那么向右边移动3像素
   elif pos[0] <30 and self.x== -80:
    self.x = 0
    self.y= -80
    print("pos0" + str(self.x) + ":pos0:" + str(self.y))
   #如果到右边框了,左移动3像素
   elif pos[2] > 300 and self.y==0:
 
    self.x = 0
    self.y=80
    print("pos2:" + str(self.x) + "pos2:" + str(self.y))
  if isStop==1:
   print("停止")
   self.canvas.move(self.id, self.x, self.y)
   # 获取某个对象在画布的坐标,返回一个数组(两个坐标,左上角的坐标和右下角的两个坐标)
   pos = self.canvas.coords(self.id)
   print(pos)
 
def getNowPoint(x1,y1,x2,y2):
 global num
 print("现在在")
 print(x1,y1,x2,y2)
 row=(x1-10)/80
 line=(y1-5)/80
 num=str(int(row))+str(int(line))
 print("点"+str(int(row))+str(int(line)))
 #return num
 
def drawPath():
 for i in range(5):
  for j in range(5):
   if i==0 or i==4:
    point = (20+80*j, 20+ 80 * i, 35+80*j, 35+ 80 * i)
    oil = canvas.create_oval(point, fill='lightblue')
   elif j==0 or j==4:
    # print("$")
    point = (20+80*j,20+ 80 * i, 35+80*j , 35+ 80 * i)
    oil = canvas.create_oval(point, fill='lightblue')
 
 
 #创建画布
 
tk = Tk()
 
tk.title("Game_ball")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
#bd=0,highlightthickness=0 画布之外没有边框
 
canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
 
canvas.pack()
 
tk.update()
 
point=(30,30,45,45)
 
 
#创建对象
ball = Ball(canvas,'red')
drawPath()
#一直保持循环
btn_start = Button(tk,text='start',width='20',command=startplay)
btn_start.pack()
btn_end=Button(tk,text='end',width='20',command=stopplay)
btn_end.pack()
global txt
txt=""
text1=Text(tk,width=30,height=4)
while 1:
 if isStop==0:
  txt = " "
  text1.insert(INSERT, txt)
  text1.delete(0.0,INSERT)
  imgtt = Image.open("tt.jpg")
  img_filett = ImageTk.PhotoImage(imgtt)
  canvas.create_image(200, 200, image=img_filett)
  while 1:
   ball.draw()
   #快速刷新屏幕
   tk.update_idletasks()
   tk.update()
   time.sleep(0.1)
   if isStop==1:
    break
 if isStop==1:
  txt="要加油哦"
  print("num" + num)
  print(type(num))
  print(type("04"))
  if num=="00" or num=="40" or num== "04" or num== "44":
    if num=="00":
     img00=Image.open("3.jpg")
     img_file00=ImageTk.PhotoImage(img00)
     canvas.create_image(200,200,image=img_file00)
     text1.insert(INSERT,"恭喜获得键盘!!!!")
     text1.tag_configure('bold',font=('Arial', 20, 'bold', 'italic'))
    elif num=="40":
     img40 = Image.open("4.jpg")
     img_file40 = ImageTk.PhotoImage(img40)
     canvas.create_image(200, 200, image=img_file40)
     text1.insert(INSERT, "恭喜获得耳机!!!!")
     text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
     text1.pack()
    elif num=="04":
     img04 = Image.open("mac.jpg")
     img_file04 = ImageTk.PhotoImage(img04)
     canvas.create_image(200, 200, image=img_file04)
     text1.insert(INSERT, "恭喜获得MAC!!!!")
     text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
     text1.pack()
    elif num=="44":
     img44 = Image.open("px.jpg")
     img_file44 = ImageTk.PhotoImage(img44)
     canvas.create_image(200, 200, image=img_file44)
     text1.insert(INSERT, "恭喜获得IPHONE XS MAX!!!!")
     text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
     text1.pack()
  else:
   #L1 = Label(tk, text=txt, font=('宋体', '28'))
   #L1.pack()
 
   text1.insert(INSERT,txt)
   text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
   text1.pack()
  while 1:
   #ball.draw()
   # 快速刷新屏幕
   tk.update_idletasks()
   tk.update()
   time.sleep(0.1)
   #print("num"+num)
   if isStop == 0:
    break

想要学习更多关于抽奖功能的实现,请参考此专题:抽奖功能

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

Python 相关文章推荐
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
Sep 06 Python
Python中统计函数运行耗时的方法
May 05 Python
Python之re操作方法(详解)
Jun 14 Python
python3实现域名查询和whois查询功能
Jun 21 Python
django如何连接已存在数据的数据库
Aug 14 Python
Python基于Tkinter模块实现的弹球小游戏
Dec 27 Python
Python一行代码解决矩阵旋转的问题
Nov 30 Python
Python +Selenium解决图片验证码登录或注册问题(推荐)
Feb 09 Python
Python接口测试结果集实现封装比较
May 01 Python
django haystack实现全文检索的示例代码
Jun 24 Python
解决运行出现'dict' object has no attribute 'has_key'问题
Jul 15 Python
python 实用工具状态机transitions
Nov 21 Python
Django保护敏感信息的方法示例
May 09 #Python
Python基于scipy实现信号滤波功能
May 08 #Python
python实现抽奖小程序
Apr 15 #Python
Python常见数据类型转换操作示例
May 08 #Python
Python数据类型之Number数字操作实例详解
May 08 #Python
利用PyCharm Profile分析异步爬虫效率详解
May 08 #Python
Python数据类型之String字符串实例详解
May 08 #Python
You might like
解析php中memcache的应用
2013/06/18 PHP
php目录拷贝实现方法
2015/07/10 PHP
用php代码限制国内IP访问我们网站
2015/09/26 PHP
PHP MySql增删改查的简单实例
2016/06/21 PHP
jQuery中:password选择器用法实例
2015/01/03 Javascript
js+css实现上下翻页相册代码分享
2015/08/18 Javascript
JavaScript实现跑马灯抽奖活动实例代码解析与优化(一)
2016/02/16 Javascript
nodeJs内存泄漏问题详解
2016/09/05 NodeJs
实例解析angularjs的filter过滤器
2016/12/14 Javascript
微信小程序 安全包括(框架、功能模块、账户使用)详解
2017/01/16 Javascript
jQuery轻松实现无缝轮播效果
2017/03/22 jQuery
利用js的闭包原理做对象封装及调用方法
2017/04/07 Javascript
详解微信小程序设置底部导航栏目方法
2017/06/29 Javascript
Validform验证时可以为空否则按照指定格式验证
2017/10/20 Javascript
[原创]jQuery实现合并/追加数组并去除重复项的方法
2018/04/11 jQuery
JS对象属性的检测与获取操作实例分析
2020/03/17 Javascript
jQuery实现鼠标放置名字上显示详细内容气泡提示框效果的方法分析
2020/04/04 jQuery
javascript设计模式 ? 解释器模式原理与用法实例分析
2020/04/17 Javascript
下载给定网页上图片的方法
2014/02/18 Python
使用C++扩展Python的功能详解
2018/01/12 Python
Python实现批量压缩图片
2018/01/25 Python
对sklearn的使用之数据集的拆分与训练详解(python3.6)
2018/12/14 Python
Django uwsgi Nginx 的生产环境部署详解
2019/02/02 Python
Flask框架中request、请求钩子、上下文用法分析
2019/07/23 Python
PyCharm 配置远程python解释器和在本地修改服务器代码
2019/07/23 Python
使用Django搭建一个基金模拟交易系统教程
2019/11/18 Python
Python Celery多队列配置代码实例
2019/11/22 Python
从训练好的tensorflow模型中打印训练变量实例
2020/01/20 Python
python误差棒图errorbar()函数实例解析
2020/02/11 Python
解决Jupyter Notebook开始菜单栏Anaconda下消失的问题
2020/04/13 Python
模具设计与制造专业推荐信
2014/02/16 职场文书
党员领导干部承诺书
2014/05/28 职场文书
留学生求职信
2014/06/03 职场文书
2014旅游局领导班子四风问题对照检查材料思想汇报
2014/09/19 职场文书
光棍节联谊晚会活动策划书
2014/10/10 职场文书
优秀党员主要事迹范文
2015/11/05 职场文书