最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3實(shí)現(xiàn)小球轉(zhuǎn)動(dòng)抽獎(jiǎng)小游戲

 更新時(shí)間:2020年04月15日 15:13:08   作者:Climale-z  
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)小球轉(zhuǎn)動(dòng)抽獎(jiǎng)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近老師在講 tkinter,所以我做了一個(gè)抽獎(jiǎng)小游戲。

一、效果圖

先上效果圖。紅色的小球會(huì)圍繞藍(lán)色小球做環(huán)形運(yùn)動(dòng)。我設(shè)置的四個(gè)角是獎(jiǎng)品,其余的都是再接再厲。

二、方法

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

drawPath():用于畫藍(lán)色的小球

Ball類 初始化畫布、運(yùn)動(dòng)小球大小、運(yùn)動(dòng)的起點(diǎn)。

ball類-》draw() 控制小球的運(yùn)動(dòng)。這里用到一個(gè)方法叫canvas.coords。這個(gè)方法可以獲取運(yùn)動(dòng)小球當(dāng)前在畫布上的坐標(biāo)。并返回一個(gè)數(shù)組。比如 pos=self.canvas.coords 。左邊:pos[0],右邊pos[2],上邊:pos[1],下邊:pos[3].用if和pos 可以控制小球的上下左右運(yùn)動(dòng)。

 self.canvas.move(self.id,self.x,self.y)
 #獲取某個(gè)對(duì)象在畫布的坐標(biāo),返回一個(gè)數(shù)組(兩個(gè)坐標(biāo),左上角的坐標(biāo)和右下角的兩個(gè)坐標(biāo))
   pos = self.canvas.coords(self.id)
   getNowPoint(pos[0],pos[1],pos[2],pos[3])
   #打印獲取的坐標(biāo)
 
   #如果最上面的縱軸坐標(biāo)在頂上,則往下移動(dòng)一個(gè)像素
   if pos[1] <=30 and self.y==-80:
    self.x = 80
    self.y=0
    print("pos1" + str(self.x) + ":pos1:" + str(self.y))
   #如果最下面的縱軸坐標(biāo)在底上,則向左移動(dòng)
   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))
   #寬度控制#
   #如果在左邊框了,那么向右邊移動(dòng)3像素
   elif pos[0] <30 and self.x== -80:
    self.x = 0
    self.y= -80
    print("pos0" + str(self.x) + ":pos0:" + str(self.y))
   #如果到右邊框了,左移動(dòng)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()當(dāng)前紅色運(yùn)動(dòng)小球的位置。

放圖片的函數(shù):

img44 = Image.open("px.jpg")
img_file44 = ImageTk.PhotoImage(img44)
canvas.create_image(200, 200, image=img_file44)(參數(shù)1,2 圖片的位置x,y,參數(shù)3是圖片)

三、遇到的問題

老師教的顯示canvas上的內(nèi)容要用mainloop(),所以一開始不知道怎么讓小球動(dòng)起來,最后查閱了很多資料發(fā)現(xiàn)。其實(shí)不用mainloop也行??梢允褂胻k.update() 刷新tk上的內(nèi)容。所以這里我們要用一個(gè)while讓小球每動(dòng)一次窗體就刷新一次。time.sleep()控制小球運(yùn)動(dòng)速度。

四、代碼

from tkinter import *
import random
import time
from PIL import Image, ImageTk
#
#創(chuàng)建一個(gè)類,這個(gè)類含有兩個(gè)參數(shù),一個(gè)是畫布,一個(gè)是球的顏色
#
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)
   #獲取某個(gè)對(duì)象在畫布的坐標(biāo),返回一個(gè)數(shù)組(兩個(gè)坐標(biāo),左上角的坐標(biāo)和右下角的兩個(gè)坐標(biāo))
   pos = self.canvas.coords(self.id)
   getNowPoint(pos[0],pos[1],pos[2],pos[3])
   #打印獲取的坐標(biāo)
 
   #如果最上面的縱軸坐標(biāo)在頂上,則往下移動(dòng)一個(gè)像素
   if pos[1] <=30 and self.y==-80:
    self.x = 80
    self.y=0
    print("pos1" + str(self.x) + ":pos1:" + str(self.y))
   #如果最下面的縱軸坐標(biāo)在底上,則向左移動(dòng)
   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))
   #寬度控制#
   #如果在左邊框了,那么向右邊移動(dòng)3像素
   elif pos[0] <30 and self.x== -80:
    self.x = 0
    self.y= -80
    print("pos0" + str(self.x) + ":pos0:" + str(self.y))
   #如果到右邊框了,左移動(dòng)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)
   # 獲取某個(gè)對(duì)象在畫布的坐標(biāo),返回一個(gè)數(shù)組(兩個(gè)坐標(biāo),左上角的坐標(biāo)和右下角的兩個(gè)坐標(biāo))
   pos = self.canvas.coords(self.id)
   print(pos)
 
def getNowPoint(x1,y1,x2,y2):
 global num
 print("現(xiàn)在在")
 print(x1,y1,x2,y2)
 row=(x1-10)/80
 line=(y1-5)/80
 num=str(int(row))+str(int(line))
 print("點(diǎn)"+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')
 
 
 #創(chuàng)建畫布
 
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)
 
 
#創(chuàng)建對(duì)象
ball = Ball(canvas,'red')
drawPath()
#一直保持循環(huán)
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, "恭喜獲得耳機(jī)?。。?!")
     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

想要學(xué)習(xí)更多關(guān)于抽獎(jiǎng)功能的實(shí)現(xiàn),請參考此專題:抽獎(jiǎng)功能

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

鄂托克旗| 南京市| 浦县| 祁东县| 宾川县| 陇西县| 嵊泗县| 余江县| 新闻| 枣阳市| 五家渠市| 通州区| 望城县| 翼城县| 道真| 安西县| 洞口县| 龙川县| 灵台县| 定远县| 扶沟县| 岳池县| 澜沧| 桑日县| 彭泽县| 汶上县| 黄大仙区| 岳阳市| 西乡县| 郴州市| 纳雍县| 福安市| 阿坝| 淅川县| 辽阳市| 冷水江市| 尼木县| 漳浦县| 云和县| 桂平市| 开江县|