Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created January 14, 2016 17:29
Show Gist options
  • Select an option

  • Save krokrob/bf1dd29da4089a2ce615 to your computer and use it in GitHub Desktop.

Select an option

Save krokrob/bf1dd29da4089a2ce615 to your computer and use it in GitHub Desktop.
# Here is the game!
require 'gosu'
require_relative 'player'
require_relative 'star'
module ZOrder
Background, Stars, Player, UI = *0..3
end
class GameWindow < Gosu::Window
def initialize
super 640, 480
self.caption = "Gosu Tutorial Game"
@background_image = Gosu::Image.new("images/space.png", :tileable => true)
@star_anim = Gosu::Image::load_tiles("images/star.png", 25, 25)
@stars = Array.new
@player = Player.new
@player.warp(320, 440)
@font = Gosu::Font.new(20)
end
def update
if Gosu::button_down? Gosu::KbLeft or Gosu::button_down? Gosu::GpLeft then
@player.turn_left
end
if Gosu::button_down? Gosu::KbRight or Gosu::button_down? Gosu::GpRight then
@player.turn_right
end
@player.move
if rand(100) < 4 and @stars.size < 250 then
@stars.push(Star.new(@star_anim))
end
@stars.each do |star|
star.move
end
@player.collect_stars(@stars)
end
def draw
@player.draw
@background_image.draw(0, 0, 0);
@stars.each { |star| star.draw }
@font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xff_ffff00)
end
def button_down(id)
if id == Gosu::KbEscape
close
end
end
end
window = GameWindow.new
window.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment