经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
python3实现小球转动抽奖小游戏
来源:jb51  时间:2019/5/10 8:40:19  对本文有异议

最近老师在讲 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 可以控制小球的上下左右运动。

  1. self.canvas.move(self.id,self.x,self.y)
  2. #获取某个对象在画布的坐标,返回一个数组(两个坐标,左上角的坐标和右下角的两个坐标)
  3. pos = self.canvas.coords(self.id)
  4. getNowPoint(pos[0],pos[1],pos[2],pos[3])
  5. #打印获取的坐标
  6. #如果最上面的纵轴坐标在顶上,则往下移动一个像素
  7. if pos[1] <=30 and self.y==-80:
  8. self.x = 80
  9. self.y=0
  10. print("pos1" + str(self.x) + ":pos1:" + str(self.y))
  11. #如果最下面的纵轴坐标在底上,则向左移动
  12. elif pos[3] > 300 and self.x==0 and self.y==80:
  13. self.x = -80
  14. self.y=0
  15. print("pos3" + str(self.x) + ":pos3:" + str(self.y))
  16. #宽度控制#
  17. #如果在左边框了,那么向右边移动3像素
  18. elif pos[0] <30 and self.x== -80:
  19. self.x = 0
  20. self.y= -80
  21. print("pos0" + str(self.x) + ":pos0:" + str(self.y))
  22. #如果到右边框了,左移动3像素
  23. elif pos[2] > 300 and self.y==0:
  24. self.x = 0
  25. self.y=80
  26. print("pos2:" + str(self.x) + "pos2:" + str(self.y))

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

放图片的函数:

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

三、遇到的问题

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

