Ejemplo n.º 1
0
class Tetris(object):
    def __init__(self, cols, rows):
        self.field = TetrisField(cols, rows)
        self.next_shape = self.random_shape()
        self.current_shape = self.random_shape()
        self.score = 0

    def step(self):
        if self.current_shape.can_move_down(self.field):
            self.current_shape.move_down(self.field)
            return True
        else:
            self.field.freeze(self.current_shape)
            self.current_shape = self.next_shape
            self.next_shape = self.random_shape()
            lines = self.field.explode()
            self.score += lines * lines * 10
            return False

    def can_go_down(self, current_shape):
        shape_bottom_y = current_shape.y + current_shape.size
        return shape_bottom_y < self.field.rows

    def touchdown(self):
        while self.step():
            pass

    def rotate(self):
        self.current_shape.rotate(self.field)

    def move_right(self):
        self.current_shape.move_right(self.field)

    def move_left(self):
        self.current_shape.move_left(self.field)

    def get_shape_future(self):
        return self.current_shape.get_future(self.field)

    def random_shape(self):
        start_x = int(self.field.cols / 2 - 1)
        return ShapeGenerator.random_shape(start_x)

    def is_over(self):
        return self.field.clashes_with(self.current_shape)
Ejemplo n.º 2
0
class FieldClashTestCase(TestCase):
    def setUp(self):
        self.field = TetrisField(5, 5)

    def test_shape_clashes_when_shape_cell_is_left_of_field(self):
        shape = ShapeT(-1, 0, 2)
        self.assertTrue(self.field.clashes_with(shape))

    def test_shape_clashes_1(self):
        shape = ShapeT(4, 0, 2)
        self.assertTrue(self.field.clashes_with(shape))

    def test_shape_not_clashes_0(self):
        shape = ShapeT(2, 0, 2)
        self.assertFalse(self.field.clashes_with(shape))

    def test_shape_not_clashes_1(self):
        shape = ShapeT(3, 0, 2)
        self.assertFalse(self.field.clashes_with(shape))

    def test_shape_not_clashes(self):
        shape = ShapeT(0, 0, 2)
        self.assertFalse(self.field.clashes_with(shape))

    def test_shape_not_clashes_2(self):
        shape = ShapeS(0, 0, 2)
        self.assertFalse(self.field.clashes_with(shape))

    def test_shape_not_clashes_3(self):
        shape = ShapeS(3, 0, 2)
        self.assertFalse(self.field.clashes_with(shape))
Ejemplo n.º 3
0
    def redraw(self, game: Tetris):
        self.drawer.clear()
        self.drawer.draw(game.field)
        self.drawer.draw_shape(game.get_shape_future(), game.field, transparent=True)
        self.drawer.draw_shape(game.current_shape, game.field)

        next_shape = game.next_shape.clone()
        next_shape.x = 0
        self.next_shape_drawer.clear()
        self.next_shape_drawer.draw_shape(next_shape, TetrisField(4, 4))

        self.score_label_var.set("Score: %d" % game.score)
Ejemplo n.º 4
0
    def init_ui(self, game):
        self.pack(fill=BOTH, expand=True)

        self.game_panel = Canvas(self, bg="#FFF", width=300, height=600, bd=0, highlightthickness=0, relief='ridge')
        self.game_panel.pack(anchor=W, expand=False, side=LEFT)

        self.info_panel = Frame(self, bg="#EEE")
        self.info_panel.pack(anchor=N, fill=BOTH, expand=TRUE, side=LEFT)

        self.score_label_var = StringVar(value="Score: 0")
        score_label = Label(self.info_panel, bg="#EEE", textvariable=self.score_label_var)
        score_label.pack()

        self.next_shape_panel = Canvas(self.info_panel, bg="#FFF", width=100, height=100, bd=0, highlightthickness=0, relief='ridge')
        self.next_shape_panel.pack()

        self.drawer = TetrisDrawer(self.game_panel, 300, 600, game.field)
        self.next_shape_drawer = TetrisDrawer(self.next_shape_panel, 100, 100, TetrisField(4, 4))
Ejemplo n.º 5
0
 def setUp(self):
     self.field = TetrisField(5, 5)
Ejemplo n.º 6
0
 def __init__(self, cols, rows):
     self.field = TetrisField(cols, rows)
     self.next_shape = self.random_shape()
     self.current_shape = self.random_shape()
     self.score = 0
Ejemplo n.º 7
0
 def rotate(self, field: TetrisField):
     my_future = self.clone()
     my_future._rotate_cells()
     if not field.clashes_with(my_future):
         self._rotate_cells()
Ejemplo n.º 8
0
 def move_left(self, field: TetrisField):
     my_future = self.clone()
     my_future.x -= 1
     if not field.clashes_with(my_future):
         self.x -= 1
Ejemplo n.º 9
0
 def move_down(self, field: TetrisField):
     my_future = self.clone()
     my_future.y += 1
     if not field.clashes_with(my_future):
         self.y += 1
Ejemplo n.º 10
0
 def can_move_down(self, field: TetrisField):
     my_future = self.clone()
     my_future.y += 1
     return not field.clashes_with(my_future)