侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   188

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

一、效果图

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

二、方法

基于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 randomimport timefrom PIL import Image, ImageTk##创建一个类,这个类含有两个参数,一个是画布,一个是球的颜色#count = 0#a = eval(input('time:'))#b = aglobal isStopglobal numisStop=0 def stopplay():   global isStop   isStop=1def startplay():  global isStop  isStop = 0class 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 txttxt=""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 Excel处理库openpyxl使用详解
    下一条:
    使用Python做定时任务及时了解互联网动态
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客