Advertisement
Guest User

lander src code

a guest
Aug 18th, 2011
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.63 KB | None | 0 0
  1. # lander.rb
  2.  
  3. $LOAD_PATH.unshift "."
  4.  
  5.  
  6. require "yaml"
  7. require "gosu"
  8.  
  9.  
  10. # Describes a basic 2D vector
  11. class Vector
  12.   PI = 3.141592653589793238462643383
  13.   TWOPI = PI * 2.0
  14.  
  15.   attr_accessor :x, :y
  16.  
  17.   def self.from_angle_and_length(angle, length)
  18.     angle_r = (angle - 90) * (PI / 180.0)
  19.     x = Math.cos(angle_r) * length
  20.     y = Math.sin(angle_r) * length
  21.     if y > 0
  22.       Vector.new(-x, y)
  23.     else
  24.       Vector.new(x, y)
  25.     end
  26.   end
  27.  
  28.   def initialize(x = 0.0, y = 0.0)
  29.     @x = x
  30.     @y = y
  31.   end
  32.  
  33.   def add(v)
  34.     @x += v.x
  35.     @y += v.y
  36.   end
  37.  
  38.   def magnitude
  39.     Math.hypot @x, @y
  40.   end
  41. end
  42.  
  43. class GameWindow < Gosu::Window
  44.   SCR_WIDTH = 800
  45.   SCR_HEIGHT = 600
  46.  
  47.   MODE_START = 1
  48.   MODE_PLAY = 2
  49.   MODE_CRASH = 3
  50.   MODE_LAND = 4
  51.  
  52.   COLOR_GREEN = 0xff00ff00
  53.   COLOR_RED = 0xffff0000
  54.  
  55.   def initialize
  56.     @game_state = MODE_START
  57.     @frame_count = 0
  58.     @start_milliseconds = Gosu::milliseconds
  59.    
  60.     config = nil
  61.    
  62.     # load the configuration file
  63.     begin
  64.       data = File.read "config.yml"
  65.       config = YAML.load data
  66.     rescue
  67.       puts "Could not read configuration file, exiting"
  68.       exit
  69.     end
  70.  
  71.     @gravity = Vector.new 0.0, config["gravity"]
  72.     @thrust = config["thrust"]
  73.     @player_fuel = config["fuel"]
  74.    
  75.     super SCR_WIDTH, SCR_HEIGHT, false
  76.     self.caption = "My first Gosu game test"
  77.    
  78.     @background = Gosu::Image.new self, "img/background.png", true
  79.     @player_img = Gosu::Imvectage.new self, "img/lander.png", true
  80.     @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
  81. #    @music = Gosu::Song.new(self, "music/song.m4a")
  82.     @thrust_sound = Gosu::Sample.new self, "sounds/thrust.wav"
  83.     @thrusting = nil
  84. #    @music.play
  85.  
  86.     reset_player
  87.   end
  88.  
  89.  
  90.   def reset_player
  91.     @player_x = 400
  92.     @player_y = 200
  93.     @player_angle = 0
  94.     @player_vector = Vector.new
  95.   end
  96.  
  97.  
  98.   def update
  99.     case @game_state
  100.     when MODE_START
  101.       update_start
  102.     when MODE_PLAY
  103.       update_play
  104.     when MODE_CRASH
  105.       update_start
  106.     when MODE_LAND
  107.       update_start
  108.     end
  109.   end
  110.  
  111.   def update_start
  112.     if button_down? Gosu::KbSpace
  113.       @game_state = MODE_PLAY
  114.       reset_player
  115.     end
  116.   end
  117.  
  118.  
  119.   def update_play
  120.     if button_down? Gosu::KbLeft
  121.       @player_angle -= 5
  122.     end
  123.     if button_down? Gosu::KbRight
  124.       @player_angle += 5
  125.     end
  126.  
  127.     @player_angle += 360 if @player_angle < 0
  128.     @player_angle -= 360 if @player_angle >= 360
  129.    
  130.     if button_down? Gosu::KbSpace and @player_fuel > 0
  131.       v = Vector.from_angle_and_length(@player_angle, @thrust)
  132.       @player_vector.add v
  133.       @player_fuel -= 1
  134.      
  135.       # If not already making thrust noise, start it
  136.       if @thrusting.nil?
  137.         @thrusting = @thrust_sound.play 1, 1, true
  138.       end
  139.       else
  140.       unless @thrusting.nil?
  141.         @thrusting.stop
  142.         @thrusting = nil
  143.       end
  144.     end
  145.    
  146.     @player_vector.add @gravity
  147.     @player_x += @player_vector.x
  148.     @player_y += @player_vector.y
  149.    
  150.     @player_x -= SCR_WIDTH if @player_x > SCR_WIDTH
  151.     @player_x += SCR_WIDTH if @player_x < 0
  152.  
  153.     # Check for landing conditions
  154.     if @player_y > 575
  155.       if (@player_angle < 10 or @player_angle > 350) and @player_vector.magnitude < 1.0
  156.         @game_state = MODE_LAND
  157.       else
  158.         @game_state = MODE_CRASH
  159.       end
  160.     end
  161.   end
  162.  
  163.  
  164.   def draw
  165.     case @game_state
  166.     when MODE_START
  167.       draw_start
  168.     when MODE_PLAY
  169.       draw_play
  170.     when MODE_CRASH
  171.       draw_start "You crashed!"
  172.     when MODE_LAND
  173.       draw_start "You landed!"
  174.     end
  175.     @frame_count += 1
  176.     fps = (@frame_count * 1000.0) / milliseconds
  177.     @font.draw "FPS: #{sprintf('%0.02f', fps)} (#{milliseconds}) (#{@frame_count})", 10, 570, 3, 1.0, 1.0, COLOR_GREEN
  178.   end
  179.  
  180.   def draw_start(msg = nil)
  181.     @background.draw 0, 0, 0
  182.  
  183.     unless msg.nil?
  184.       @font.draw msg, 100, 370, 3, 1.0, 1.0, COLOR_GREEN
  185.     end
  186.     @font.draw "Press the spacebar to start", 100, 400, 3, 1.0, 1.0, COLOR_GREEN
  187.   end
  188.  
  189.  
  190.   def draw_play
  191.     @background.draw 0, 0, 0
  192.    
  193.     @player_img.draw_rot @player_x, @player_y, 1, @player_angle
  194.     color = @player_fuel > 100 ? COLOR_GREEN : COLOR_RED
  195.     @font.draw "Fuel: #{@player_fuel}", 700, 5, 3, 1.0, 1.0, color
  196.   end
  197.  
  198.  
  199.  
  200.   def button_down(key_id)
  201.     close if key_id == Gosu::KbEscape
  202.   end
  203.  
  204.  
  205.   def milliseconds
  206.     Gosu::milliseconds - @start_milliseconds
  207.   end
  208.  
  209.  
  210. end
  211.  
  212. def main
  213.   window = GameWindow::new
  214.   window.show
  215. end
  216.  
  217. main
  218.  
  219.  
  220. # config.yml
  221. # Configuration file for Lander
  222. fuel: 800
  223. gravity: 0.025
  224. thrust: 0.10
  225. play_music: NO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement