lunar_lander 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.8.7@lunar_lander
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lunar_lander.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = lunar_lander
2
+
3
+ Ruby version of 1979 Atari video game Lunar Lander
4
+
5
+ == Install instructions
6
+
7
+ git clone git://github.com/lucasmundim/lunar_lander.git
8
+ cd lunar_lander
9
+ gem install bundler
10
+ bundle install
11
+ rake run
12
+
13
+ == Contributing to lunar_lander
14
+
15
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
16
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
17
+ * Fork the project
18
+ * Start a feature/bugfix branch
19
+ * Commit and push until you are happy with your contribution
20
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
21
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own com$
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2011 Lucas Mundim, Marcelo Murad. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift('lib')
2
+ require 'bundler'
3
+ require 'lunar_lander'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ Bundler::GemHelper.install_tasks
14
+
15
+ desc "Run game"
16
+ task :run do
17
+ LunarLander::Game.new.show
18
+ end
data/bin/lunar_lander ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler'
3
+ require 'lunar_lander'
4
+ LunarLander::Game.new.show
@@ -0,0 +1,12 @@
1
+ module LunarLander
2
+ class Game < Chingu::Window
3
+ def initialize
4
+ super(800,600)
5
+ Gosu::Image.autoload_dirs << File.join(File.expand_path(File.dirname(__FILE__)), "..", "media")
6
+ Gosu::Sound.autoload_dirs << File.join(File.expand_path(File.dirname(__FILE__)), "..", "media")
7
+
8
+ self.input = { :escape => :exit }
9
+ push_game_state(LunarLander::Play)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,107 @@
1
+ module LunarLander
2
+ class Player < Chingu::GameObject
3
+ trait :velocity
4
+ trait :bounding_box
5
+
6
+ attr_accessor :fuel
7
+
8
+ def initialize(options={})
9
+ super(options.merge(:image => Gosu::Image["player.png"]))
10
+ end
11
+
12
+ def setup
13
+ @engine_sound = Gosu::Sound["fierce_wind.wav"].play(1,1,true)
14
+ @engine_sound.pause
15
+ self.acceleration_y = 0.01
16
+ self.velocity_y = 1
17
+ @particle_animation = Chingu::Animation.new(:file => "particle.png", :size => [32,32])
18
+ @fuel = 100.0
19
+ end
20
+
21
+ def rotate_left
22
+ @angle -= 0.5
23
+
24
+ Chingu::Particle.create(
25
+ :x => @x + Gosu::offset_x(@angle + 60, 20),
26
+ :y => @y + Gosu::offset_y(@angle + 60, 20),
27
+ :animation => @particle_animation,
28
+ :scale_rate => -0.03,
29
+ :fade_rate => -35,
30
+ :rotation_rate => +1,
31
+ :mode => :default,
32
+ :factor => 0.5
33
+ )
34
+ Chingu::Particle.each { |particle|
35
+ particle.y -= Gosu::offset_y(@angle-90, 2)
36
+ particle.x -= Gosu::offset_x(@angle-90, 2)
37
+ }
38
+ end
39
+
40
+ def rotate_right
41
+ @angle += 0.5
42
+
43
+ Chingu::Particle.create(
44
+ :x => @x + Gosu::offset_x(@angle - 60, 20),
45
+ :y => @y + Gosu::offset_y(@angle - 60, 20),
46
+ :animation => @particle_animation,
47
+ :scale_rate => -0.03,
48
+ :fade_rate => -35,
49
+ :rotation_rate => +1,
50
+ :mode => :default,
51
+ :factor => 0.5
52
+ )
53
+ Chingu::Particle.each { |particle|
54
+ particle.y -= Gosu::offset_y(@angle+90, 2)
55
+ particle.x -= Gosu::offset_x(@angle+90, 2)
56
+ }
57
+ end
58
+
59
+ def thrust
60
+ if @fuel > 0
61
+ self.velocity_x += Gosu::offset_x(@angle, 0.05)
62
+ self.velocity_y += Gosu::offset_y(@angle, 0.05)
63
+
64
+ Chingu::Particle.create(
65
+ :x => @x - Gosu::offset_x(@angle, 20),
66
+ :y => @y - Gosu::offset_y(@angle, 20),
67
+ :animation => @particle_animation,
68
+ :scale_rate => -0.03,
69
+ :fade_rate => -35,
70
+ :rotation_rate => +1,
71
+ :mode => :default
72
+ )
73
+ Chingu::Particle.each { |particle|
74
+ particle.y -= Gosu::offset_y(@angle, 10 + rand(4))
75
+ particle.x -= Gosu::offset_x(@angle, 10 + rand(4))
76
+ }
77
+ @engine_sound.resume unless @engine_sound.playing?
78
+
79
+ @fuel -= 0.5
80
+ end
81
+ end
82
+
83
+ def stop_engine
84
+ @engine_sound.pause
85
+ end
86
+
87
+ def stop
88
+ self.velocity_x = 0
89
+ self.velocity_y = 0
90
+ end
91
+
92
+ def die
93
+ @engine_sound.stop
94
+ Gosu::Sound["explosion.wav"].play
95
+ end
96
+
97
+ def should_die?
98
+ velocity_x > 0.5 or velocity_y > 0.4 or angle.abs > 5
99
+ end
100
+
101
+ def update
102
+ super
103
+ @x %= $window.width
104
+ #@y %= 480
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,32 @@
1
+ module LunarLander
2
+ class Gameover < Chingu::GameState
3
+
4
+ def initialize(options = {})
5
+ super
6
+
7
+ @white = Gosu::Color.new(255,255,255,255)
8
+ @color = Gosu::Color.new(200,0,0,0)
9
+ @font = Gosu::Font[35]
10
+ @text = ["Perdeu playboy!","Pressione 'n' para tentar novamente."]
11
+
12
+ self.input = { :n => :new_game }
13
+ end
14
+
15
+ def new_game
16
+ switch_game_state LunarLander::Play
17
+ end
18
+
19
+ def draw
20
+ previous_game_state.draw # Draw prev game state onto screen (in this case our level)
21
+ $window.draw_quad( 0,0,@color,
22
+ $window.width,0,@color,
23
+ $window.width,$window.height,@color,
24
+ 0,$window.height,@color, Chingu::DEBUG_ZORDER)
25
+
26
+ @text.each_with_index do |text, index|
27
+ @font.draw(text, ($window.width/2 - @font.text_width(text)/2), $window.height/2 - (@font.height - (index * @font.height)), Chingu::DEBUG_ZORDER + 1)
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ module LunarLander
2
+ class Pause < Chingu::GameState
3
+
4
+ def initialize(options = {})
5
+ super
6
+ @white = Gosu::Color.new(255,255,255,255)
7
+ @color = Gosu::Color.new(200,0,0,0)
8
+ @font = Gosu::Font[35]
9
+ @text = "PAUSADO - pressione 'p' para retornar."
10
+
11
+ self.input = { :p => :resume }
12
+ end
13
+
14
+ def resume
15
+ pop_game_state(:setup => false)
16
+ end
17
+
18
+ def draw
19
+ previous_game_state.draw # Draw prev game state onto screen (in this case our level)
20
+ $window.draw_quad( 0,0,@color,
21
+ $window.width,0,@color,
22
+ $window.width,$window.height,@color,
23
+ 0,$window.height,@color, Chingu::DEBUG_ZORDER)
24
+
25
+ @font.draw(@text, ($window.width/2 - @font.text_width(@text)/2), $window.height/2 - @font.height, Chingu::DEBUG_ZORDER + 1)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,62 @@
1
+ module LunarLander
2
+ class Play < Chingu::GameState
3
+ def setup
4
+ self.input = { :p => LunarLander::Pause }
5
+
6
+ @player = Player.create({ :x => $window.width/2, :y => $window.height/2})
7
+ @player.input = {:holding_left => :rotate_left, :holding_right => :rotate_right, :holding_up => :thrust, :released_up => :stop_engine}
8
+
9
+ @background = Gosu::Image["moon.png"]
10
+ @surface = Chingu::Rect.new(0, $window.height-50, 800, 50)
11
+
12
+ setup_hud
13
+ end
14
+
15
+ def update
16
+ super
17
+
18
+ game_objects.destroy_if { |object|
19
+ if object.kind_of? Chingu::Particle
20
+ object.outside_window? || object.color.alpha == 0
21
+ end
22
+ }
23
+
24
+ test_colision
25
+
26
+ update_hud
27
+ end
28
+
29
+ def setup_hud
30
+ @velocity_x_text = Chingu::Text.create("Velocidade Lateral: 0", :x => 10, :y => 10, :zorder => 55, :size=>20)
31
+ @velocity_y_text = Chingu::Text.create("Velocidade Vertical: 0", :x => 10, :y => 30, :zorder => 55, :size=>20)
32
+ @angle_text = Chingu::Text.create("Ângulo: 0", :x => 10, :y => 50, :zorder => 55, :size=>20)
33
+ @fuel_text = Chingu::Text.create("Combustível: #{@player.fuel}", :x => 10, :y => 70, :zorder => 55, :size=>20)
34
+ end
35
+
36
+ def update_hud
37
+ if @player
38
+ @velocity_x_text.text = "Velocidade Lateral: #{(@player.velocity_x * 10).ceil.abs}"
39
+ @velocity_y_text.text = "Velocidade Vertical: #{(@player.velocity_y * 10).ceil * -1}"
40
+ @angle_text.text = "Ângulo: #{@player.angle}"
41
+ @fuel_text.text = "Combustível: #{@player.fuel.ceil}"
42
+ end
43
+ end
44
+
45
+ def test_colision
46
+ if @player.bounding_box.collide_rect?(@surface)
47
+ if @player.should_die?
48
+ @player.die
49
+ switch_game_state LunarLander::Gameover
50
+ end
51
+ @player.stop
52
+ end
53
+ end
54
+
55
+
56
+ def draw
57
+ super
58
+ @background.draw(0,0,0)
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module LunarLander
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ begin
2
+ require "bundler"
3
+ rescue LoadError
4
+ raise "Could not load the bundler gem. Install it with `gem install bundler`."
5
+ end
6
+
7
+ if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
8
+ raise RuntimeError, "Your bundler version is too old." +
9
+ "Run `gem install bundler` to upgrade."
10
+ end
11
+
12
+ begin
13
+ Bundler.setup(:default)
14
+ rescue Bundler::BundlerError => e
15
+ $stderr.puts e.message
16
+ $stderr.puts "Run `bundle install` to install missing gems"
17
+ exit e.status_code
18
+ end
19
+
20
+ require 'chingu'
21
+ require 'lunar_lander/game'
22
+ require 'lunar_lander/game_states/play'
23
+ require 'lunar_lander/game_states/pause'
24
+ require 'lunar_lander/game_states/gameover'
25
+ require 'lunar_lander/game_objects/player'
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "lunar_lander/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "lunar_lander"
7
+ s.version = LunarLander::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Lucas Roxo Mundim", "Marcelo Murad"]
10
+ s.email = ["lucas.mundim@gmail.com", "email@marcelomurad.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Moon landing simulator}
13
+ s.description = %q{Ruby version of 1979 Atari video game Lunar Lander}
14
+
15
+ s.rubyforge_project = "lunar_lander"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency(%q<chingu>, ["~> 0.8.1"])
23
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lunar_lander
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Lucas Roxo Mundim
14
+ - Marcelo Murad
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-03-02 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ type: :runtime
25
+ name: chingu
26
+ version_requirements: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ hash: 61
32
+ segments:
33
+ - 0
34
+ - 8
35
+ - 1
36
+ version: 0.8.1
37
+ requirement: *id001
38
+ description: Ruby version of 1979 Atari video game Lunar Lander
39
+ email:
40
+ - lucas.mundim@gmail.com
41
+ - email@marcelomurad.com
42
+ executables:
43
+ - lunar_lander
44
+ extensions: []
45
+
46
+ extra_rdoc_files: []
47
+
48
+ files:
49
+ - .gitignore
50
+ - .rvmrc
51
+ - Gemfile
52
+ - README.rdoc
53
+ - Rakefile
54
+ - bin/lunar_lander
55
+ - lib/lunar_lander.rb
56
+ - lib/lunar_lander/game.rb
57
+ - lib/lunar_lander/game_objects/player.rb
58
+ - lib/lunar_lander/game_states/gameover.rb
59
+ - lib/lunar_lander/game_states/pause.rb
60
+ - lib/lunar_lander/game_states/play.rb
61
+ - lib/lunar_lander/version.rb
62
+ - lib/media/explosion.wav
63
+ - lib/media/fierce_wind.wav
64
+ - lib/media/moon.png
65
+ - lib/media/particle.png
66
+ - lib/media/player.png
67
+ - lunar_lander.gemspec
68
+ has_rdoc: true
69
+ homepage: ""
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: lunar_lander
98
+ rubygems_version: 1.5.2
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Moon landing simulator
102
+ test_files: []
103
+