Skip to content

Commit 1aefc6d

Browse files
committed
Tetris game
1 parent 30f8f14 commit 1aefc6d

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

GAMES/Tetris/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame==2.1.2

GAMES/Tetris/tetris.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
from socket import gaierror
2+
import pygame
3+
import random
4+
pygame.init()
5+
6+
blocks = [
7+
[[1,4,7],[3,4,5]], #straight
8+
[[1,3,4,5,7]], #cross
9+
[[0,1,4,5],[1,3,4,6]], # two on two ones
10+
[[1,2,3,4],[0,3,4,7]],
11+
[[0,1,3,6],[0,1,2,5],[2,5,7,8],[3,6,7,8]],
12+
[[1,2,5,8],[5,6,7,8],[0,3,6,7],[0,1,2,3]],
13+
[[4,6,7,8],[0,3,4,6],[0,1,2,4],[2,4,5,8]]
14+
]
15+
16+
colours = [
17+
(122,78,0),
18+
(0,255,0),
19+
(100,60,200),
20+
(100,50,100),
21+
(50,100,200),
22+
(255,0,0),
23+
(0,0,255)
24+
]
25+
26+
class Block:
27+
def __init__(self,x,y):
28+
self.x = x
29+
self.y = y
30+
self.type = random.randint(0,len(blocks) - 1)
31+
self.rotation = 0
32+
self.colour = colours[random.randint(0,len(colours) -1)]
33+
34+
def shape(self):
35+
return blocks[self.type][self.rotation]
36+
37+
def draw_block(screen, block, grid_size, x_gap, y_gap):
38+
for y in range(3):
39+
for x in range(3):
40+
if y * 3 + x in block.shape():
41+
pygame.draw.rect(screen,block.colour,
42+
[(x + block.x) * grid_size + x_gap + 1,
43+
(y + block.y) * grid_size + y_gap + 1,grid_size - 2,grid_size - 2])
44+
45+
def collides(block,rows,cols,game_board,ny):
46+
collision = False
47+
for y in range(3):
48+
for x in range(3):
49+
if y * 3 + x in block.shape():
50+
if y + block.y + ny > rows - 1 or y + block.y + ny < 0:
51+
collision = True
52+
break
53+
if x + block.x > cols - 1 or x + block.x < 0:
54+
collision = True
55+
break
56+
if game_board[x + block.x][y + block.y + ny] != (0,0,0):
57+
collision = True
58+
break
59+
return collision
60+
61+
def rotate(block,rows,cols,game_board):
62+
last_rotate = block.rotation
63+
block.rotation = (block.rotation + 1)%len(blocks[block.type])
64+
can_rotate = True
65+
for y in range(3):
66+
for x in range(3):
67+
if y * 3 + x in block.shape():
68+
if collides(block,rows,cols,game_board,ny=0):
69+
can_rotate = False
70+
if not can_rotate:
71+
block.rotation = last_rotate
72+
73+
74+
def drawGrid(screen,rows,cols,x_gap,y_gap,grid_size,game_board):
75+
for y in range(rows):
76+
for x in range(cols):
77+
pygame.draw.rect(screen,(100,100,100),[x * grid_size + x_gap,y * grid_size + y_gap,grid_size,grid_size],1)
78+
pygame.draw.rect(screen,game_board[x][y],[x * grid_size + x_gap + 1,y * grid_size + y_gap + 1,grid_size - 2,grid_size - 2])
79+
80+
def dropBlock(block,rows,cols,game_board):
81+
can_drop = True
82+
for y in range(3):
83+
for x in range(3):
84+
if y * 3 + x in block.shape():
85+
if collides(block,rows,cols,game_board,ny=1):
86+
can_drop = False
87+
if can_drop:
88+
block.y += 1
89+
else:
90+
for y in range(3):
91+
for x in range(3):
92+
if y * 3 + x in block.shape():
93+
game_board[x + block.x][y + block.y] = block.colour
94+
return can_drop
95+
96+
97+
def sideMove(block,cols,dx):
98+
can_move = True
99+
for y in range(3):
100+
for x in range(3):
101+
if y * 3 + x in block.shape():
102+
if block.x + x >= cols - 1 and dx == 1:
103+
can_move = False
104+
elif block.x + x < 1 and dx == -1:
105+
can_move = False
106+
if can_move:
107+
block.x += dx
108+
109+
def findLines(rows,cols,game_board):
110+
lines = 0
111+
for y in range(rows):
112+
empty = 0
113+
for x in range(cols):
114+
if game_board[x][y] == (0,0,0):
115+
empty += 1
116+
if empty == 0:
117+
lines += 1
118+
for y2 in range(y,1,-1):
119+
for x2 in range(cols):
120+
game_board[x2][y2] = game_board[x2][y2 - 1]
121+
return lines
122+
123+
def main():
124+
screen = pygame.display.set_mode((300,600))
125+
pygame.display.set_caption('Tetris')
126+
grid_size = 30
127+
cols = screen.get_width() // grid_size
128+
rows = screen.get_height() // grid_size
129+
x_gap = (screen.get_width() - cols * grid_size) // 2
130+
y_gap = (screen.get_height() - rows * grid_size) // 2
131+
block = Block((cols - 1) // 2,0)
132+
game_over = False
133+
clock = pygame.time.Clock()
134+
fps = 8
135+
136+
#game board setting black color
137+
game_board = []
138+
for i in range(cols):
139+
new_col = []
140+
for j in range(rows):
141+
new_col.append((0,0,0))
142+
game_board.append(new_col)
143+
144+
score = 0
145+
font = pygame.font.SysFont('Arial', 25, True)
146+
font_quit = pygame.font.SysFont('Arial', 50, True)
147+
finished_text = font_quit.render('Game Over', True, (255,255,255))
148+
text_pos = [(screen.get_width() - finished_text.get_width())// 2, (screen.get_height() - finished_text.get_height()) // 2]
149+
game_finished = False
150+
while not game_over:
151+
clock.tick(fps)
152+
for event in pygame.event.get():
153+
if event.type == pygame.QUIT:
154+
game_over = True
155+
if event.type == pygame.KEYDOWN:
156+
if event.key == pygame.K_UP:
157+
rotate(block,rows,cols,game_board)
158+
if event.type == pygame.KEYDOWN:
159+
if block is not None:
160+
if event.key == pygame.K_LEFT:
161+
sideMove(block,cols,-1)
162+
if event.key == pygame.K_RIGHT:
163+
sideMove(block,cols,1)
164+
165+
screen.fill((0,0,0))
166+
drawGrid(screen,rows,cols,x_gap,y_gap,grid_size,game_board)
167+
168+
if block is not None:
169+
draw_block(screen, block, grid_size, x_gap, y_gap)
170+
if not dropBlock(block,rows,cols,game_board) and not game_finished:
171+
score += findLines(rows,cols,game_board)
172+
block = Block(random.randint(5,cols-5),0)
173+
if collides(block,rows,cols,game_board,ny=0):
174+
game_finished = True
175+
176+
text = font.render("Score: " + str(score), True, (255,255,255))
177+
screen.blit(text, [0,0])
178+
if game_finished == True:
179+
screen.blit(finished_text,text_pos)
180+
pygame.display.update()
181+
182+
pygame.quit()
183+
184+
if __name__ == '__main__':
185+
main()

0 commit comments

Comments
 (0)