Initial commit
Вместо змея пока что шарики, но и то хорошо
This commit is contained in:
commit
9124fe464b
20
emitter.rb
Normal file
20
emitter.rb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# An emitter simply emits particles at a regular interval. In this case, every
|
||||||
|
# time spawn is called, a new particle is generated with a random angle of movement and speed.
|
||||||
|
class Emitter
|
||||||
|
def initialize(particles)
|
||||||
|
@particles = particles
|
||||||
|
end
|
||||||
|
|
||||||
|
def spawn (x, y)
|
||||||
|
@particles.create(x, y, rand * 360, (rand * 3)+3)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
@particles.draw
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@particles.update
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
BIN
images/background.png
Normal file
BIN
images/background.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 96 KiB |
68
particle.rb
Normal file
68
particle.rb
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
require './python.rb'
|
||||||
|
|
||||||
|
# One particle is basically a sprite, that moves itself by speed every tick along an angle.
|
||||||
|
# It also has a concept of decay, in how quickly the particle dies. In this case, the draw
|
||||||
|
# command takes into account decay to shrink the particles and make them more translucent.
|
||||||
|
class Particle
|
||||||
|
def initialize(window, x, y, ang, speed, decay)
|
||||||
|
@x = x
|
||||||
|
@y = y
|
||||||
|
@angle = 90-ang
|
||||||
|
@speed = speed
|
||||||
|
@d_x = offset_x(@angle, @speed)
|
||||||
|
@d_y = offset_y(@angle, @speed)
|
||||||
|
@life = 1
|
||||||
|
@decay = decay
|
||||||
|
|
||||||
|
radius = (rand * 10).floor
|
||||||
|
@img = Gosu::Image.new(window, Python.new(radius), false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@x = @x + @d_x
|
||||||
|
@y = @y + @d_y
|
||||||
|
@life = @life - @decay
|
||||||
|
@angle += (@speed / 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
def dead?
|
||||||
|
@life <= 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_layer(layer, color, x = nil, y = nil)
|
||||||
|
@img.draw_rot(x || @x, y || @y, layer, @angle, 0.5, 0.5, @life, @life, color, :default)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
return if dead?
|
||||||
|
draw_layer(ZOrder::Shadow, Gosu::Color.new((@life * 128).floor,0,0,0), @x+2, @y+2)
|
||||||
|
draw_layer(ZOrder::Shape, Gosu::Color.new((@life * 255).floor, 255,255,0))
|
||||||
|
draw_layer(ZOrder::Highlight, Gosu::Color.new((@life * 20).floor,255,255,255))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# This class keeps track of our moving particles. It loads in a few white png files, and removes
|
||||||
|
# dead particles during every tick. They could probably be garbage collected less frequently.
|
||||||
|
class Particles
|
||||||
|
|
||||||
|
def initialize(window)
|
||||||
|
@particles = []
|
||||||
|
@window = window
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
@particles.each {|p| p.update}
|
||||||
|
@particles.delete_if {|p| p.dead?}
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
@particles.each {|p| p.draw}
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(x, y, angle, speed)
|
||||||
|
@particles << Particle.new(@window, x, y, angle, speed, 0.01)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
49
particles.rb
Executable file
49
particles.rb
Executable file
|
@ -0,0 +1,49 @@
|
||||||
|
#!/usr/bin/ruby
|
||||||
|
require 'gosu'
|
||||||
|
include Gosu
|
||||||
|
require './particle.rb'
|
||||||
|
require './emitter.rb'
|
||||||
|
|
||||||
|
# Our window size
|
||||||
|
module Dimensions
|
||||||
|
Width, Height = 1280, 720
|
||||||
|
end
|
||||||
|
|
||||||
|
# Our layers for drawing.
|
||||||
|
module ZOrder
|
||||||
|
Background, Shadow, Shape, Highlight = *0..3
|
||||||
|
end
|
||||||
|
|
||||||
|
class GameWindow < Gosu::Window
|
||||||
|
def initialize
|
||||||
|
super(Dimensions::Width, Dimensions::Height, false)
|
||||||
|
self.caption = "Particle Effects"
|
||||||
|
|
||||||
|
@background_image = Gosu::Image.new(self, "images/background.png", true)
|
||||||
|
particles = Particles.new(self)
|
||||||
|
@emitter = Emitter.new(particles)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Called every tick by gosu to update object positions
|
||||||
|
def update
|
||||||
|
@emitter.update
|
||||||
|
end
|
||||||
|
|
||||||
|
# Called every tick by gosu to draw things on the screen
|
||||||
|
def draw
|
||||||
|
@background_image.draw(0, 0, ZOrder::Background)
|
||||||
|
@emitter.draw
|
||||||
|
end
|
||||||
|
|
||||||
|
# Quit on ESC
|
||||||
|
def button_down(id)
|
||||||
|
close if id == Gosu::Button::KbEscape
|
||||||
|
if id == Gosu::Button::MsLeft then
|
||||||
|
@emitter.spawn(self.mouse_x, self.mouse_y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create the gosu window
|
||||||
|
window = GameWindow.new
|
||||||
|
window.show
|
20
python.rb
Normal file
20
python.rb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# python who ate an elephant
|
||||||
|
|
||||||
|
class Python
|
||||||
|
attr_reader :columns, :rows
|
||||||
|
|
||||||
|
def initialize radius
|
||||||
|
@columns = @rows = radius * 2
|
||||||
|
lower_half = (0...radius).map do |y|
|
||||||
|
x = Math.sqrt(radius**2 - y**2).round
|
||||||
|
right_half = "#{"\xff" * x}#{"\x00" * (radius - x)}"
|
||||||
|
"#{right_half.reverse}#{right_half}"
|
||||||
|
end.join
|
||||||
|
@blob = lower_half.reverse + lower_half
|
||||||
|
@blob.gsub!(/./) { |alpha| "\xff\xff\xff#{alpha}"}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_blob
|
||||||
|
@blob
|
||||||
|
end
|
||||||
|
end
|
Reference in a new issue