-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_window.rb
74 lines (67 loc) · 1.5 KB
/
game_window.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
require 'rubygems'
require 'gosu'
require './piece'
class GameWindow < Gosu::Window
attr_accessor :piece
def initialize
super 1024, 960, false, 48
self.caption = "Tetronimo"
@background_image = Gosu::Image.new(self, "bg.png", true)
@menu = Gosu::Image.new(self, "menu.png", true)
@game_start = false
end
def update
if @game_start
if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft then
@piece.move_left
end
if button_down? Gosu::KbRight or button_down? Gosu::GpRight then
@piece.move_right
end
if button_down? Gosu::KbDown or button_down? Gosu::GpDown then
@piece.drop_faster
end
else
if button_down? Gosu::KbEnter or button_down? Gosu::KbReturn then
@game_start = true
@grid = Grid.new(self)
@piece = Piece.new(self, @grid)
end
end
end
def button_up(id)
if @game_start
case id
when Gosu::KbUp, Gosu::GpUp
@piece.rotate
@piece.draw
end
end
end
def draw
if @game_start
if @piece.y < Grid::GRID_BOTTOM
@piece.move
else
@grid.fit_to_grid(@piece)
@grid.check_for_lines
@piece = Piece.new(self, @grid)
end
@background_image.draw(0,0,0);
@piece.draw
@grid.draw
else
@menu.draw(0,0,0)
end
end
def button_down(id)
if id == Gosu::KbEscape
close
end
end
def gameover
@game_start = false
end
end
window = GameWindow.new
window.show