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 中文乱码问题深入分析
Mar 13 Python
Python正则表达式匹配ip地址实例
Oct 09 Python
Python中生成器和yield语句的用法详解
Apr 17 Python
用Python将动态GIF图片倒放播放的方法
Nov 02 Python
Python实现的序列化和反序列化二叉树算法示例
Mar 02 Python
利用python将图片版PDF转文字版PDF
May 03 Python
django页面跳转问题及注意事项
Jul 18 Python
django自带serializers序列化返回指定字段的方法
Aug 21 Python
基于python 等频分箱qcut问题的解决
Mar 03 Python
python中可以声明变量类型吗
Jun 18 Python
Python中json.load()和json.loads()有哪些区别
Jun 07 Python
python识别围棋定位棋盘位置
Jul 26 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
Session服务器配置指南与使用经验的深入解析
2013/06/17 PHP
PHP实现绘制3D扇形统计图及图片缩放实例
2014/10/01 PHP
php上传大文件设置方法
2016/04/14 PHP
php生成图片缩略图功能示例
2017/02/22 PHP
PHP面向对象程序设计子类扩展父类(子类重新载入父类)操作详解
2019/06/14 PHP
浅析Js(Jquery)中,字符串与JSON格式互相转换的示例(直接运行实例)
2013/07/09 Javascript
关于javascript中dataset的问题小结
2015/11/16 Javascript
基于html5和nodejs相结合实现websocket即使通讯
2015/11/19 NodeJs
一个简单的JavaScript Map实例(分享)
2016/08/03 Javascript
jQuery子元素过滤选择器用法示例
2016/09/09 Javascript
浅谈JavaScript事件绑定的常用方法及其优缺点分析
2016/11/01 Javascript
AngularJS开发教程之控制器之间的通信方法分析
2016/12/25 Javascript
解析JavaScript实现DDoS攻击原理与保护措施
2016/12/26 Javascript
angular的输入和输出的使用方法
2018/09/22 Javascript
ES6知识点整理之模块化的应用详解
2019/04/15 Javascript
Vue 3.0 前瞻Vue Function API新特性体验
2019/08/12 Javascript
微信小程序使用前置摄像头拍照
2020/10/22 Javascript
vue项目如何监听localStorage或sessionStorage的变化
2021/01/04 Vue.js
[01:50]2014DOTA2西雅图邀请赛 专访欢乐周宝龙
2014/07/08 DOTA
Python中使用Tkinter模块创建GUI程序实例
2015/01/14 Python
python解决网站的反爬虫策略总结
2016/10/26 Python
Python网络编程 Python套接字编程
2017/09/13 Python
简述:我为什么选择Python而不是Matlab和R语言
2017/11/14 Python
python3+PyQt5实现自定义窗口部件Counters
2018/04/20 Python
浅析Python3 pip换源问题
2020/01/06 Python
使用Python3 poplib模块删除服务器多天前的邮件实现代码
2020/04/24 Python
python 操作mysql数据中fetchone()和fetchall()方式
2020/05/15 Python
Python2手动安装更新pip过程实例解析
2020/07/16 Python
美国专注于健康商品的网站:eVitamins
2017/01/23 全球购物
Opodo英国旅游网站:预订廉价航班、酒店和汽车租赁
2018/07/14 全球购物
管理站站长岗位职责
2013/11/27 职场文书
市场开发与营销专业求职信
2013/12/31 职场文书
国家税务局干部作风整顿整改措施
2014/09/18 职场文书
群众路线教育实践活动剖析材料
2014/09/30 职场文书
卖房授权委托书样本
2014/10/05 职场文书
八年级作文之一起的走过日子
2019/09/17 职场文书