ryanb (owner)

Revisions

gist: 50833 Download_button fork
public
Public Clone URL: git://gist.github.com/50833.git
Embed All Files: show embed
gosu_example.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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