Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 554bcdd12b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 36 lines (28 sloc) 0.668 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
class Level < Chingu::GameState
  ENEMY_PROBABILITY_PER_TICK = 0.25
  MAX_ENEMIES = 5

  def initialize
    super
    @spaceship = Spaceship.create
    @score = 0
    @score_text = Chingu::Text.create("Score: #{@score}", x: 10, y: 10, zorder: 55, size: 20)
  end

  def update
    super

    if enemy_needed?
      Enemy.create
    end

    @spaceship.each_collision(Enemy) do |spaceship, enemy|
      enemy.destroy
      @score += Enemy::SCORE
      update_score_text
    end
  end

  private

  def enemy_needed?
    rand < ENEMY_PROBABILITY_PER_TICK && Enemy.all.size < MAX_ENEMIES
  end

  def update_score_text
    @score_text.text = "Score: #{@score}"
  end
end
Something went wrong with that request. Please try again.