使用pygame实现动量定理的小球碰撞演示动画

动量定理我们在高中的时候就已经接触过了,是十分重要的物理定理。其中的完全弹性碰撞(机械能守恒)是十分典型的例子,机械能守恒和动量定理两个公式就可以推出小球碰撞之后的速度,依据这个公式,我们就可以完成我们的动画演示了。
m 1 v 1 + m 2 v 2 = m 1 v 1 ′ + m 2 v 2 ′ m_1v_1+m_2v_2=m_1v_1'+m_2v_2' m1​v1​+m2​v2​=m1​v1′​+m2​v2′​

1 2 m 1 v 1 2 + 1 2 m 2 v 2 2 = 1 2 m 1 v 1 ′ 2 + 1 2 m 2 v 2 ′ 2 \frac{1}{2}m_1v_1^2+\frac{1}{2}m_2v_2^2=\frac{1}{2}m_1v_1'^2+\frac{1}{2}m_2v_2'^2 21​m1​v12​+21​m2​v22​=21​m1​v1′2​+21​m2​v2′2​
根据这两个式子,可以得出碰撞之后的 v 1 ’ v_1’ v1​’和 v 2 ′ v_2' v2′​的速度:
v 1 ′ = 2 m 2 m 1 + m 2 v 2 + m 1 − m 2 m 1 + m 2 v 1 v_1'=\frac{2m_2}{m_1+m_2}v_2+\frac{m_1-m_2}{m_1+m_2}v_1 v1′​=m1​+m2​2m2​​v2​+m1​+m2​m1​−m2​​v1​

v 2 ′ = 2 m 1 m 1 + m 2 v 1 + m 2 − m 1 m 1 + m 2 v 2 v_2'=\frac{2m_1}{m_1+m_2}v_1+\frac{m_2-m_1}{m_1+m_2}v_2 v2′​=m1​+m2​2m1​​v1​+m1​+m2​m2​−m1​​v2​

然后就可以写代码了

源代码

