Tetris

Форк
0
/
settingsTetris.py 
117 строк · 3.2 Кб
1
import random
2

3

4
class Setting:
5
    """Parameters of Frame"""
6
    board_h = 15
7
    board_w = 10
8

9

10
class Tetro:
11
    """  Набор фигур """
12
    NoShape = 0
13
    Square = 1
14
    Piramid = 2
15
    zFigure = 3
16
    Line = 4
17
    lFigure = 5
18
    MirroredLShape = 6
19
    MirroredZFigure = 7
20

21

22
class Shape():
23
    coordsTable = (
24
        ((0, 0), (0, 0), (0, 0), (0, 0)),
25
        ((0, 0), (-1, -1), (0, -1), (-1, 0)),
26
        ((0, 0), (-1, 1), (0, 1), (1, 1)),
27
        ((0, 0), (0, 1), (1, 1), (1, 2)),
28
        ((0, 0), (0, -1), (0, -2), (0, 1)),
29
        ((0, 0), (0, 1), (0, 2), (1, 0)),
30
        ((0, 0), (0, 1), (0, 2), (-1, 0)),
31
        ((0, 0), (0, 1), (-1, 1), (-1, 2))
32
    )
33

34
    def __init__(self):
35
        '''Start coords and type of shape'''
36
        self.curx = 0
37
        self.cury = 0
38
        self.coords = [[0, 0] for i in range(4)]
39
        self.shape = Tetro.NoShape
40
        self.futureShape = random.randint(1, 7)
41
        self.setRandomShape()
42

43
    def setShape(self, shape):
44
        '''Set type of shape'''
45
        self.shape = shape
46
        table = Shape.coordsTable[self.shape]
47
        for i in range(4):
48
            for j in range(2):
49
                self.coords[i][j] = table[i][j]
50

51
    def setRandomShape(self):
52
        self.shape = self.futureShape
53
        self.futureShape = random.randint(1, 7)
54

55
    def checkMaxSizeX(self):
56
        '''Found max x'''
57
        maxX = []
58
        for i in range(4):
59
            maxX.append(self.coords[i][0])
60
        return max(maxX)
61

62
    def checkMinSizeX(self):
63
        '''Found min x'''
64
        minX = []
65
        for i in range(4):
66
            minX.append(self.coords[i][0])
67
        return min(minX)
68

69
    def checkMinSizeY(self):
70
        '''Found min y'''
71
        minY = []
72
        for i in range(4):
73
            minY.append(self.coords[i][1])
74
        return min(minY)
75

76
    def checkMaxSizeY(self):
77
        '''Found max y'''
78
        maxY = []
79
        for i in range(4):
80
            maxY.append(self.coords[i][1])
81
        return max(maxY)
82

83
    def rotateShape(self, direction, curshape, board):
84
        if curshape.shape == Tetro.Square:
85
            return curshape.coords
86
        result = Shape()
87
        result.shape = curshape.shape
88
        result.setShape(result.shape)
89
        result.curx = curshape.curx
90
        result.cury = curshape.cury
91
        for i in range(4):
92
            result.coords[i][0] = curshape.coords[i][1] * direction
93
            result.coords[i][1] = -curshape.coords[i][0] * direction
94

95
        st = Setting()
96
        # Check  border
97
        xRight = result.curx + result.checkMaxSizeX()
98
        xLeft = result.curx + result.checkMinSizeX()
99

100
        YUp = result.cury + result.checkMaxSizeY()
101
        YDown = result.cury + result.checkMinSizeY()
102

103
        if xRight < st.board_w and xLeft >= 0 and YUp < st.board_h and YDown >= 0:
104
            if not result.checkCollision(board, 0, 0):
105
                return result.coords
106
            else:
107
                return curshape.coords
108
        else:
109
            return curshape.coords
110

111
    def checkCollision(self, board, dopX, dopY):
112
        for i in range(4):
113
            x = (self.coords[i][0] + self.curx) + dopX
114
            y = (self.coords[i][1] + self.cury) + dopY
115
            if board[y][x] != Tetro.NoShape:
116
                return True
117
        return False
118

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.