温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python3怎么实现加载图片动画效果

发布时间:2021-05-07 12:16:50 来源:亿速云 阅读:378 作者:小新 栏目:开发技术

小编给大家分享一下Python3怎么实现加载图片动画效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

python是什么意思

Python是一种跨平台的、具有解释性、编译性、互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。

Python3实现的画图及加载图片动画效果,具体如下:

#__*__coding:utf-8__*__
#python3
import time
from 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的事物,填充为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')#创建三角形
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)#后面两个参数去掉边框
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)

以上是“Python3怎么实现加载图片动画效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI