python3實現(xiàn)小球轉(zhuǎn)動抽獎小游戲
最近老師在講 tkinter,所以我做了一個抽獎小游戲。
一、效果圖
先上效果圖。紅色的小球會圍繞藍色小球做環(huán)形運動。我設(shè)置的四個角是獎品,其余的都是再接再厲。
二、方法
基于tkinter中的button,text,PIL ,time.Canvas
drawPath():用于畫藍色的小球
Ball類 初始化畫布、運動小球大小、運動的起點。
ball類-》draw() 控制小球的運動。這里用到一個方法叫canvas.coords。這個方法可以獲取運動小球當(dāng)前在畫布上的坐標。并返回一個數(shù)組。比如 pos=self.canvas.coords 。左邊:pos[0],右邊pos[2],上邊:pos[1],下邊:pos[3].用if和pos 可以控制小球的上下左右運動。
self.canvas.move(self.id,self.x,self.y) #獲取某個對象在畫布的坐標,返回一個數(shù)組(兩個坐標,左上角的坐標和右下角的兩個坐標) 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()當(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(),所以一開始不知道怎么讓小球動起來,最后查閱了很多資料發(fā)現(xiàn)。其實不用mainloop也行。可以使用tk.update() 刷新tk上的內(nèi)容。所以這里我們要用一個while讓小球每動一次窗體就刷新一次。time.sleep()控制小球運動速度。
四、代碼
from tkinter import * import random import time from PIL import Image, ImageTk # #創(chuàng)建一個類,這個類含有兩個參數(shù),一個是畫布,一個是球的顏色 # 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) #獲取某個對象在畫布的坐標,返回一個數(shù)組(兩個坐標,左上角的坐標和右下角的兩個坐標) 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) # 獲取某個對象在畫布的坐標,返回一個數(shù)組(兩個坐標,左上角的坐標和右下角的兩個坐標) 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("點"+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)建對象 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, "恭喜獲得耳機?。。?!") 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)于抽獎功能的實現(xiàn),請參考此專題:抽獎功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flask實現(xiàn)的接口響應(yīng)中存在中文時接口返回為unicode亂碼的解決方法
本文給大家分享了新版Flask實現(xiàn)的接口響應(yīng)中存在中文時接口返回為unicode亂碼的解決方法,文中通過代碼示例和圖文介紹的非常詳細,如果有遇到相同問題的朋友,可以參考閱讀本文2023-11-11用python寫PDF轉(zhuǎn)換器的實現(xiàn)
這篇文章主要介紹了用python寫PDF轉(zhuǎn)換器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10使用python連接Linux服務(wù)器發(fā)送指定命令的示例代碼
這篇文章主要介紹了使用python連接Linux服務(wù)器發(fā)送指定命令,首先安裝paramiko庫,使用paramiko庫連接linux,使用paramiko庫上傳下載文件,結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-10-10python中通過selenium簡單操作及元素定位知識點總結(jié)
在本篇文章里小編給大家整理的是關(guān)于python中通過selenium簡單操作及元素定位的知識點,有需要的朋友們可以學(xué)習(xí)下。2019-09-09Python實現(xiàn)執(zhí)行Shell命令并獲取輸出
這篇文章主要介紹了如何借助?os.system()?從?Python?腳本執(zhí)行?cmd?命令,以及如何借助?Python?中的?subprocess?模塊以更簡單的方式從腳本執(zhí)行?cmd?命令,感興趣的小伙伴可以了解下2023-10-10