用python3寫一個小球轉動的抽獎小遊戲

最近老師在講 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 random
import time
from PIL import Image, ImageTk
#
#創建一個類,這個類含有兩個參數,一個是畫布,一個是球的顏色
#
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)
            #獲取某個對象在畫布的座標,返回一個數組(兩個座標,左上角的座標和右下角的兩個座標)
            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 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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章