四、代码

  1. from tkinter import *
  2. import random
  3. import time
  4. from PIL import Image, ImageTk
  5. #
  6. #创建一个类,这个类含有两个参数,一个是画布,一个是球的颜色
  7. #
  8. count = 0
  9. #a = eval(input('time:'))
  10. #b = a
  11. global isStop
  12. global num
  13. isStop=0
  14. def stopplay():
  15. global isStop
  16. isStop=1
  17. def startplay():
  18. global isStop
  19. isStop = 0
  20. class Ball:
  21. def __init__(self,canvas,color):
  22. self.canvas = canvas
  23. self.id = canvas.create_oval(0,0,35,35,fill=color)
  24. self.canvas.move(self.id,10,5)
  25. self.x = 80
  26. self.y = 0
  27. def draw(self):
  28. if isStop==0:
  29. self.canvas.move(self.id,self.x,self.y)
  30. #获取某个对象在画布的坐标,返回一个数组(两个坐标,左上角的坐标和右下角的两个坐标)
  31. pos = self.canvas.coords(self.id)
  32. getNowPoint(pos[0],pos[1],pos[2],pos[3])
  33. #打印获取的坐标
  34. #如果最上面的纵轴坐标在顶上,则往下移动一个像素
  35. if pos[1] <=30 and self.y==-80:
  36. self.x = 80
  37. self.y=0
  38. print("pos1" + str(self.x) + ":pos1:" + str(self.y))
  39. #如果最下面的纵轴坐标在底上,则向左移动
  40. elif pos[3] > 300 and self.x==0 and self.y==80:
  41. self.x = -80
  42. self.y=0
  43. print("pos3" + str(self.x) + ":pos3:" + str(self.y))
  44. #宽度控制#
  45. #如果在左边框了,那么向右边移动3像素
  46. elif pos[0] <30 and self.x== -80:
  47. self.x = 0
  48. self.y= -80
  49. print("pos0" + str(self.x) + ":pos0:" + str(self.y))
  50. #如果到右边框了,左移动3像素
  51. elif pos[2] > 300 and self.y==0:
  52. self.x = 0
  53. self.y=80
  54. print("pos2:" + str(self.x) + "pos2:" + str(self.y))
  55. if isStop==1:
  56. print("停止")
  57. self.canvas.move(self.id, self.x, self.y)
  58. # 获取某个对象在画布的坐标,返回一个数组(两个坐标,左上角的坐标和右下角的两个坐标)
  59. pos = self.canvas.coords(self.id)
  60. print(pos)
  61. def getNowPoint(x1,y1,x2,y2):
  62. global num
  63. print("现在在")
  64. print(x1,y1,x2,y2)
  65. row=(x1-10)/80
  66. line=(y1-5)/80
  67. num=str(int(row))+str(int(line))
  68. print("点"+str(int(row))+str(int(line)))
  69. #return num
  70. def drawPath():
  71. for i in range(5):
  72. for j in range(5):
  73. if i==0 or i==4:
  74. point = (20+80*j, 20+ 80 * i, 35+80*j, 35+ 80 * i)
  75. oil = canvas.create_oval(point, fill='lightblue')
  76. elif j==0 or j==4:
  77. # print("$")
  78. point = (20+80*j,20+ 80 * i, 35+80*j , 35+ 80 * i)
  79. oil = canvas.create_oval(point, fill='lightblue')
  80. #创建画布
  81. tk = Tk()
  82. tk.title("Game_ball")
  83. tk.resizable(0,0)
  84. tk.wm_attributes("-topmost",1)
  85. #bd=0,highlightthickness=0 画布之外没有边框
  86. canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
  87. canvas.pack()
  88. tk.update()
  89. point=(30,30,45,45)
  90. #创建对象
  91. ball = Ball(canvas,'red')
  92. drawPath()
  93. #一直保持循环
  94. btn_start = Button(tk,text='start',width='20',command=startplay)
  95. btn_start.pack()
  96. btn_end=Button(tk,text='end',width='20',command=stopplay)
  97. btn_end.pack()
  98. global txt
  99. txt=""
  100. text1=Text(tk,width=30,height=4)
  101. while 1:
  102. if isStop==0:
  103. txt = " "
  104. text1.insert(INSERT, txt)
  105. text1.delete(0.0,INSERT)
  106. imgtt = Image.open("tt.jpg")
  107. img_filett = ImageTk.PhotoImage(imgtt)
  108. canvas.create_image(200, 200, image=img_filett)
  109. while 1:
  110. ball.draw()
  111. #快速刷新屏幕
  112. tk.update_idletasks()
  113. tk.update()
  114. time.sleep(0.1)
  115. if isStop==1:
  116. break
  117. if isStop==1:
  118. txt="要加油哦"
  119. print("num" + num)
  120. print(type(num))
  121. print(type("04"))
  122. if num=="00" or num=="40" or num== "04" or num== "44":
  123. if num=="00":
  124. img00=Image.open("3.jpg")
  125. img_file00=ImageTk.PhotoImage(img00)
  126. canvas.create_image(200,200,image=img_file00)
  127. text1.insert(INSERT,"恭喜获得键盘!!!!")
  128. text1.tag_configure('bold',font=('Arial', 20, 'bold', 'italic'))
  129. elif num=="40":
  130. img40 = Image.open("4.jpg")
  131. img_file40 = ImageTk.PhotoImage(img40)
  132. canvas.create_image(200, 200, image=img_file40)
  133. text1.insert(INSERT, "恭喜获得耳机!!!!")
  134. text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
  135. text1.pack()
  136. elif num=="04":
  137. img04 = Image.open("mac.jpg")
  138. img_file04 = ImageTk.PhotoImage(img04)
  139. canvas.create_image(200, 200, image=img_file04)
  140. text1.insert(INSERT, "恭喜获得MAC!!!!")
  141. text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
  142. text1.pack()
  143. elif num=="44":
  144. img44 = Image.open("px.jpg")
  145. img_file44 = ImageTk.PhotoImage(img44)
  146. canvas.create_image(200, 200, image=img_file44)
  147. text1.insert(INSERT, "恭喜获得IPHONE XS MAX!!!!")
  148. text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
  149. text1.pack()
  150. else:
  151. #L1 = Label(tk, text=txt, font=('宋体', '28'))
  152. #L1.pack()
  153. text1.insert(INSERT,txt)
  154. text1.tag_configure('bold', font=('Arial', 20, 'bold', 'italic'))
  155. text1.pack()
  156. while 1:
  157. #ball.draw()
  158. # 快速刷新屏幕
  159. tk.update_idletasks()
  160. tk.update()
  161. time.sleep(0.1)
  162. #print("num"+num)
  163. if isStop == 0:
  164. break

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号