烟花模型与代码

它是什么?

这个程序模拟了烟花的表演。火箭从视野底部开始,向天空射出,然后爆炸,释放出瀑布般的火花。

它是如何工作的?

每个火箭都由一只乌龟来代表,以初始的x和y速度向上发射。在天空的某一点,会发生爆炸,这由一系列乌龟的孵化来表示。每个孵化的乌龟都会继承原始火箭的速度,还会获得来自爆炸本身的速度。结果就是一个烟花表演的模拟。

如何使用它

SETUP根据所有滑块和开关指示的值设置模型。GO是一个一直执行模型的按钮。

FIREWORKS会创建0到滑块上指示的数字之间的随机数量的烟花。

FRAGMENTS确定在单个烟花爆炸后会有多少粒子碎片。

GRAVITY确定环境中的重力强度。较大的值会提供更大的重力加速度,意味着粒子会以更快的速度被迫落到地面上。较小的值则相反。

INIT-X-VEL将每个火箭的初始x速度设置为滑块指示的负值和正值之间的随机数。

INIT-Y-VEL将每个火箭的初始y速度设置为0到滑块指示的数字加十之间的随机数。这是为了确保烟花的初始y速度有不同范围。

FADE-AMOUNT确定爆炸后粒子消失的速度。

TRAILS允许用户打开或关闭粒子留下的尾迹。换句话说,如果TRAILS开关打开,那么乌龟会留下尾迹。如果关闭,它们就不会留下尾迹。

这个模型被构建成滑块和开关的所有变都会在执行过程中生效。因此,当GO按钮还按下时,你可以改变滑块和开关的值,你可以立即在视图中看到这些改变。

注意事项

尝试使用INIT-X-VEL和INIT-Y-VEL滑块。观察到当初始x速度为零时,火箭会直直地向上发射。当初始x速度增加时,注意到一些火箭在天空中左右弧线轨迹取决于初始x速度是负数还是正数。

观察到在固定的GRAVITY值下,初始y速度较小时,烟花的高度较低,而初始y速度较大时,高度较高。还要观察到每个火箭在高度等于或略低于其顶点处爆炸。

尝试的事情

观察当GRAVITY滑块设置为不同值时模型会发生什么。当GRAVITY设置为零时观察模型会发生什么。你能解释模型中烟花发生了什么吗?你能解释为什么会发生这种现象吗?这对重力的重要性有什么说法?现在将GRAVITY滑块设置为最大值。在这种情况下,烟花的行为有什么不同?你能得出关于重力和物体在空间中移动的关系的结论吗?

扩展模型

这个模型中表示的烟花只是一种基本类型。扩展这个模型的一个好方法是创建其他更复杂的烟花。一些可能有多次爆炸、多种颜色或特定形状的设计。

注意到这个模型是以二维视角描绘的烟花。然而,当我们看到真正的烟花时,它们似乎呈现出三维形式。尝试通过将其视角从2D转换为3D来扩展这个模型。

代码

breed [ rockets rocket ]
breed [ frags frag ]

globals [
  countdown       ; how many ticks to wait before a new salvo of fireworks
]

turtles-own [
  col             ; sets color of an explosion particle
  x-vel           ; x-velocity
  y-vel           ; y-velocity
]

rockets-own [
  terminal-y-vel  ; velocity at which rocket will explode
]

frags-own [
  dim             ; used for fading particles
]

to setup
  clear-all
  set-default-shape turtles "circle"
  reset-ticks
end

; This procedure executes the model. If there are no turtles,
; it will either initiate a new salvo of fireworks by calling
; INIT-ROCKETS or continue to count down if it hasn't reached 0.
; It then calls PROJECTILE-MOTION, which launches and explodes
; any currently existing fireworks.
to go
  if not any? turtles [
    ifelse countdown = 0 [
      init-rockets
      ; use a higher countdown to get a longer pause when trails are drawn
      set countdown ifelse-value trails? [ 30 ] [ 10 ]
    ] [
      ; count down before launching a new salvo
      set countdown countdown - 1
    ]
  ]
  ask turtles [ projectile-motion ]
  tick
end

; This procedure creates a random number of rockets according to the
; slider FIREWORKS and sets all the initial values for each firework.
to init-rockets
  clear-drawing
  create-rockets (random max-fireworks) + 1 [
    setxy random-xcor min-pycor
    set x-vel ((random-float (2 * initial-x-vel)) - (initial-x-vel))
    set y-vel ((random-float initial-y-vel) + initial-y-vel * 2)
    set col one-of base-colors
    set color (col + 2)
    set size 2
    set terminal-y-vel (random-float 4.0) ; at what speed does the rocket explode?
  ]
end

; This function simulates the actual free-fall motion of the turtles.
; If a turtle is a rocket it checks if it has slowed down enough to explode.
to projectile-motion ; turtle procedure
  set y-vel (y-vel - (gravity / 5))
  set heading (atan x-vel y-vel)
  let move-amount (sqrt ((x-vel ^ 2) + (y-vel ^ 2)))
  if not can-move? move-amount [ die ]
  fd (sqrt ((x-vel ^ 2) + (y-vel ^ 2)))

  ifelse (breed = rockets) [
    if (y-vel < terminal-y-vel) [
      explode
      die
    ]
  ] [
    fade
  ]
end

; This is where the explosion is created.
; EXPLODE calls hatch a number of times indicated by the slider FRAGMENTS.
to explode ; turtle procedure
  hatch-frags fragments [
    set dim 0
    rt random 360
    set size 1
    set x-vel (x-vel * .5 + dx + (random-float 2.0) - 1)
    set y-vel (y-vel * .3 + dy + (random-float 2.0) - 1)
    ifelse trails?
      [ pen-down ]
      [ pen-up ]
  ]
end

; This function changes the color of a frag.
; Each frag fades its color by an amount proportional to FADE-AMOUNT.
to fade ; frag procedure
  set dim dim - (fade-amount / 10)
  set color scale-color col dim -5 .5
  if (color < (col - 3.5)) [ die ]
end


; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

异数时空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值