# color.py
# 需要用到的颜色
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255,255,0)
UNKNOWN = (0, 255,255)
import pygame
from pygame.locals import *
from color import *# 精灵类
class ball(pygame.sprite.Sprite):def __init__(self, mass, position, velocity, color): # position 为左下角的坐标self.mass = massself.image = pygame.Surface((40, 40))self.rect = self.image.get_rect()# pygame.draw.rect(self.image, BLACK, self.rect, 1)self.image.fill(WHITE)pygame.draw.circle(self.image, color, (20, 20), 20)self.rect.bottomleft = positionself.velocity = velocity
# 主函数
import pygame, sys
from pygame.locals import *
from color import *
from ball import balldef main():pygame.init()screen = pygame.display.set_mode((800, 600))pygame.display.set_caption('动量定理动画')FPSCLOCK = pygame.time.Clock()floor1 = 200floor2 = 400floor3 = 600# 初始化小球ball1 = ball(10, (0, floor1), 10,RED)ball2 = ball(20, (760, floor1), -5,BLACK)ball3 = ball(10, (0, floor2), 10,GREEN)ball4 = ball(10, (760, floor2), -10,BLUE)ball5 = ball(1, (0, floor3), 10, YELLOW)ball6 = ball(1000000000000000000000, (400, floor3), 0, UNKNOWN)# 绘制两个小球screen.fill(WHITE)pygame.draw.line(screen, BLACK,(0, floor1), (800, floor1), 1)pygame.draw.line(screen, BLACK,(0, floor2), (800, floor2), 1)pygame.draw.line(screen, BLACK,(0, floor3), (800, floor3), 1)screen.blit(ball1.image, ball1.rect)screen.blit(ball2.image, ball2.rect)screen.blit(ball3.image, ball3.rect)screen.blit(ball4.image, ball4.rect)screen.blit(ball5.image, ball5.rect)screen.blit(ball6.image, ball6.rect)while True:for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()if collision_check(ball1.rect, ball2.rect):v1 = ball1.velocityv2 = ball2.velocitym1 = ball1.massm2 = ball2.massball1.velocity = int(2*m2*v2/(m1+m2)+((m1-m2)/(m1+m2))*v1)ball2.velocity = int(2*m1*v1/(m1+m2)+((m2-m1)/(m1+m2))*v2)if ball3.rect.right > ball4.rect.left and ball3.rect.right - ball3.velocity <= ball4.rect.left - ball4.velocity:v1 = ball3.velocityv2 = ball4.velocitym1 = ball3.massm2 = ball4.massball3.velocity = int(2*m2*v2/(m1+m2)+((m1-m2)/(m1+m2))*v1)ball4.velocity = int(2*m1*v1/(m1+m2)+((m2-m1)/(m1+m2))*v2)if ball5.rect.right > ball6.rect.left and ball5.rect.right - ball5.velocity <= ball6.rect.left - ball6.velocity:v1 = ball5.velocityv2 = ball6.velocitym1 = ball5.massm2 = ball6.massprint(ball5.velocity)ball5.velocity = int(2*m2*v2/(m1+m2)+((m1-m2)/(m1+m2))*v1)ball6.velocity = int(2*m1*v1/(m1+m2)+((m2-m1)/(m1+m2))*v2)if ball1.rect.left < 0 or ball1.rect.right > 800:ball1.velocity = -ball1.velocityif (ball1.rect.left < 0 and ball1.velocity < 0) or (ball1.rect.right > 800 and ball1.velocity > 0):ball1.velocity = -ball1.velocityif ball2.rect.left < 0 or ball2.rect.right > 800:ball2.velocity = -ball2.velocityif (ball2.rect.left < 0 and ball2.velocity < 0) or (ball2.rect.right > 800 and ball2.velocity > 0):ball2.velocity = -ball2.velocityif ball3.rect.left < 0 or ball3.rect.right > 800:ball3.velocity = -ball3.velocityif (ball3.rect.left < 0 and ball3.velocity < 0) or (ball3.rect.right > 800 and ball3.velocity > 0):ball3.velocity = -ball3.velocityif ball4.rect.left < 0 or ball4.rect.right > 800:ball4.velocity = -ball4.velocityif (ball4.rect.left < 0 and ball4.velocity < 0) or (ball4.rect.right > 800 and ball4.velocity > 0):ball4.velocity = -ball4.velocityif ball5.rect.left < 0 or ball5.rect.right > 800:ball5.velocity = -ball5.velocityif (ball5.rect.left < 0 and ball5.velocity < 0) or (ball5.rect.right > 800 and ball5.velocity > 0):ball5.velocity = -ball5.velocityif ball6.rect.left < 0 or ball6.rect.right > 800:ball6.velocity = -ball6.velocityif (ball6.rect.left < 0 and ball6.velocity < 0) or (ball6.rect.right > 800 and ball6.velocity > 0):ball6.velocity = -ball6.velocityball1.rect.left += ball1.velocityball2.rect.left += ball2.velocityball3.rect.left += ball3.velocityball4.rect.left += ball4.velocityball5.rect.left += ball5.velocityball6.rect.left += ball6.velocityscreen.fill(WHITE)pygame.draw.line(screen, BLACK,(0, floor1), (800, floor1), 1)pygame.draw.line(screen, BLACK,(0, floor2), (800, floor2), 1)pygame.draw.line(screen, BLACK,(0, floor3), (800, floor3), 1)screen.blit(ball1.image, ball1.rect)screen.blit(ball2.image, ball2.rect)screen.blit(ball3.image, ball3.rect)screen.blit(ball4.image, ball4.rect)screen.blit(ball5.image, ball5.rect)screen.blit(ball6.image, ball6.rect)pygame.display.update()FPSCLOCK.tick(60)if __name__ == '__main__':main()

效果

