a_maze_ing 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/a_maze_ing/cell.rb +18 -11
- data/lib/a_maze_ing/cli.rb +1 -1
- data/lib/a_maze_ing/game_window.rb +23 -11
- data/lib/a_maze_ing/infor.rb +2 -0
- data/lib/a_maze_ing/maze.rb +33 -14
- data/lib/a_maze_ing/player.rb +8 -7
- data/lib/a_maze_ing/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac2d90d1b404af1a23d3c15aa56ba2b43980a053
|
4
|
+
data.tar.gz: 16c989a33c190aa047ea29d4e3e1a4edac58678d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2869dfccd82fe6eb394db5e1b2c80a79d97a35de8263baf0405f0a7fe611383ffc58aa8ae89d84fb3ae7f0b1d85e5a1f0560e968e26d390d648b2aa3d984e880
|
7
|
+
data.tar.gz: b61f4b49474ef300ea74daeed71aad0eac7ff8a17910cbc2e3c106447e3b1aecfce1aae3c2a5c0c3462197c547756eb570284e3c90651704d1cb22d2d1c7ef89
|
data/lib/a_maze_ing/cell.rb
CHANGED
@@ -11,9 +11,16 @@ module AMazeIng
|
|
11
11
|
@is_current = false
|
12
12
|
end
|
13
13
|
|
14
|
+
# cell_index is the index in cells array
|
15
|
+
# cell_index_x and cell_index_y are index of cells in vitual grid
|
16
|
+
|
17
|
+
# Get cell_index by cell_index_x and cell_index_y
|
18
|
+
# if the given indexes is invalid (outside the maze)
|
19
|
+
# it will return cells_length,
|
20
|
+
# which will cause the returned cell (neighbor) to be equal to nil
|
14
21
|
def cell_index(i, j, cells_length)
|
15
22
|
if i < 0 || j < 0 || i > $cols-1 || j > $rows-1
|
16
|
-
return cells_length # cause the cell (neighbor) equal nil
|
23
|
+
return cells_length # cause the cell to be (neighbor) equal to nil
|
17
24
|
else
|
18
25
|
return i + j * $cols
|
19
26
|
end
|
@@ -22,10 +29,10 @@ module AMazeIng
|
|
22
29
|
def get_random_neighbor(cells)
|
23
30
|
@neigh_bors = Array.new
|
24
31
|
|
25
|
-
top = cells[cell_index(cell_index_x,
|
26
|
-
right = cells[cell_index(cell_index_x + 1, cell_index_y,
|
27
|
-
bottom = cells[cell_index(cell_index_x,
|
28
|
-
left = cells[cell_index(cell_index_x - 1, cell_index_y,
|
32
|
+
top = cells[cell_index(cell_index_x, cell_index_y - 1, cells.length)]
|
33
|
+
right = cells[cell_index(cell_index_x + 1, cell_index_y, cells.length)]
|
34
|
+
bottom = cells[cell_index(cell_index_x, cell_index_y + 1, cells.length)]
|
35
|
+
left = cells[cell_index(cell_index_x - 1, cell_index_y, cells.length)]
|
29
36
|
|
30
37
|
if top
|
31
38
|
if !top.visited
|
@@ -65,23 +72,23 @@ module AMazeIng
|
|
65
72
|
y = @cell_index_y * cell_size
|
66
73
|
|
67
74
|
if @walls[0]
|
68
|
-
draw_line x,
|
69
|
-
x + cell_size, y,
|
75
|
+
draw_line x, y, color,
|
76
|
+
x + cell_size, y, color
|
70
77
|
end
|
71
78
|
|
72
79
|
if @walls[1]
|
73
|
-
draw_line x + cell_size, y,
|
80
|
+
draw_line x + cell_size, y, color,
|
74
81
|
x + cell_size, y + cell_size, color
|
75
82
|
end
|
76
83
|
|
77
84
|
if @walls[2]
|
78
85
|
draw_line x + cell_size, y + cell_size, color,
|
79
|
-
x,
|
86
|
+
x, y + cell_size, color
|
80
87
|
end
|
81
88
|
|
82
89
|
if @walls[3]
|
83
|
-
draw_line x,
|
84
|
-
x,
|
90
|
+
draw_line x, y + cell_size, color,
|
91
|
+
x, y, color
|
85
92
|
end
|
86
93
|
end
|
87
94
|
end
|
data/lib/a_maze_ing/cli.rb
CHANGED
@@ -41,7 +41,7 @@ module AMazeIng
|
|
41
41
|
|
42
42
|
command :annoying_friend do |c|
|
43
43
|
c.syntax = 'a_maze_ing anfri [options]'
|
44
|
-
c.description = '
|
44
|
+
c.description = 'Annoying friend mode, player 1 try to get to the gate while the player 2(annoying friend) try to catch him'
|
45
45
|
c.action do
|
46
46
|
AMazeIng::GameWindow.new(@full_screen,3).show
|
47
47
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# cGREENits to: http://en.wikipedia.org/wiki/Maze_generation_algorithm
|
3
2
|
|
4
3
|
require 'gosu'
|
5
4
|
include Gosu
|
@@ -8,15 +7,28 @@ require "observer"
|
|
8
7
|
module AMazeIng
|
9
8
|
DIMENSION = 700
|
10
9
|
SIDE_BAR = 180
|
10
|
+
ROWS = COLS = 10
|
11
11
|
PLAYER_COLOR_PRIMARY = Color::GREEN
|
12
12
|
PLAYER_COLOR_SECONDARY = Color::AQUA
|
13
13
|
PLAYER_COLOR_ANGRY = Color::RED
|
14
|
+
|
15
|
+
# the rate of character speed, in pixel,
|
16
|
+
# the higher step rate the slower character will move
|
17
|
+
STEP_RATE = 4
|
18
|
+
|
19
|
+
|
20
|
+
# Change this if you don't want it to be at bottom right
|
21
|
+
TARGET_CELL_INDEX_X = COLS - 1
|
22
|
+
TARGET_CELL_INDEX_Y = ROWS - 1
|
23
|
+
|
14
24
|
class GameWindow < Window
|
15
|
-
$rows = $cols =
|
25
|
+
$rows = $cols = ROWS
|
16
26
|
def initialize(full_screen, game_mode)
|
17
27
|
@game_mode = game_mode
|
18
28
|
super DIMENSION + SIDE_BAR, DIMENSION, full_screen, 30
|
19
29
|
self.caption = "Maze"
|
30
|
+
|
31
|
+
|
20
32
|
|
21
33
|
#---------------------------------------------------------------------------------#
|
22
34
|
# create code block (update, player_draw and new_player) for different game mode #
|
@@ -88,7 +100,11 @@ module AMazeIng
|
|
88
100
|
@infor = Infor.new(PLAYER_COLOR_PRIMARY, PLAYER_COLOR_ANGRY)
|
89
101
|
end
|
90
102
|
|
91
|
-
|
103
|
+
@target_x = (TARGET_CELL_INDEX_X * $cell_size) + $cell_size/2 - $player_size/2
|
104
|
+
@target_y = (TARGET_CELL_INDEX_Y * $cell_size) + $cell_size/2 - $player_size/2
|
105
|
+
|
106
|
+
end # End initialize function
|
107
|
+
|
92
108
|
|
93
109
|
def new_round
|
94
110
|
$rows += 2
|
@@ -99,14 +115,10 @@ module AMazeIng
|
|
99
115
|
end
|
100
116
|
|
101
117
|
def draw_target(cell)
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
draw_quad x, y, Color::WHITE,
|
107
|
-
x+$player_size, y, Color::WHITE,
|
108
|
-
x+$player_size, y+$player_size, Color::WHITE,
|
109
|
-
x, y+$player_size, Color::WHITE
|
118
|
+
draw_quad @target_x, @target_y, Color::WHITE,
|
119
|
+
@target_x + $player_size, @target_y, Color::WHITE,
|
120
|
+
@target_x + $player_size, @target_y + $player_size, Color::WHITE,
|
121
|
+
@target_x, @target_y + $player_size, Color::WHITE
|
110
122
|
end
|
111
123
|
|
112
124
|
def check_for_finish(player)
|
data/lib/a_maze_ing/infor.rb
CHANGED
@@ -4,6 +4,7 @@ module AMazeIng
|
|
4
4
|
def initialize(*player_color)
|
5
5
|
@player_color = player_color
|
6
6
|
|
7
|
+
# This class will renders information of the game, like points and level
|
7
8
|
|
8
9
|
#---------------------------------------------------------------------------------#
|
9
10
|
# create code block (initial, update and draw) for different game mode #
|
@@ -15,6 +16,7 @@ module AMazeIng
|
|
15
16
|
|
16
17
|
@level = 1
|
17
18
|
@level_image = Gosu::Image.from_text @level.to_s, 36
|
19
|
+
|
18
20
|
@label = 'LEVEL'
|
19
21
|
@label_image = Gosu::Image.from_text @label, 40
|
20
22
|
|
data/lib/a_maze_ing/maze.rb
CHANGED
@@ -1,23 +1,33 @@
|
|
1
|
+
# Credits to: http://en.wikipedia.org/wiki/Maze_generation_algorithm
|
2
|
+
# I use Depth-first search recursive backtracking algorithm
|
3
|
+
# You can find the information in the wiki link
|
4
|
+
|
1
5
|
module AMazeIng
|
2
6
|
class Maze
|
3
7
|
|
8
|
+
# This is very important.
|
9
|
+
# With this calculated dimension
|
10
|
+
# the character will be exactly at the pixel you want it to be
|
4
11
|
def check_dimension
|
5
|
-
until $dimension/$cols %
|
12
|
+
until $dimension/$cols % STEP_RATE == 0
|
6
13
|
$dimension -= 1
|
7
14
|
end
|
8
15
|
end
|
9
16
|
|
10
17
|
def generate_maze
|
18
|
+
|
19
|
+
# set initial specifications for the maze
|
11
20
|
$dimension = DIMENSION
|
12
21
|
check_dimension
|
13
22
|
$cell_size = $dimension/$cols
|
14
|
-
$speed_per_tick = $cell_size/
|
23
|
+
$speed_per_tick = $cell_size/STEP_RATE
|
15
24
|
$player_size = 0.5 * $cell_size
|
16
25
|
|
17
26
|
$cells = Array.new
|
18
27
|
@stack = Array.new
|
19
28
|
|
20
|
-
|
29
|
+
# I use one dimensional array to store all the cell...
|
30
|
+
# yeah, should not do that... feel regret now
|
21
31
|
$rows.times do |row_index|
|
22
32
|
$cols.times do |col_index|
|
23
33
|
cell = Cell.new(col_index, row_index)
|
@@ -25,11 +35,15 @@ module AMazeIng
|
|
25
35
|
end
|
26
36
|
end
|
27
37
|
|
38
|
+
# Ok, here is where the heavy work begin
|
39
|
+
# Let set visited for the top left cell, where we start
|
28
40
|
$cells[0].visited = true
|
29
41
|
@current_cell = $cells[0]
|
30
42
|
# @current_cell.is_current = true
|
31
43
|
@stack.push(@current_cell)
|
32
44
|
|
45
|
+
# This is the same as "While there are unvisited cells"
|
46
|
+
# at least, same result, see the wiki page for more
|
33
47
|
while @stack.length > 0 do
|
34
48
|
next_cell = @current_cell.get_random_neighbor($cells)
|
35
49
|
if next_cell
|
@@ -43,37 +57,42 @@ module AMazeIng
|
|
43
57
|
@current_cell = @stack.pop
|
44
58
|
end
|
45
59
|
end
|
60
|
+
|
61
|
+
# Remove more random walls to make more available way
|
46
62
|
remove_extra_walls
|
47
63
|
end
|
48
64
|
|
49
65
|
def remove_walls(current_cell, next_cell)
|
50
66
|
if next_cell != nil
|
67
|
+
|
68
|
+
# yeah I'd like to call it magic_number
|
69
|
+
# it show me the relative position of two cell
|
51
70
|
magic_number = next_cell.cell_index_x - current_cell.cell_index_x
|
52
|
-
|
53
|
-
|
71
|
+
|
72
|
+
if magic_number == 1 # next cell is on the right
|
54
73
|
current_cell.walls[1] = next_cell.walls[3] = false
|
55
|
-
|
56
|
-
|
74
|
+
|
75
|
+
elsif magic_number == -1 # next cell is on the left
|
57
76
|
current_cell.walls[3] = next_cell.walls[1] = false
|
58
|
-
|
59
|
-
|
77
|
+
|
78
|
+
elsif magic_number == 0 # next cell is either on top or bottom
|
60
79
|
magic_number = next_cell.cell_index_y - current_cell.cell_index_y
|
61
|
-
|
62
|
-
|
80
|
+
|
81
|
+
if magic_number == 1 #next cell is bottom
|
63
82
|
current_cell.walls[2] = next_cell.walls[0] = false
|
64
|
-
|
65
|
-
|
83
|
+
|
84
|
+
elsif magic_number ==-1#next cell is on top
|
66
85
|
current_cell.walls[0] = next_cell.walls[2] = false
|
67
86
|
end
|
68
87
|
end
|
69
88
|
end
|
70
89
|
end
|
71
90
|
|
91
|
+
# For now, it almost doesn't remove horizontal walls
|
72
92
|
def remove_extra_walls
|
73
93
|
a_count_number = $cells.length/2/5
|
74
94
|
while a_count_number > 0 do
|
75
95
|
cell_index = rand(0..$cells.length/2)
|
76
|
-
# wall_index = rand(0..3)
|
77
96
|
remove_walls($cells[cell_index], $cells[cell_index+1])
|
78
97
|
a_count_number -= 1
|
79
98
|
end
|
data/lib/a_maze_ing/player.rb
CHANGED
@@ -49,12 +49,13 @@ module AMazeIng
|
|
49
49
|
def move
|
50
50
|
if @is_moving
|
51
51
|
if @x == @target_x && @y == @target_y
|
52
|
+
|
52
53
|
# check for available path,
|
53
|
-
# and ignore the the opposite of the LAST path
|
54
|
+
# and ignore the the opposite directions of the LAST path
|
54
55
|
# cuz you don't wanna go back where you just left
|
55
56
|
@path = check_for_path(@path == 0 ? 2:
|
56
|
-
|
57
|
-
|
57
|
+
@path == 1 ? 3:
|
58
|
+
@path == 2 ? 0: 1)
|
58
59
|
if @path != nil
|
59
60
|
# set new player's cell index depend on "current @path"
|
60
61
|
set_status(@path)
|
@@ -85,10 +86,10 @@ module AMazeIng
|
|
85
86
|
end
|
86
87
|
|
87
88
|
def draw
|
88
|
-
draw_quad @x,
|
89
|
-
@x
|
90
|
-
@x
|
91
|
-
@x,
|
89
|
+
draw_quad @x, @y, @color,
|
90
|
+
@x + $player_size, @y, @color,
|
91
|
+
@x + $player_size, @y + $player_size, @color,
|
92
|
+
@x, @y + $player_size, @color
|
92
93
|
end
|
93
94
|
end
|
94
95
|
end
|
data/lib/a_maze_ing/version.rb
CHANGED