Created
April 14, 2016 16:25
-
-
Save krokrob/edcb76ea5cb54a4b2bf7c13429b7242d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'gosu' | |
require_relative 'player' | |
require_relative 'star' | |
class GameWindow < Gosu::Window | |
def initialize | |
super 640, 480 | |
self.caption = "Gosu Tutorial Game" | |
@background_image = Gosu::Image.new("media/space.png", :tileable => true) | |
@player = Player.new | |
@player.warp(320, 240) | |
@star_anim = Gosu::Image::load_tiles("media/star.png", 25, 25) | |
@stars = Array.new | |
@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 | |
if Gosu::button_down? Gosu::KbUp or Gosu::button_down? Gosu::GpButton0 then | |
@player.accelerate | |
end | |
if Gosu::button_down? Gosu::KbDown or Gosu::button_down? Gosu::GpButton0 then | |
@player.reverse | |
end | |
@player.move | |
@player.collect_stars(@stars) | |
if rand(100) < 4 and @stars.size < 25 then | |
@stars.push(Star.new(@star_anim)) | |
end | |
end | |
def draw | |
@player.draw | |
@background_image.draw(0, 0, 0) | |
@stars.each { |star| star.draw } | |
@font.draw("Score: #{@player.score}", 10, 10, 1, 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