Skip to content

Instantly share code, notes, and snippets.

@ryanb
Created January 23, 2009 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanb/50833 to your computer and use it in GitHub Desktop.
Save ryanb/50833 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'gosu'
class GameWindow < Gosu::Window
def initialize
super(640, 480, false)
self.caption = "Ruby Warrior"
@board = Board.new(self, 8, 1)
@unit = Unit.new(self)
@board.units << @unit
end
def update
end
def draw
@board.draw
end
def button_down(id)
if id == Gosu::Button::KbEscape
close
end
if id == Gosu::Button::KbLeft
@unit.x -= 1
end
if id == Gosu::Button::KbRight
@unit.x += 1
end
end
end
class Unit
attr_accessor :x, :y
def initialize(window)
@image = Gosu::Image.new(window, "media/warrior.png", false)
@x = 0
@y = 0
end
def draw(x_offset, y_offset, size)
@image.draw(x_offset + @x*size, y_offset + @y*size, 1)
end
end
class Board
attr_accessor :units
def initialize(window, w, h)
@window = window
@width = w
@height = h
@units = []
end
def draw
draw_rect(0, 0, pixel_width, pixel_height, Gosu::Color.new(0xff555555))
@width.times do |x|
@height.times do |y|
draw_rect(x*(square_size+padding)+boarder, y*(square_size+padding)+boarder, square_size, square_size)
end
end
@units.each do |unit|
unit.draw(x_offset+boarder, y_offset+boarder, square_size+padding)
end
end
def boarder
5
end
def padding
2
end
def square_size
50
end
def pixel_width
@width*(square_size+padding)+boarder*2
end
def pixel_height
@height*(square_size+padding)+boarder*2
end
def x_offset
@window.width/2 - pixel_width/2
end
def y_offset
@window.height/2 - pixel_height/2
end
def draw_rect(x, y, width, height, color = nil)
color ||= Gosu::Color.new(0xffffffff)
x += x_offset
y += y_offset
@window.draw_quad(x, y, color, x+width, y, color, x, y+height, color, x+width, y+height, color)
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