pygame动画演示小球碰撞相关推荐

  1. Simscape基础教程之实例(一)——使用Simscape三维物理仿真自由落体小球碰撞平面

    一. 简介 本实例使用MATLAB/Simulink里面的simscape,实现自由落体小球碰撞平面的三维物理仿真,可用于新手入门simscape的参考示例. 二. 准备工作 需要安装的工具箱 (1) ...

  2. matlab模拟台球比赛,Matlab 台球模拟程序 动画演示

    发布时间: Oct 21, 2012 更新时间: Oct 21, 2012 总字数:1669 阅读时间:4m 作者: 谢先斌 Matlab 台球模拟程序 动画演示 代码 axis([-1.6,12.6 ...

  3. 绘制canvas彩色泡泡小球碰撞

    globalCompositeOperation带来的不一样的烟花 一.知识点 1.动画: setInterval(             function(){                 d ...

  4. 原创数据结构算法Flash动画演示课件-Action Script(AS)脚本实现

    2001年,和大学同学一起完成的毕业设计作品.cooling&bobo. 学习数据结构与算法,请访问:数据结构学习网站 http://xu-laoshi.cn/shujujiegou/ .&q ...

  5. raft原理的动画演示

    过去, Paxos一直是分布式协议的标准,但是Paxos难于理解,更难以实现,Google的分布式锁系统Chubby作为Paxos实现曾经遭遇到很多坑. 来自Stanford的新的分布式协议研究称为R ...

  6. Visaul Studio 常用快捷键的动画演示

    从本篇文章开始,我将会陆续介绍提高 VS 开发效率的文章,欢迎大家补充~ 在进行代码开发的时候,我们往往会频繁的使用键盘.鼠标进行协作,但是切换使用两种工具会影响到我们的开发速度,如果所有的操作都可以 ...

  7. raft算法动画演示

    简言 1. 分布式一致性算法,知名的有Paxos,Multi Paxos,Raft 3种算法,其中Raft算法最容易理解 2. 关于Raft算法的原理,可以参考这篇博客:https://blog.cs ...

  8. 动画演示男性结扎手术 | 今日趣图

    全世界只有3.14 % 的人关注了 青少年数学之旅 流鼻涕了怎么办? @医学教育徐琦 招聘程序员啦 知识@萌死大丧失:图@熊本科技 把下列句子补充完整 图@三好学生李宇 幼年的大象喝水并不会使用鼻子 ...

  9. 工程打包是什么意思_太生动形象了!500个建筑施工3D动画演示,施工工艺一目了然,零基础工程人也能看懂...

    在建筑行业干了这么多年,老杨深知其中的艰辛.刚毕业的时候去了中建,那时候带我的老师傅很忙,天天都让看书,看得头晕眼花,不懂的地方也不好意思去问,后来还是一个同事给了这500个建筑施工3D动画演示,涵盖 ...

最新文章

  1. ev3编码软件linux,利用官方固件搭建EV3自制系统
  2. while循环里嵌套一个if_if-else嵌套太深?教你一个新手都能掌握的设计模式搞定!...
  3. Android Logcat 报错:Could not create the view: For input string:
  4. Linux内核分析 - 网络[十一]:ICMP模块
  5. Fiddler过滤css、js、图片等静态文件
  6. 恩恩,庆祝一下,我也开博了。
  7. 如何保证缓存与数据库双写时的数据一致性?
  8. 安鑫 十年资产翻十倍 普通人是怎么做到的
  9. 方维众筹网站源码V1.71 PC+WAP+商业版
  10. Android新浪微博开发(一)授权认证
  11. 史上最拉跨的导线平差程序( by C#)
  12. Pytorch中的forward的理解
  13. 计算机cast函数是什么意思,CAST()函数
  14. 嵌入式系统硬件设计与实践(学习方法)
  15. go 字符串分割数组
  16. 最全的中国大陆运营商所有号段!
  17. 硬盘的文件格式变为RAW格式
  18. linux系统实训总结报告,linux实训心得体会范文
  19. 知识付费时代对内容为王的反思
  20. Seeeduino XIAO入门详解

热门文章

  1. mbio期刊拒稿率_PLoS Pathogens
  2. mysql的binlog开启方式,查看方式.三种binlog模式介绍.以及使用binlog恢复数据.删除binlog
  3. 八字-十天干、十二地支、六十甲子
  4. 【iCheck基本用法的使用】
  5. 常见的麦克风供电方式总结(驻极体电容器麦克风)
  6. SVN上传、更新、添加、删除文件、版本回退
  7. java单例模式几种常见实现方式
  8. Docker 使用快速入门
  9. 利用smtp协议实现命令行发送邮件
  10. 如何使用FFmpeg的解码器