【数学之美】基于加速度计的粒子发射系统

2018-10-191.3万
AI 快速预览详细 收起
基于加速度计的粒子发射系统是一种通过加速度计的读数实时改变粒子发射点位置的技术。通过mPython库实现,可以解决粒子系统读数与板子方向不一致的问题。适用于STEM教育,激发孩子的创造力和逻辑思维。


数学之美第三贴

郞咸蒙老师的粒子发射系统

代码全名是

基于加速度计的读数实时改变发射点位置的粒子系统

哈哈,名字够长够严谨

视频欣赏一下


无论怎么转动

粒子发射源总在下面

源源不断发送出智慧的

小点点

这小点点散发出魔幻的光

吸引我们好好学习天天向上

【代码】




[mw_shl_code=python,true]'''
基于加速度计的读数实时改变发射点位置的粒子系统
感觉粒子系统的读数和板子的方向似乎有点问题
同时使用pixel和DispChar会导致显示巨卡
'''
from mpython import *
from time import sleep_ms
from random import randint
from collections import deque


class Ball():
    '''
    粒子类
    '''

    def __init__(self, x, y):
        self.x = x
        self.y = y

        self.vx = randint(-6, 6)
        self.vy = randint(-6, 6)

    def run(self):
        self.update()
        self.display()

    def update(self):

        self.x += self.vx
        self.y += self.vy

    def isDead(self):

        if self.x > 128 or self.x < 0 or self.y > 64 or self.y < 0:
            return True
        else:
            return False

    def display(self):
        display.pixel(self.x, self.y, 1)


class ParticleSystem():
    '''
    粒子系统类
    '''

    def __init__(self, x, y):
        self.particles = []
        self.x = x
        self.y = y
        for i in range(200):
            self.particles.append(Ball(self.x, self.y))

    def run(self):
        for p in self.particles:
            if p.isDead():
                p.x = self.x
                p.y = self.y
            p.run()


# 初始化粒子系统
ps = ParticleSystem(0, 0)

while True:

    sleep_ms(20)

    display.fill(0)

    x = accelerometer.get_y()
    y = accelerometer.get_x()
    ps.x = 128-int(x*64+64)
    ps.y = int(y*32+32)
    ps.run()
    # 把加速度计的读数

    display.show()[/mw_shl_code]

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(1)
许培享
许培享
实时运行效果好:victory:
- 没有更多了 -