Skip to content

Instantly share code, notes, and snippets.

@actsasbuffoon
Created October 4, 2011 19:51
Show Gist options
  • Save actsasbuffoon/1262606 to your computer and use it in GitHub Desktop.
Save actsasbuffoon/1262606 to your computer and use it in GitHub Desktop.
Simple game made with Ruby-Processing
# Drop
class Player
attr_accessor :health, :x, :y, :width, :height
def initialize(args = {})
args.each_pair {|k, v| send "#{k}=", v}
end
def check_health
@health = 0 if @health < 0
@health = 100 if @health > 100
if @health == 0
exit
end
end
def check_level
$GAME.level = ($GAME.score + 1 / 5) + 1
end
def absorb(droplet)
if droplet.type == :good
@health += 5
$GAME.score += 1
elsif droplet.type == :bad
@health -= 20
end
check_health
check_level
droplet.reset
end
def collide(droplet)
@y > droplet.y - droplet.height && @y < droplet.y + droplet.height && @x > droplet.x - droplet.width && @x < droplet.x + droplet.width
end
end
class Droplet
attr_accessor :type, :x, :y, :width, :height, :speed
def initialize(args = {})
args.each_pair {|k, v| send "#{k}=", v}
reset
end
def reset
@height = 20
@width = 20
@y = -@height
@x = rand($GAME.width)
@type = $droplet_types[rand($droplet_types.length)]
@speed = rand(30) / 10.0 + 1
self
end
end
class Drop < Processing::App
attr_accessor :level, :score
def setup
size(400, 400)
@player = Player.new :health => 100, :x => 100, :y => 100, :width => 20, :height => 20
@droplets = []
$droplet_types = [:good, :bad]
@colors = {
:good => [100, 100, 255],
:bad => [255, 100, 100],
:hit => [100, 255, 100]
}
$GAME = self
@level = 1
@score = 0
end
def draw
fill 0
no_stroke
rect 0, 0, @height, @width
fill 127
@droplets << Droplet.new if rand(100) == 0 && @droplets.length < @level * 5
@player.x -= (@player.x - mouse_x) / 10
@player.y -= (@player.y - mouse_y) / 10
rect @player.x - @player.width / 2, @player.y - @player.height / 2, @player.width, @player.height
@droplets.each do |d|
d.y += d.speed
d.reset if d.y - d.height / 2 > height
@player.absorb(d) if @player.collide(d)
fill *@colors[d.type]
rect d.x - d.width / 2, d.y - d.height / 2, d.width, d.height
end
no_fill
stroke 128
rect 20, 20, 20, 100
fill 255
rect 20, 120 - @player.health, 20, @player.health
end
end
Drop.new :title => "Drop"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment