|
6 | 6 | class MyPiece < Piece
|
7 | 7 | # The constant All_My_Pieces should be declared here
|
8 | 8 |
|
| 9 | + def initialize(point_array, board) |
| 10 | + super(point_array, board) |
| 11 | + end |
| 12 | + |
| 13 | + def self.next_piece(board) |
| 14 | + if board.set_check |
| 15 | + board.set_check = false |
| 16 | + piece = [[[0,0]]] |
| 17 | + else |
| 18 | + piece = All_Pieces.sample |
| 19 | + end |
| 20 | + MyPiece.new(piece, board) |
| 21 | + end |
| 22 | + |
9 | 23 | # your enhancements here
|
| 24 | + # |
| 25 | + |
| 26 | + All_My_Pieces = All_Pieces |
| 27 | + |
| 28 | + All_Pieces << [ [[0,0], [-1, 0], [-2, 0], [1, 0], [2, 0]], |
| 29 | + [[0,0], [0, 1], [0, 2], [0, -1], [0, -2]]] |
| 30 | + All_Pieces << rotations([[0, 0], [0, 1], [1,1]]) |
| 31 | + All_Pieces << rotations([[0,0], [-1,0], [-1,1], [0, 1], [1, 1]]) |
10 | 32 |
|
11 | 33 | end
|
12 | 34 |
|
13 | 35 | class MyBoard < Board
|
14 | 36 | # your enhancements here
|
| 37 | + attr_accessor :set_check |
| 38 | + |
| 39 | + def initialize(game) |
| 40 | + super(game) |
| 41 | + @set_check = false |
| 42 | + end |
| 43 | + |
| 44 | + def rotate180 |
| 45 | + if !game_over? and @game.is_running? |
| 46 | + @current_block.move(0, 0, 1) |
| 47 | + @current_block.move(0, 0, 1) |
| 48 | + end |
| 49 | + draw |
| 50 | + end |
| 51 | + |
| 52 | + def store_current |
| 53 | + locations = @current_block.current_rotation |
| 54 | + displacement = @current_block.position |
| 55 | + locations.each_index{|index| |
| 56 | + current = locations[index]; |
| 57 | + @grid[current[1]+displacement[1]][current[0]+displacement[0]] = |
| 58 | + @current_pos[index] |
| 59 | + } |
| 60 | + remove_filled |
| 61 | + @delay = [@delay - 2, 80].max |
| 62 | + end |
| 63 | + |
| 64 | + def check |
| 65 | + return if @set_check or score < 100 |
| 66 | + @set_check = true |
| 67 | + @score -= 100 |
| 68 | + end |
| 69 | + |
| 70 | + def next_piece |
| 71 | + @current_block = MyPiece.next_piece(self) |
| 72 | + @current_pos = nil |
| 73 | + end |
15 | 74 |
|
16 | 75 | end
|
17 | 76 |
|
18 | 77 | class MyTetris < Tetris
|
19 | 78 | # your enhancements here
|
20 | 79 |
|
21 |
| -end |
| 80 | + def initialize |
| 81 | + super |
| 82 | + end |
| 83 | + |
| 84 | + def set_board |
| 85 | + @canvas = TetrisCanvas.new |
| 86 | + @board = MyBoard.new(self) |
| 87 | + @canvas.place(@board.block_size * @board.num_rows + 3, |
| 88 | + @board.block_size * @board.num_columns + 6, 24, 80) |
| 89 | + @board.draw |
| 90 | + end |
22 | 91 |
|
23 | 92 |
|
| 93 | + def check |
| 94 | + @board.check |
| 95 | + update_score |
| 96 | + end |
| 97 | + |
| 98 | + def key_bindings |
| 99 | + super |
| 100 | + |
| 101 | + @root.bind('c', proc {self.check}) |
| 102 | + @root.bind('u', proc {@board.rotate180}) |
| 103 | + end |
| 104 | +end |
0 commit comments