-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
153 lines (132 loc) · 5.07 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'gosu'
require_relative 'components/pusher'
require_relative 'components/box'
require_relative 'components/storage'
require_relative 'components/empty'
require_relative 'components/wall'
require_relative 'components/level'
# Main window of our sokoban game
class SokoRuby < Gosu::Window
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 640
# We'll use a grid of 32x32
TILE_WIDTH = 32
TILE_HEIGHT = 32
def initialize
super(WINDOW_WIDTH, WINDOW_HEIGHT, resizable: false)
self.caption = 'SokoRuby'
@gameover = false
# To draw information on the screen
@font = Gosu::Font.new(32)
# Sample level to test the game
#
# Types of pieces:
# 0 = Empty
# 1 = Wall
# 2 = Box
# 3 = Storage
# 4 = Pusher
#
@level1 = Level.new(
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
)
# The current level being played
@current_level = @level1
@game_map = @current_level.populate
end
# Reset the game to the start
def reset
@current_level.reset
@game_map = @current_level.populate
@gameover = false
end
# Main update
def update
# The player needs to have the game level for movement
@current_level.player.update_map(@game_map)
# Check for game-winning condition
@gameover = true if @current_level.finished?
# Keyboard actions
reset if button_down?(Gosu::KB_R)
close if button_down?(Gosu::KB_ESCAPE)
end
def button_down(id)
# Player input is done here
@current_level.player.button_down?(id) unless @gameover
end
def draw
draw_level
draw_hud
draw_gameover if @gameover
end
def draw_hud
@font.draw_text('R to restart', 5, 0, 0)
@font.draw_text("Steps: #{@current_level.player.steps}", 5, 605, 0)
end
# Iterate throught the level and draw each piece
def draw_level
@game_map.each_with_index do |row, y|
row.each_with_index do |cell, x|
case cell
when Wall
Gosu.draw_rect(x * TILE_WIDTH,
y * TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
Gosu::Color::BLUE)
when Box
if cell.is_on_storage
Gosu.draw_rect(x * TILE_WIDTH,
y * TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
Gosu::Color::GREEN)
else
Gosu.draw_rect(x * TILE_WIDTH,
y * TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
Gosu::Color::YELLOW)
end
when Storage
Gosu.draw_rect(x * TILE_WIDTH,
y * TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
Gosu::Color::RED)
when Pusher
Gosu.draw_rect(x * TILE_WIDTH,
y * TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
Gosu::Color::WHITE)
end
end
end
end
# Draw a banner congratulating the player
def draw_gameover
Gosu.draw_rect(200, 200, 400, 50, Gosu::Color::WHITE)
@font.draw_text('Congratulations, you win!', 250, 205, 0, 1, 1, Gosu::Color::BLACK)
end
end
SokoRuby.new.show