Tetris
/
settingsTetris.py
117 строк · 3.2 Кб
1import random
2
3
4class Setting:
5"""Parameters of Frame"""
6board_h = 15
7board_w = 10
8
9
10class Tetro:
11""" Набор фигур """
12NoShape = 0
13Square = 1
14Piramid = 2
15zFigure = 3
16Line = 4
17lFigure = 5
18MirroredLShape = 6
19MirroredZFigure = 7
20
21
22class Shape():
23coordsTable = (
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
34def __init__(self):
35'''Start coords and type of shape'''
36self.curx = 0
37self.cury = 0
38self.coords = [[0, 0] for i in range(4)]
39self.shape = Tetro.NoShape
40self.futureShape = random.randint(1, 7)
41self.setRandomShape()
42
43def setShape(self, shape):
44'''Set type of shape'''
45self.shape = shape
46table = Shape.coordsTable[self.shape]
47for i in range(4):
48for j in range(2):
49self.coords[i][j] = table[i][j]
50
51def setRandomShape(self):
52self.shape = self.futureShape
53self.futureShape = random.randint(1, 7)
54
55def checkMaxSizeX(self):
56'''Found max x'''
57maxX = []
58for i in range(4):
59maxX.append(self.coords[i][0])
60return max(maxX)
61
62def checkMinSizeX(self):
63'''Found min x'''
64minX = []
65for i in range(4):
66minX.append(self.coords[i][0])
67return min(minX)
68
69def checkMinSizeY(self):
70'''Found min y'''
71minY = []
72for i in range(4):
73minY.append(self.coords[i][1])
74return min(minY)
75
76def checkMaxSizeY(self):
77'''Found max y'''
78maxY = []
79for i in range(4):
80maxY.append(self.coords[i][1])
81return max(maxY)
82
83def rotateShape(self, direction, curshape, board):
84if curshape.shape == Tetro.Square:
85return curshape.coords
86result = Shape()
87result.shape = curshape.shape
88result.setShape(result.shape)
89result.curx = curshape.curx
90result.cury = curshape.cury
91for i in range(4):
92result.coords[i][0] = curshape.coords[i][1] * direction
93result.coords[i][1] = -curshape.coords[i][0] * direction
94
95st = Setting()
96# Check border
97xRight = result.curx + result.checkMaxSizeX()
98xLeft = result.curx + result.checkMinSizeX()
99
100YUp = result.cury + result.checkMaxSizeY()
101YDown = result.cury + result.checkMinSizeY()
102
103if xRight < st.board_w and xLeft >= 0 and YUp < st.board_h and YDown >= 0:
104if not result.checkCollision(board, 0, 0):
105return result.coords
106else:
107return curshape.coords
108else:
109return curshape.coords
110
111def checkCollision(self, board, dopX, dopY):
112for i in range(4):
113x = (self.coords[i][0] + self.curx) + dopX
114y = (self.coords[i][1] + self.cury) + dopY
115if board[y][x] != Tetro.NoShape:
116return True
117return False
118