Python3實現(xiàn)的畫圖及加載圖片動畫效果示例
本文實例講述了Python3實現(xiàn)的畫圖及加載圖片動畫效果。分享給大家供大家參考,具體如下:
#__*__coding:utf-8__*__
#python3
import time
from tkinter import *
def moveImage(event):#圖片logo.gif的移動要綁定的函數(shù)
if event.keysym=='Up':
canvas.move(1,0,-3)#移動ID為1的事物,使得橫坐標加0,縱坐標減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)
運行結果:

摁上下左右鍵后可以移動圖片
擋板游戲例子
#__*__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#把擋板傳遞進來
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)#隨機化列表
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)#返回相應ID代表的圖形的當前坐標(左上角和右上角坐標)
#使得小球不會超出窗口
pad=self.canvas.coords(self.paddle.id)#獲取擋板的坐標
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)#使得窗口大小不可調整
tk.wm_attributes('-topmost',1)#包含畫布的窗口放在其他窗口的前面
canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)#后面兩個參數(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)
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python sklearn中的.fit與.predict的用法說明
這篇文章主要介紹了Python sklearn中的.fit與.predict的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
在django中form的label和verbose name的區(qū)別說明
這篇文章主要介紹了在django中form的label和verbose name的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python基于內置函數(shù)type創(chuàng)建新類型
這篇文章主要介紹了Python基于內置函數(shù)type創(chuàng)建新類型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
Pytorch搭建簡單的卷積神經網絡(CNN)實現(xiàn)MNIST數(shù)據(jù)集分類任務
這篇文章主要介紹了Pytorch搭建簡單的卷積神經網絡(CNN)實現(xiàn)MNIST數(shù)據(jù)集分類任務,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

