Python中tkinter如何实现图片移动碰撞动画效果
更新:HHH   时间:2023-1-7


这篇文章给大家分享的是有关Python中tkinter如何实现图片移动碰撞动画效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

先来看看运行效果:

具体代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
try:
 from tkinter import *
except ImportError: #Python 2.x
 PythonVersion = 2
 from Tkinter import *
 from tkFont import Font
 from ttk import *
 from tkMessageBox import *
 import tkFileDialog
else: #Python 3.x
 PythonVersion = 3
 from tkinter.font import Font
 from tkinter.ttk import *
 from tkinter.messagebox import *
# 配置
# 要打开的图像
image1 = "open.gif"
# 初始坐标
x0 = 50.0
y0 = 50.0
# 列表将包含所有的x和y坐标.到目前为止,他们只包含初始坐标
x = [x0]
y = [y0]
# 每次移动的速度或距离
vx = 1.0# x 速度
vy = 0.5# y 速度
# 边界,这里要考虑到图片的大小,要预留一半的长和宽
x_min = 46.0
y_min = 46.0
x_max = 754.0
y_max = 554.0
# 图片间隔时间,要动画效果,此处设为每秒40帧
sleep_time = 0.025
# 运行步数
range_min = 1
range_max = 2000
# 创建500次的x和y坐标
for t in range(range_min, range_max):
 # 新坐标等于旧坐标加每次移动的距离
 new_x = x[t - 1] + vx
 new_y = y[t - 1] + vy
 # 如果已经越过边界,反转方向
 if new_x >= x_max or new_x <= x_min:
  vx = vx * -1.0
 if new_y >= y_max or new_y <= y_min:
  vy = vy * -1.0
 # 添加新的值到列表
 x.append(new_x)
 y.append(new_y)
# 开始使用tk绘图
root = Tk()
root.title("天达云 tkinter动画测试") #在这里修改窗口的标题
canvas = Canvas(width=800, height=600, bg='white')
canvas.pack()
photo1 = PhotoImage(file=image1)
width2 = photo1.width()
height1 = photo1.height()
image_x = (width2) / 2.0
image_y = (height1) / 2.0
# 每次的移动
for t in range(range_min, range_max):
 canvas.create_image(x[t], y[t], image=photo1, tag="pic")
 canvas.update()
 # 暂停0.05妙,然后删除图像
 time.sleep(sleep_time)
 canvas.delete("pic")
root.mainloop()

感谢各位的阅读!关于“Python中tkinter如何实现图片移动碰撞动画效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

返回开发技术教程...