Project1

标题: 随手写了一个粒子特效 [打印本页]

作者: 有丘直方    时间: 2019-2-8 21:43
标题: 随手写了一个粒子特效



RUBY 代码复制
  1. class Particle < Sprite
  2.   BITMAP = Bitmap.new(64, 64)
  3.   64.times do |x|
  4.     64.times do |y|
  5.       if (x - 32) ** 2 + (y - 32) ** 2 <= 128
  6.         BITMAP.set_pixel(x, y, Color.new(255, 0, 0))
  7.       end
  8.     end
  9.   end
  10.   32.times { BITMAP.blur }
  11.  
  12.   VIEWPORT = Viewport.new(-128, -128, 1280, 1024)
  13.  
  14.   @@z = 1 << 30
  15.  
  16.   def initialize(x, y)
  17.     super(VIEWPORT)
  18.     self.bitmap = BITMAP.clone
  19.     self.ox = 8
  20.     self.oy = 8
  21.     self.x = x + 128
  22.     self.y = y + 128
  23.     self.opacity = 0
  24.     self.z = (@@z -= rand(2))
  25.     @frame_count = 0
  26.     @velocity = rand / 2
  27.     @acceleraction = rand / 2000
  28.     @float_y = self.y
  29.     @fade_in_rate = rand(10) + 15
  30.     @fade_out_rate = rand(5) + 5
  31.     @zoom_rate = rand / 400
  32.     @life = rand(50) + 150
  33.     @tone_change_rate = rand / 2 + 0.5
  34.     @float_tone = 0
  35.   end
  36.  
  37.   def update
  38.     @float_y -= (@velocity += @acceleraction)
  39.     self.y = @float_y
  40.     self.opacity += @fade_in_rate if opacity < 255 && @frame_count < @life
  41.     self.opacity -= @fade_out_rate if @frame_count >= @life
  42.     self.zoom_x += @zoom_rate
  43.     self.zoom_y += @zoom_rate
  44.     bitmap.hue_change(3) if @frame_count % 10 == 0
  45.     @float_tone += @tone_change_rate
  46.     tone.red = @float_tone
  47.     tone.green = @float_tone
  48.     tone.blue = @float_tone
  49.     if opacity <= 0
  50.       bitmap.dispose
  51.       dispose
  52.     end
  53.     @frame_count += 1
  54.   end
  55. end
  56.  
  57. class Scene
  58.   def initialize
  59.     Graphics.resize_screen(1024, 768)
  60.     @x = Graphics.width / 2
  61.     @y = Graphics.height / 2
  62.     @particles = []
  63.   end
  64.  
  65.   def set_position(x, y)
  66.     @x, @y = x, y
  67.   end
  68.  
  69.   def update
  70.     update_basic
  71.     update_position
  72.     update_particles
  73.   end
  74.  
  75.   def update_basic
  76.     Mouse.update
  77.     Graphics.update
  78.   end
  79.  
  80.   def update_particles
  81.     @particles.each(&:update)
  82.     @particles.delete_if(&:disposed?)
  83.     360.times do |angle|
  84.       if rand(120) == 0
  85.         radian = angle * Math::PI / 180
  86.         radius = rand(4) + 62
  87.         x = @x + Math.cos(radian) * radius
  88.         y = @y + Math.sin(radian) * radius
  89.         @particles.push(Particle.new(x, y))
  90.       end
  91.     end
  92.   end
  93.  
  94.   def update_position
  95.     set_position(Mouse.x, Mouse.y) if Mouse.press?
  96.   end
  97. end
  98.  
  99. scene = Scene.new
  100. loop { scene.update }

写得太差劲了,看上去没什么感觉
使用RGD
作者: apolinz    时间: 2019-2-9 17:09
谢谢大佬!拿去用顺便膜一下大佬
作者: myownroc    时间: 2019-2-9 18:32
用RGSS实现真的不会卡吗- -




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1