btetris_kp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/btetris_kp +5 -0
- data/btetris_kp.gemspec +25 -0
- data/lib/btetris_kp/btetris.rb +37 -0
- data/lib/btetris_kp/constants.rb +197 -0
- data/lib/btetris_kp/core/board.rb +223 -0
- data/lib/btetris_kp/core/piece.rb +79 -0
- data/lib/btetris_kp/game.rb +118 -0
- data/lib/btetris_kp/gui/menuitem.rb +44 -0
- data/lib/btetris_kp/gui/textfield.rb +66 -0
- data/lib/btetris_kp/menu.rb +83 -0
- data/lib/btetris_kp/netgame.rb +152 -0
- data/lib/btetris_kp/netjoin.rb +115 -0
- data/lib/btetris_kp/netsetup.rb +79 -0
- data/lib/btetris_kp/version.rb +3 -0
- data/lib/btetris_kp.rb +5 -0
- data/media/drop.ogg +0 -0
- data/media/pop.ogg +0 -0
- data/media/rotate.ogg +0 -0
- data/media/title.png +0 -0
- data/spec/btetris_spec.rb +15 -0
- data/spec/core/board_spec.rb +94 -0
- data/spec/core/piece_spec.rb +114 -0
- data/spec/game_spec.rb +29 -0
- data/spec/gui/menuitem_spec.rb +39 -0
- data/spec/gui/textfield_spec.rb +35 -0
- data/spec/menu_spec.rb +24 -0
- data/spec/netgame_spec.rb +75 -0
- data/spec/netjoin_spec.rb +15 -0
- data/spec/netsetup_spec.rb +16 -0
- data/spec/spec_helper.rb +5 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 425a26d38d0419d50ff8cb0126faaa1caaf6d1e0
|
4
|
+
data.tar.gz: f8f2deaa630c4a9bd247656156d7869aa2725142
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ae3785d6fd1403caefa5074b9e10fde7da908ad09338bcdce001e32de21c5e464aa8cd5062d977573c109afa4b386457345286bbd5a222dd1bf6da8b099c1f57
|
7
|
+
data.tar.gz: e0eb2eda6ba106b60420ce7ea59a58c0c89f9af787fe8212660c98fa87514bdd912f1f7590bfd3e7b1b4169d5ba92a389dbb3af94aaf9197077e6df20263142d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Pavel Kocka
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# BtetrisKp
|
2
|
+
|
3
|
+
Battle Tetris - tetris game for 2 player via lan
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'btetris_kp'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install btetris_kp
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
run btetris_kp
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/btetris_kp
ADDED
data/btetris_kp.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'btetris_kp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "btetris_kp"
|
8
|
+
spec.version = BtetrisKp::VERSION
|
9
|
+
spec.authors = ["Pavel Kocka"]
|
10
|
+
spec.email = ["kockapav@gmail.com"]
|
11
|
+
spec.description = %q{Battle Tetris}
|
12
|
+
spec.summary = %q{Tetris for 2 players via lan}
|
13
|
+
spec.homepage = "https://github.com/kockapav/btetris_kp"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake", "~> 0"
|
23
|
+
spec.add_development_dependency "gosu", "~> 0.7"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'gosu'
|
2
|
+
require 'btetris_kp/menu'
|
3
|
+
require 'btetris_kp/constants'
|
4
|
+
|
5
|
+
module BTetrisKp
|
6
|
+
# Main class maintaining game window,
|
7
|
+
# drawing, updating and reacting to key events
|
8
|
+
class BTetrisWindow < Gosu::Window
|
9
|
+
attr_accessor :state
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super(Const::GAME_WIDTH, Const::GAME_HEIGHT, false)
|
13
|
+
self.caption = Const::GAME_CAPTION
|
14
|
+
@state = MenuState.new(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
# propagates update event to current window state
|
18
|
+
def update
|
19
|
+
@state.update
|
20
|
+
end
|
21
|
+
|
22
|
+
# propagates draw event to current window state
|
23
|
+
def draw
|
24
|
+
@state.draw
|
25
|
+
end
|
26
|
+
|
27
|
+
# propagates button_down event to current window state
|
28
|
+
def button_down(id)
|
29
|
+
@state.button_down(id)
|
30
|
+
end
|
31
|
+
|
32
|
+
# shows or hides default system cursor depending on state
|
33
|
+
def needs_cursor?
|
34
|
+
@state.needs_cursor?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,197 @@
|
|
1
|
+
module BTetrisKp
|
2
|
+
# module with constants, colors, tile shapes
|
3
|
+
module Const
|
4
|
+
# Game window Const
|
5
|
+
GAME_WIDTH = 800
|
6
|
+
GAME_HEIGHT = 600
|
7
|
+
GAME_CAPTION = 'Battle tetris'
|
8
|
+
PAUSE_CAPTION = 'Paused!'
|
9
|
+
GAME_OVER_CAPTION = 'Game over!'
|
10
|
+
GAME_WON_CAPTION = 'You WIN!'
|
11
|
+
GAME_LOST_CAPTION = 'You LOOSE!'
|
12
|
+
|
13
|
+
# File paths
|
14
|
+
PATH = File.dirname(File.expand_path(__FILE__))
|
15
|
+
PATH_IMAGE_TITLE = File.join(PATH, '../../media/title.png')
|
16
|
+
PATH_SND_DROP = File.join(PATH, '../../media/drop.ogg')
|
17
|
+
PATH_SND_POP = File.join(PATH, '../../media/pop.ogg')
|
18
|
+
PATH_SND_ROTATE = File.join(PATH, '../../media/rotate.ogg')
|
19
|
+
|
20
|
+
# Menu item captions
|
21
|
+
MENU_NEW = 'New game'
|
22
|
+
MENU_CREATE = 'Create net game'
|
23
|
+
MENU_JOIN = 'Join net game'
|
24
|
+
MENU_QUIT = 'Quit'
|
25
|
+
|
26
|
+
# Net captions
|
27
|
+
IP_CAPTION = 'IP:'
|
28
|
+
PORT_CAPTION = 'Port:'
|
29
|
+
CONNECTING = 'Connecting...'
|
30
|
+
DEF_IP = '127.0.0.1'
|
31
|
+
DEF_PORT = ''
|
32
|
+
SERVER_WAIT = 'Waiting for client to connect...'
|
33
|
+
SERVER_PORT = 'Used port: '
|
34
|
+
|
35
|
+
# Game Const
|
36
|
+
GAME_SPEED = 60
|
37
|
+
DROP_SPEED = 5
|
38
|
+
TURN_SPEED = 8
|
39
|
+
GAME_WON = 1
|
40
|
+
GAME_LOST = -1
|
41
|
+
GAME_ON = 0
|
42
|
+
|
43
|
+
# Size of board in pieces
|
44
|
+
PNR_HOR = 10
|
45
|
+
PNR_VER = 20
|
46
|
+
|
47
|
+
# TextInput Const
|
48
|
+
BORDER_GAP = 5
|
49
|
+
|
50
|
+
# Network Const
|
51
|
+
GOT_NO_MESSAGE = '0'
|
52
|
+
MSG_PAUSE = '1'
|
53
|
+
MSG_GAME_OVER = '2'
|
54
|
+
MSG_BOARD = '3'
|
55
|
+
MSG_GARBAGE = '4'
|
56
|
+
MSG_WELCOME = '5'
|
57
|
+
|
58
|
+
# Color Const
|
59
|
+
BOARD_BACK_CLR = Gosu::Color.new(0xFF101010)
|
60
|
+
BOARD_CLR = Gosu::Color.new(0xFFB8B8B8)
|
61
|
+
CARET_CLR = Gosu::Color.new(0xFFFFFFFF)
|
62
|
+
MENU_ITEM_CLR = Gosu::Color.new(0xFFFFFFFF)
|
63
|
+
MENU_ITEM_MO_CLR = Gosu::Color.new(0xFFCCFF33)
|
64
|
+
# NR of colors shouldnt be higher then 9, (board#from_s)
|
65
|
+
TILE_COLORS_NR = 8
|
66
|
+
TILE_COLORS = [0,
|
67
|
+
Gosu::Color.new(0xFFC80000),
|
68
|
+
Gosu::Color.new(0xFF0000CC),
|
69
|
+
Gosu::Color.new(0xFF990066),
|
70
|
+
Gosu::Color.new(0xFF99FF00),
|
71
|
+
Gosu::Color.new(0xFFCCFF00),
|
72
|
+
Gosu::Color.new(0xFFFF6600),
|
73
|
+
Gosu::Color.new(0xFF00FF99),
|
74
|
+
Gosu::Color.new(0xFFFFFF00)]
|
75
|
+
TILE_BRIGHT_CLR = [0,
|
76
|
+
Gosu::Color.new(0xFFE00000),
|
77
|
+
Gosu::Color.new(0xFF0033CC),
|
78
|
+
Gosu::Color.new(0xFF990099),
|
79
|
+
Gosu::Color.new(0xFF99FF33),
|
80
|
+
Gosu::Color.new(0xFFCCFF33),
|
81
|
+
Gosu::Color.new(0xFFFF6633),
|
82
|
+
Gosu::Color.new(0xFF00FFCC),
|
83
|
+
Gosu::Color.new(0xFFFFFF33)]
|
84
|
+
TILE_SHADOW_CLR = [0,
|
85
|
+
Gosu::Color.new(0xFF700000),
|
86
|
+
Gosu::Color.new(0xFF000099),
|
87
|
+
Gosu::Color.new(0xFF990033),
|
88
|
+
Gosu::Color.new(0xFF99CC00),
|
89
|
+
Gosu::Color.new(0xFFCCCC00),
|
90
|
+
Gosu::Color.new(0xFFFF3300),
|
91
|
+
Gosu::Color.new(0xFF00CC99),
|
92
|
+
Gosu::Color.new(0xFFFFCC00)]
|
93
|
+
|
94
|
+
TILES =
|
95
|
+
[
|
96
|
+
[
|
97
|
+
[
|
98
|
+
[1, 1],
|
99
|
+
[1, 1]
|
100
|
+
]
|
101
|
+
],
|
102
|
+
[
|
103
|
+
[
|
104
|
+
[1, 1, 1],
|
105
|
+
[0, 1, 0]
|
106
|
+
],
|
107
|
+
[
|
108
|
+
[0, 1],
|
109
|
+
[1, 1],
|
110
|
+
[0, 1]
|
111
|
+
],
|
112
|
+
[
|
113
|
+
[0, 1, 0],
|
114
|
+
[1, 1, 1]
|
115
|
+
],
|
116
|
+
[
|
117
|
+
[1, 0],
|
118
|
+
[1, 1],
|
119
|
+
[1, 0]
|
120
|
+
]
|
121
|
+
],
|
122
|
+
[
|
123
|
+
[
|
124
|
+
[1, 1, 0],
|
125
|
+
[0, 1, 1]
|
126
|
+
],
|
127
|
+
[
|
128
|
+
[0, 1],
|
129
|
+
[1, 1],
|
130
|
+
[1, 0]
|
131
|
+
]
|
132
|
+
],
|
133
|
+
[
|
134
|
+
[
|
135
|
+
[0, 1, 1],
|
136
|
+
[1, 1, 0]
|
137
|
+
],
|
138
|
+
[
|
139
|
+
[1, 0],
|
140
|
+
[1, 1],
|
141
|
+
[0, 1]
|
142
|
+
]
|
143
|
+
],
|
144
|
+
[
|
145
|
+
[
|
146
|
+
[1, 1, 1, 1]
|
147
|
+
],
|
148
|
+
[
|
149
|
+
[1],
|
150
|
+
[1],
|
151
|
+
[1],
|
152
|
+
[1]
|
153
|
+
]
|
154
|
+
],
|
155
|
+
[
|
156
|
+
[
|
157
|
+
[1, 0, 0],
|
158
|
+
[1, 1, 1]
|
159
|
+
],
|
160
|
+
[
|
161
|
+
[1, 1],
|
162
|
+
[1, 0],
|
163
|
+
[1, 0]
|
164
|
+
],
|
165
|
+
[
|
166
|
+
[1, 1, 1],
|
167
|
+
[0, 0, 1]
|
168
|
+
],
|
169
|
+
[
|
170
|
+
[0, 1],
|
171
|
+
[0, 1],
|
172
|
+
[1, 1]
|
173
|
+
]
|
174
|
+
],
|
175
|
+
[
|
176
|
+
[
|
177
|
+
[0, 0, 1],
|
178
|
+
[1, 1, 1]
|
179
|
+
],
|
180
|
+
[
|
181
|
+
[1, 0],
|
182
|
+
[1, 0],
|
183
|
+
[1, 1]
|
184
|
+
],
|
185
|
+
[
|
186
|
+
[1, 1, 1],
|
187
|
+
[1, 0, 0]
|
188
|
+
],
|
189
|
+
[
|
190
|
+
[1, 1],
|
191
|
+
[0, 1],
|
192
|
+
[0, 1]
|
193
|
+
]
|
194
|
+
]
|
195
|
+
]
|
196
|
+
end
|
197
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
require 'btetris_kp/menu'
|
2
|
+
require 'btetris_kp/core/piece'
|
3
|
+
|
4
|
+
module BTetrisKp
|
5
|
+
# class representing one tetris board
|
6
|
+
class Board
|
7
|
+
attr_accessor :board, :cur_piece
|
8
|
+
|
9
|
+
def initialize(window, posx, posy)
|
10
|
+
@window = window
|
11
|
+
@tile_size = @window.width / 30
|
12
|
+
|
13
|
+
# board corners position
|
14
|
+
@posx = posx
|
15
|
+
@posx2 = posx + Const::PNR_HOR * @tile_size
|
16
|
+
@posy = posy
|
17
|
+
@posy2 = posy + Const::PNR_VER * @tile_size
|
18
|
+
|
19
|
+
# initializes board array and current piece
|
20
|
+
@board = Array.new(Const::PNR_VER) { Array.new(Const::PNR_HOR, 0) }
|
21
|
+
@cur_piece = Piece.new(@board, 0, Const::PNR_HOR / 3 + 1, rand(Const::TILES.size))
|
22
|
+
end
|
23
|
+
|
24
|
+
# generates new piece,
|
25
|
+
# current piece is set on board and replaced by new one
|
26
|
+
def new_piece!
|
27
|
+
@cur_piece.set_on_board
|
28
|
+
@cur_piece = Piece.new(@board, 0, Const::PNR_HOR / 3 + 1, rand(Const::TILES.size))
|
29
|
+
end
|
30
|
+
|
31
|
+
# checks whether piece can move down
|
32
|
+
def piece_stuck?
|
33
|
+
@cur_piece.move_down!
|
34
|
+
can_be_set = @cur_piece.can_be_set?
|
35
|
+
@cur_piece.move_up!
|
36
|
+
!can_be_set
|
37
|
+
end
|
38
|
+
|
39
|
+
# moves piece left
|
40
|
+
def piece_left!
|
41
|
+
@cur_piece.move_left!
|
42
|
+
@cur_piece.move_right! unless @cur_piece.can_be_set?
|
43
|
+
end
|
44
|
+
|
45
|
+
# moves piece right
|
46
|
+
def piece_right!
|
47
|
+
@cur_piece.move_right!
|
48
|
+
@cur_piece.move_left! unless @cur_piece.can_be_set?
|
49
|
+
end
|
50
|
+
|
51
|
+
# moves piece down faster
|
52
|
+
def piece_down!
|
53
|
+
@cur_piece.move_down!
|
54
|
+
@cur_piece.move_up! unless @cur_piece.can_be_set?
|
55
|
+
end
|
56
|
+
|
57
|
+
# rotates piece, returns true when successful
|
58
|
+
def piece_rotate!
|
59
|
+
@cur_piece.rotate_right!
|
60
|
+
return true if @cur_piece.can_be_set?
|
61
|
+
@cur_piece.rotate_left!
|
62
|
+
false
|
63
|
+
end
|
64
|
+
|
65
|
+
# drops peace down as far as possible
|
66
|
+
def piece_drop!
|
67
|
+
@cur_piece.move_down! while @cur_piece.can_be_set?
|
68
|
+
@cur_piece.move_up!
|
69
|
+
end
|
70
|
+
|
71
|
+
# next step of game, returns count of cleared rows
|
72
|
+
def next_step!
|
73
|
+
cnt = 0
|
74
|
+
@cur_piece.move_down!
|
75
|
+
unless @cur_piece.can_be_set?
|
76
|
+
@cur_piece.move_up!
|
77
|
+
new_piece!
|
78
|
+
cnt = clear_rows!
|
79
|
+
end
|
80
|
+
cnt
|
81
|
+
end
|
82
|
+
|
83
|
+
# copies content of one row into another
|
84
|
+
# r1 - source row, r2 - destination row
|
85
|
+
def copy_row(r1, r2)
|
86
|
+
if r1 < 0
|
87
|
+
@board[r2].each_with_index { |val, index| @board[r2][index] = 0 }
|
88
|
+
else
|
89
|
+
@board[r1].each_with_index { |val, index| @board[r2][index] = val }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# fills row randomly with blocks and spaces
|
94
|
+
# atleast one block has to be empty space
|
95
|
+
def fill_row_randomly(index)
|
96
|
+
@board[index].each_with_index do |val, x|
|
97
|
+
@board[index][x] = rand(Const::TILE_COLORS_NR + 1)
|
98
|
+
end
|
99
|
+
@board[index][rand(Const::PNR_HOR)] = 0
|
100
|
+
end
|
101
|
+
|
102
|
+
# inserts garbage in board depending on count of cleared rows
|
103
|
+
def insert_garbage!(cnt)
|
104
|
+
for index in cnt.upto(Const::PNR_VER - 1)
|
105
|
+
copy_row(index, index - cnt)
|
106
|
+
end
|
107
|
+
for index in (Const::PNR_VER - cnt).upto(Const::PNR_VER - 1)
|
108
|
+
fill_row_randomly(index)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# moves down all rows with smaller index then row_index,
|
113
|
+
# removing row at row_index content in the process
|
114
|
+
def clear_row!(row_index)
|
115
|
+
for index in row_index.downto(0) do
|
116
|
+
copy_row(index - 1, index)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# clears rows that are full, returns number of cleared rows
|
121
|
+
def clear_rows!
|
122
|
+
cnt = 0
|
123
|
+
@board.each_with_index do |row, index|
|
124
|
+
unless row.include?(0)
|
125
|
+
clear_row!(index)
|
126
|
+
cnt += 1
|
127
|
+
end
|
128
|
+
end
|
129
|
+
cnt
|
130
|
+
end
|
131
|
+
|
132
|
+
# checks if game is over
|
133
|
+
def game_over?
|
134
|
+
!@cur_piece.can_be_set?
|
135
|
+
end
|
136
|
+
|
137
|
+
# draws board border and background
|
138
|
+
def draw_background
|
139
|
+
@window.draw_quad(@posx, @posy, Const::BOARD_BACK_CLR,
|
140
|
+
@posx2, @posy, Const::BOARD_BACK_CLR,
|
141
|
+
@posx, @posy2, Const::BOARD_BACK_CLR,
|
142
|
+
@posx2, @posy2, Const::BOARD_BACK_CLR)
|
143
|
+
@window.draw_line(@posx - 1, @posy - 1, Const::BOARD_CLR,
|
144
|
+
@posx2 + 1, @posy - 1, Const::BOARD_CLR)
|
145
|
+
@window.draw_line(@posx2 + 1, @posy - 1, Const::BOARD_CLR,
|
146
|
+
@posx2 + 1, @posy2 + 1, Const::BOARD_CLR)
|
147
|
+
@window.draw_line(@posx - 1, @posy - 1, Const::BOARD_CLR,
|
148
|
+
@posx - 1, @posy2 + 1, Const::BOARD_CLR)
|
149
|
+
@window.draw_line(@posx - 1, @posy2 + 1, Const::BOARD_CLR,
|
150
|
+
@posx2 + 1, @posy2 + 1, Const::BOARD_CLR)
|
151
|
+
end
|
152
|
+
|
153
|
+
# draws block on board
|
154
|
+
def draw_block(x, y, val)
|
155
|
+
unless val == 0
|
156
|
+
x1 = @posx + x * @tile_size
|
157
|
+
x2 = @posx + (x + 1) * @tile_size
|
158
|
+
y1 = @posy + y * @tile_size
|
159
|
+
y2 = @posy + (y + 1) * @tile_size
|
160
|
+
|
161
|
+
@window.draw_quad(x1, y1, Const::TILE_COLORS[val],
|
162
|
+
x2, y1, Const::TILE_COLORS[val],
|
163
|
+
x1, y2, Const::TILE_COLORS[val],
|
164
|
+
x2, y2, Const::TILE_COLORS[val])
|
165
|
+
|
166
|
+
@window.draw_line(x2 - 1, y1 + 1, Const::TILE_SHADOW_CLR[val],
|
167
|
+
x2 - 1, y2 - 1, Const::TILE_SHADOW_CLR[val])
|
168
|
+
@window.draw_line(x1 + 1 , y2 - 1, Const::TILE_SHADOW_CLR[val],
|
169
|
+
x2 - 1, y2 - 1, Const::TILE_SHADOW_CLR[val])
|
170
|
+
@window.draw_line(x2, y1, Const::TILE_SHADOW_CLR[val],
|
171
|
+
x2, y2, Const::TILE_SHADOW_CLR[val])
|
172
|
+
@window.draw_line(x1, y2, Const::TILE_SHADOW_CLR[val],
|
173
|
+
x2, y2, Const::TILE_SHADOW_CLR[val])
|
174
|
+
|
175
|
+
@window.draw_line(x1, y1, Const::TILE_BRIGHT_CLR[val],
|
176
|
+
x2, y1, Const::TILE_BRIGHT_CLR[val])
|
177
|
+
@window.draw_line(x1, y1, Const::TILE_BRIGHT_CLR[val],
|
178
|
+
x1, y2, Const::TILE_BRIGHT_CLR[val])
|
179
|
+
@window.draw_line(x1 + 1, y1 + 1, Const::TILE_BRIGHT_CLR[val],
|
180
|
+
x2 - 1, y1 + 1, Const::TILE_BRIGHT_CLR[val])
|
181
|
+
@window.draw_line(x1 + 1, y1 + 1, Const::TILE_BRIGHT_CLR[val],
|
182
|
+
x1 + 1, y2 - 1, Const::TILE_BRIGHT_CLR[val])
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# draws board and all blocks on it
|
187
|
+
def draw
|
188
|
+
draw_background
|
189
|
+
@cur_piece.set_on_board
|
190
|
+
@board.each_with_index do |row, y|
|
191
|
+
row.each_with_index do |val, x|
|
192
|
+
draw_block(x, y, val)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
@cur_piece.unset_on_board
|
196
|
+
end
|
197
|
+
|
198
|
+
# loads board content from string
|
199
|
+
# !!! (expects board to contain only nrs from 0 to 9) !!!
|
200
|
+
def from_s!(state)
|
201
|
+
@board = state.split('').map { |i| i.to_i }.each_slice(Const::PNR_HOR).to_a
|
202
|
+
end
|
203
|
+
|
204
|
+
# returns string of board with current piece
|
205
|
+
def get_board_s
|
206
|
+
@cur_piece.set_on_board
|
207
|
+
s = to_s
|
208
|
+
@cur_piece.unset_on_board
|
209
|
+
s
|
210
|
+
end
|
211
|
+
|
212
|
+
# converts board content to string
|
213
|
+
def to_s
|
214
|
+
s = ''
|
215
|
+
@board.each do |row|
|
216
|
+
row.each do |val|
|
217
|
+
s << val.to_s
|
218
|
+
end
|
219
|
+
end
|
220
|
+
s
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'btetris_kp/constants'
|
2
|
+
|
3
|
+
module BTetrisKp
|
4
|
+
# class representing actual piece in game
|
5
|
+
class Piece
|
6
|
+
def initialize(board, posx, posy, piece_nr)
|
7
|
+
@board = board
|
8
|
+
@posx = posx
|
9
|
+
@posy = posy
|
10
|
+
@nr = piece_nr
|
11
|
+
@rot = 0
|
12
|
+
@color = rand(Const::TILE_COLORS_NR) + 1
|
13
|
+
@piece = Const::TILES[@nr][@rot]
|
14
|
+
end
|
15
|
+
|
16
|
+
# moves piece one block to the left
|
17
|
+
def move_left!
|
18
|
+
@posy -= 1
|
19
|
+
end
|
20
|
+
|
21
|
+
# moves piece one block to the right
|
22
|
+
def move_right!
|
23
|
+
@posy += 1
|
24
|
+
end
|
25
|
+
|
26
|
+
# moves piece one block up
|
27
|
+
def move_up!
|
28
|
+
@posx -= 1
|
29
|
+
end
|
30
|
+
|
31
|
+
# moves piece one block down
|
32
|
+
def move_down!
|
33
|
+
@posx += 1
|
34
|
+
end
|
35
|
+
|
36
|
+
# rotates piece left
|
37
|
+
def rotate_left!
|
38
|
+
@rot = (@rot - 1) % Const::TILES[@nr].size
|
39
|
+
@piece = Const::TILES[@nr][@rot]
|
40
|
+
end
|
41
|
+
|
42
|
+
# rotates piece right
|
43
|
+
def rotate_right!
|
44
|
+
@rot = (@rot + 1) % Const::TILES[@nr].size
|
45
|
+
@piece = Const::TILES[@nr][@rot]
|
46
|
+
end
|
47
|
+
|
48
|
+
# returns true if there is no collision with peace on board
|
49
|
+
def can_be_set?
|
50
|
+
return false if @posx < 0 || @posy < 0
|
51
|
+
@piece.each_with_index do |row, x|
|
52
|
+
row.each_with_index do |val, y|
|
53
|
+
return false if @posx + x > Const::PNR_VER - 1 ||
|
54
|
+
@posy + y > Const::PNR_HOR - 1
|
55
|
+
return false if @board[@posx + x][@posy + y] > 0 && val > 0
|
56
|
+
end
|
57
|
+
end
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
# sets peace on board
|
62
|
+
def set_on_board
|
63
|
+
@piece.each_with_index do |row, x|
|
64
|
+
row.each_with_index do |val, y|
|
65
|
+
@board[@posx + x][@posy + y] = @color if val == 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# unsets peace from board
|
71
|
+
def unset_on_board
|
72
|
+
@piece.each_with_index do |row, x|
|
73
|
+
row.each_with_index do |val, y|
|
74
|
+
@board[@posx + x][@posy + y] = 0 if val == 1
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|