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類 初始化畫(huà)布、運(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)前在畫(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è)類,這個(gè)類含有兩個(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)參考此專題:抽獎(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)文章
python實(shí)現(xiàn)的jpg格式圖片修復(fù)代碼
這篇文章主要介紹了python實(shí)現(xiàn)的jpg格式圖片修復(fù)代碼,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
Python機(jī)器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor)實(shí)例詳解
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor),結(jié)合實(shí)例形式分析了k-近鄰算法的原理、操作步驟、相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-06-06
Tensorflow:轉(zhuǎn)置函數(shù) transpose的使用詳解
今天小編就為大家分享一篇Tensorflow:轉(zhuǎn)置函數(shù) transpose的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python3中獲取文件當(dāng)前絕對(duì)路徑的兩種方法
下面小編就為大家分享一篇python3中獲取文件當(dāng)前絕對(duì)路徑的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
用python修改excel表某一列內(nèi)容的操作方法
這篇文章主要介紹了用python修改excel表某一列內(nèi)容的操作代碼,在實(shí)現(xiàn)過(guò)程中用到openpyxl這個(gè)庫(kù),要生成隨機(jī)數(shù)就要有random這個(gè)庫(kù),具體代碼跟隨小編一起看看吧2021-06-06
Python簡(jiǎn)單實(shí)現(xiàn)查找一個(gè)字符串中最長(zhǎng)不重復(fù)子串的方法
這篇文章主要介紹了Python簡(jiǎn)單實(shí)現(xiàn)查找一個(gè)字符串中最長(zhǎng)不重復(fù)子串的方法,涉及Python針對(duì)字符串的簡(jiǎn)單遍歷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
出現(xiàn)module 'queue' has no attrib
這篇文章主要介紹了出現(xiàn)module 'queue' has no attribute 'Queue'問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

