python3實(shí)現(xiàn)小球轉(zhuǎn)動(dòng)抽獎(jiǎng)小游戲
最近老師在講 tkinter,所以我做了一個(gè)抽獎(jiǎng)小游戲。
一、效果圖
先上效果圖。紅色的小球會(huì)圍繞藍(lán)色小球做環(huán)形運(yùn)動(dòng)。我設(shè)置的四個(gè)角是獎(jiǎng)品,其余的都是再接再厲。
二、方法
基于tkinter中的button,text,PIL ,time.Canvas
drawPath():用于畫(huà)藍(lán)色的小球
Ball類(lèi) 初始化畫(huà)布、運(yùn)動(dòng)小球大小、運(yùn)動(dòng)的起點(diǎn)。
ball類(lèi)-》draw() 控制小球的運(yùn)動(dòng)。這里用到一個(gè)方法叫canvas.coords。這個(gè)方法可以獲取運(yùn)動(dòng)小球當(dāng)前在畫(huà)布上的坐標(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ì)象在畫(huà)布的坐標(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是圖片)
三、遇到的問(wèn)題
老師教的顯示canvas上的內(nèi)容要用mainloop(),所以一開(kāi)始不知道怎么讓小球動(dòng)起來(lái),最后查閱了很多資料發(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è)類(lèi),這個(gè)類(lèi)含有兩個(gè)參數(shù),一個(gè)是畫(huà)布,一個(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ì)象在畫(huà)布的坐標(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ì)象在畫(huà)布的坐標(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)建畫(huà)布 tk = Tk() tk.title("Game_ball") tk.resizable(0,0) tk.wm_attributes("-topmost",1) #bd=0,highlightthickness=0 畫(huà)布之外沒(méi)有邊框 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,"恭喜獲得鍵盤(pán)?。。?!") 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),請(qǐng)參考此專(zhuān)題:抽獎(jiǎng)功能
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 如何基于python實(shí)現(xiàn)年會(huì)抽獎(jiǎng)工具
- Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤(pán)抽獎(jiǎng)器的步驟詳解
- Python實(shí)現(xiàn)的企業(yè)粉絲抽獎(jiǎng)功能示例
- 詳解用python寫(xiě)一個(gè)抽獎(jiǎng)程序
- python實(shí)現(xiàn)抽獎(jiǎng)小程序
- python實(shí)現(xiàn)年會(huì)抽獎(jiǎng)程序
- python實(shí)現(xiàn)公司年會(huì)抽獎(jiǎng)程序
- python實(shí)現(xiàn)的簡(jiǎn)單抽獎(jiǎng)系統(tǒng)實(shí)例
- 編寫(xiě)python代碼實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)器
相關(guān)文章
Flask實(shí)現(xiàn)的接口響應(yīng)中存在中文時(shí)接口返回為unicode亂碼的解決方法
本文給大家分享了新版Flask實(shí)現(xiàn)的接口響應(yīng)中存在中文時(shí)接口返回為unicode亂碼的解決方法,文中通過(guò)代碼示例和圖文介紹的非常詳細(xì),如果有遇到相同問(wèn)題的朋友,可以參考閱讀本文2023-11-11用python寫(xiě)PDF轉(zhuǎn)換器的實(shí)現(xiàn)
這篇文章主要介紹了用python寫(xiě)PDF轉(zhuǎn)換器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10使用python連接Linux服務(wù)器發(fā)送指定命令的示例代碼
這篇文章主要介紹了使用python連接Linux服務(wù)器發(fā)送指定命令,首先安裝paramiko庫(kù),使用paramiko庫(kù)連接linux,使用paramiko庫(kù)上傳下載文件,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10python中通過(guò)selenium簡(jiǎn)單操作及元素定位知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于python中通過(guò)selenium簡(jiǎn)單操作及元素定位的知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下。2019-09-09Python實(shí)現(xiàn)執(zhí)行Shell命令并獲取輸出
這篇文章主要介紹了如何借助?os.system()?從?Python?腳本執(zhí)行?cmd?命令,以及如何借助?Python?中的?subprocess?模塊以更簡(jiǎn)單的方式從腳本執(zhí)行?cmd?命令,感興趣的小伙伴可以了解下2023-10-10