Python3实现的画图及加载图片动画效果示例
Python  /  管理员 发布于 7年前   266
本文实例讲述了Python3实现的画图及加载图片动画效果。分享给大家供大家参考,具体如下:
#__*__coding:utf-8__*__#python3import timefrom tkinter import *def moveImage(event):#图片logo.gif的移动要绑定的函数 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的事物,填充为bluetk=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')#创建三角形print (im);print (pg) #显示图片和三角形的IDcanvas.bind_all('<KeyPress-Up>',moveImage)#绑定方向键 upcanvas.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__*__#python3from tkinter import *import randomimport timeclass 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=-3class 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=20tk=Tk()tk.title('Game')tk.resizable(0,0)#使得窗口大小不可调整tk.wm_attributes('-topmost',1)#包含画布的窗口放在其他窗口的前面canvas=Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)#后面两个参数去掉边框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数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号