Python3實(shí)現(xiàn)的畫圖及加載圖片動(dòng)畫效果示例
本文實(shí)例講述了Python3實(shí)現(xiàn)的畫圖及加載圖片動(dòng)畫效果。分享給大家供大家參考,具體如下:
#__*__coding:utf-8__*__
#python3
import time
from tkinter import *
def moveImage(event):#圖片logo.gif的移動(dòng)要綁定的函數(shù)
if event.keysym=='Up':
canvas.move(1,0,-3)#移動(dòng)ID為1的事物,使得橫坐標(biāo)加0,縱坐標(biāo)減3
elif event.keysym=='Down':
canvas.move(1,0,+3)
elif event.keysym=='Left':
canvas.move(1,-3,0)
elif event.keysym=='Right':
canvas.move(1,3,0)
tk.update()
time.sleep(0.05)
def changeColor(event):
if event.keysym=='Up':
canvas.itemconfig(pg,fill='blue')#填充ID為pg的事物,填充為blue
tk=Tk()#窗口
canvas=Canvas(tk,width=400,height=400)#畫布
canvas.pack()#顯示出來
myImage=PhotoImage(file='C:\\Users\\lai\\Desktop\\logo.gif')#圖片格式必須為gif格式
im=canvas.create_image(0,0,anchor=NW,image=myImage)#加載圖片
pg=canvas.create_polygon(10,10,10,60,50,35,fill='red')#創(chuàng)建三角形
print (im);print (pg) #顯示圖片和三角形的ID
canvas.bind_all('<KeyPress-Up>',moveImage)#綁定方向鍵 up
canvas.bind_all('<KeyPress-Down>',moveImage)
canvas.bind_all('<KeyPress-Left>',moveImage)
canvas.bind_all('<KeyPress-Right>',moveImage)
#canvas.bind_all('<KeyPress-Up>',changeColor)
運(yùn)行結(jié)果:

摁上下左右鍵后可以移動(dòng)圖片
擋板游戲例子
#__*__coding:utf-8__*__
#python3
from tkinter import *
import random
import time
class Ball:#小球的類
def __init__(self,canvas,paddle,color):
self.canvas=canvas#傳遞畫布值
self.paddle=paddle#把擋板傳遞進(jìn)來
self.id=canvas.create_oval(10,10,25,25,fill=color)#畫橢圓并且保存其ID
self.canvas.move(self.id,245,100)
start=[-3,-2,-1,1,2,3]
random.shuffle(start)#隨機(jī)化列表
self.x=start[0]
self.y=-3
self.canvas_heigh=self.canvas.winfo_height()#獲取窗口高度并保存
self.canvas_width=self.canvas.winfo_width()
def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id)#返回相應(yīng)ID代表的圖形的當(dāng)前坐標(biāo)(左上角和右上角坐標(biāo))
#使得小球不會(huì)超出窗口
pad=self.canvas.coords(self.paddle.id)#獲取擋板的坐標(biāo)
if pos[1]<=0 :
self.y=3
if pos[3]>=self.canvas_heigh or(pos[3]>=pad[1] and pos[2]>=pad[0] and pos[2]<=pad[2]):
self.y=-3
if pos[0]<=0:
self.x=3
if pos[2]>=self.canvas_width:
self.x=-3
class Paddle:#擋板的類
def __init__(self,canvas,color):
self.canvas=canvas
self.color=color
self.id=canvas.create_rectangle(0,0,100,10,fill=color)
self.canvas.move(self.id,200,300)
self.canvas_width=self.canvas.winfo_width()
self.l=0
self.r=0
def draw(self):
pos=self.canvas.coords(self.id)
if pos[0]<=0:
self.l=0
if pos[2]>=self.canvas_width:
self.r=0
def turn_left(self,event):
self.canvas.move(self.id,self.l,0)
self.l=-20
def turn_right(self,event):
self.canvas.move(self.id,self.r,0)
self.r=20
tk=Tk()
tk.title('Game')
tk.resizable(0,0)#使得窗口大小不可調(diào)整
tk.wm_attributes('-topmost',1)#包含畫布的窗口放在其他窗口的前面
canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)#后面兩個(gè)參數(shù)去掉邊框
canvas.pack()
tk.update()
paddle=Paddle(canvas,'blue')
ball=Ball(canvas,paddle,'red')
canvas.bind_all('<KeyPress-Left>',paddle.turn_left)#綁定方向鍵
canvas.bind_all('<KeyPress-Right>',paddle.turn_right)
while 1:
ball.draw()
paddle.draw()
tk.update_idletasks()#快速重畫屏幕
tk.update()
time.sleep(0.01)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python sklearn中的.fit與.predict的用法說明
這篇文章主要介紹了Python sklearn中的.fit與.predict的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
wxPython實(shí)現(xiàn)帶顏色的進(jìn)度條
這篇文章主要介紹了wxPython實(shí)現(xiàn)帶顏色的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
在django中form的label和verbose name的區(qū)別說明
這篇文章主要介紹了在django中form的label和verbose name的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python基于內(nèi)置函數(shù)type創(chuàng)建新類型
這篇文章主要介紹了Python基于內(nèi)置函數(shù)type創(chuàng)建新類型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Pytorch搭建簡(jiǎn)單的卷積神經(jīng)網(wǎng)絡(luò)(CNN)實(shí)現(xiàn)MNIST數(shù)據(jù)集分類任務(wù)
這篇文章主要介紹了Pytorch搭建簡(jiǎn)單的卷積神經(jīng)網(wǎng)絡(luò)(CNN)實(shí)現(xiàn)MNIST數(shù)據(jù)集分類任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Python玩轉(zhuǎn)Excel的讀寫改實(shí)例
今天小編就為大家分享一篇關(guān)于Python玩轉(zhuǎn)Excel的讀寫改實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02

