Skip to content

Commit f01ed52

Browse files
authored
Merge pull request #1 from itz-Mathankumar/tetris
Completed Tetris Game
2 parents da00d00 + 1968480 commit f01ed52

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
11
# Tetris-in-Python
2-
Tetris in Python
2+
3+
Tetris is an iconic puzzle game developed by Alexey Pajitnov and released in 1984. The game starts as random coloured letter pieces (geometric shapes) starts falling from the above and can be rotated and aligned before they descend to the bottom and hence the next piece starts falling and goes on.
4+
5+
# Requirements
6+
7+
To run the Tetris game, you'll need the following:
8+
9+
* **Python**: If you don't have Python installed on your computer, you can download it from the official Python website: [python.org/downloads](https://www.python.org/downloads/). Make sure to select the appropriate version for your operating system.
10+
11+
* **PyGame Library**: PyGame is a library that allows you to create games and multimedia applications in Python. You can install it by running the following command in your command line or terminal:
12+
13+
```bash
14+
python -m pip install pygame
15+
```
16+
17+
# Running the Game
18+
19+
To run the Tetris game, follow these steps:
20+
21+
1. Clone the repository from GitHub by opening your command line or terminal and running the following command:
22+
23+
```bash
24+
git clone https://github.com/OpenGenus/Tetris-in-Python
25+
```
26+
27+
2. Change the current directory to the cloned repository:
28+
29+
```bash
30+
cd Tetris-in-Python
31+
```
32+
33+
3. Launch the game by running the Python file:
34+
35+
```bash
36+
python tetris.py
37+
```
38+
39+
Please note that these instructions assume you have Git installed on your system. If you don't have Git, you can also manually download the repository from GitHub by visiting the repository URL ([github.com/OpenGenus/Tetris-in-Python](https://github.com/itz-Mathankumar/Tetris-in-Python)) and clicking on the "Code" button, then selecting "Download ZIP". After extracting the ZIP file, navigate to the extracted directory using the command line or terminal and proceed with step 3 to run the game.

tetris.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import pygame
2+
import random
3+
pygame.init()
4+
5+
BLACK = (0, 0, 0)
6+
WHITE = (255, 255, 255)
7+
RED = (255, 0, 0)
8+
GREEN = (0, 255, 0)
9+
BLUE = (0, 0, 255)
10+
CYAN = (0, 255, 255)
11+
MAGENTA = (255, 0, 255)
12+
YELLOW = (255, 255, 0)
13+
ORANGE = (255, 165, 0)
14+
SCREEN_WIDTH = 800
15+
SCREEN_HEIGHT = 600
16+
GRID_SIZE = 30
17+
GRID_WIDTH = SCREEN_WIDTH // GRID_SIZE
18+
GRID_HEIGHT = SCREEN_HEIGHT // GRID_SIZE
19+
INITIAL_POSITION = (GRID_WIDTH // 2, 0)
20+
PIECES = [
21+
[[1, 1, 1, 1]],
22+
[[1, 1], [1, 1]],
23+
[[1, 1, 0], [0, 1, 1]],
24+
[[0, 1, 1], [1, 1, 0]],
25+
[[1, 1, 1], [0, 1, 0]],
26+
[[1, 1, 1], [0, 0, 1]],
27+
[[1, 1, 1], [1, 0, 0]]
28+
]
29+
30+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
31+
pygame.display.set_caption("Tetris")
32+
clock = pygame.time.Clock()
33+
34+
def draw_grid():
35+
for x in range(0, SCREEN_WIDTH, GRID_SIZE):
36+
pygame.draw.line(screen, WHITE, (x, 0), (x, SCREEN_HEIGHT))
37+
for y in range(0, SCREEN_HEIGHT, GRID_SIZE):
38+
pygame.draw.line(screen, WHITE, (0, y), (SCREEN_WIDTH, y))
39+
40+
def draw_tetromino(tetromino, position):
41+
for y in range(len(tetromino)):
42+
for x in range(len(tetromino[y])):
43+
if tetromino[y][x] == 1:
44+
pygame.draw.rect(screen, tetromino_color, (position[0] * GRID_SIZE + x * GRID_SIZE, position[1] * GRID_SIZE + y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
45+
46+
def check_collision(tetromino, position, grid):
47+
for y in range(len(tetromino)):
48+
for x in range(len(tetromino[y])):
49+
if tetromino[y][x] == 1:
50+
if position[0] + x < 0 or position[0] + x >= GRID_WIDTH or \
51+
position[1] + y >= GRID_HEIGHT or grid[position[1] + y][position[0] + x]:
52+
return True
53+
return False
54+
55+
def merge_tetromino(tetromino, position, grid):
56+
for y in range(len(tetromino)):
57+
for x in range(len(tetromino[y])):
58+
if tetromino[y][x] == 1:
59+
grid[position[1] + y][position[0] + x] = 1
60+
61+
def remove_completed_rows(grid):
62+
completed_rows = []
63+
for y in range(GRID_HEIGHT):
64+
if all(grid[y]):
65+
completed_rows.append(y)
66+
for row in completed_rows:
67+
del grid[row]
68+
grid.insert(0, [0] * GRID_WIDTH)
69+
return len(completed_rows)
70+
71+
grid = [[0] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
72+
tetromino = random.choice(PIECES)
73+
tetromino_color = random.choice([CYAN, YELLOW, MAGENTA, GREEN, RED, BLUE, ORANGE])
74+
position = list(INITIAL_POSITION)
75+
score = 0
76+
start_time = pygame.time.get_ticks()
77+
78+
game_over = False
79+
while not game_over:
80+
for event in pygame.event.get():
81+
if event.type == pygame.QUIT:
82+
game_over = True
83+
84+
if event.type == pygame.KEYDOWN:
85+
if event.key == pygame.K_LEFT:
86+
position[0] -= 1
87+
if check_collision(tetromino, position, grid):
88+
position[0] += 1
89+
elif event.key == pygame.K_RIGHT:
90+
position[0] += 1
91+
if check_collision(tetromino, position, grid):
92+
position[0] -= 1
93+
elif event.key == pygame.K_DOWN:
94+
position[1] += 1
95+
if check_collision(tetromino, position, grid):
96+
position[1] -= 1
97+
elif event.key == pygame.K_UP:
98+
rotated_tetromino = list(zip(*reversed(tetromino)))
99+
if not check_collision(rotated_tetromino, position, grid):
100+
tetromino = rotated_tetromino
101+
102+
position[1] += 1
103+
if check_collision(tetromino, position, grid):
104+
position[1] -= 1
105+
merge_tetromino(tetromino, position, grid)
106+
completed_rows = remove_completed_rows(grid)
107+
score += completed_rows
108+
tetromino = random.choice(PIECES)
109+
tetromino_color = random.choice([CYAN, YELLOW, MAGENTA, GREEN, RED, BLUE, ORANGE])
110+
position = list(INITIAL_POSITION)
111+
if check_collision(tetromino, position, grid):
112+
game_over = True
113+
114+
115+
screen.fill(BLACK)
116+
draw_grid()
117+
draw_tetromino(tetromino, position)
118+
for y in range(GRID_HEIGHT):
119+
for x in range(GRID_WIDTH):
120+
if grid[y][x] == 1:
121+
pygame.draw.rect(screen, WHITE, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
122+
123+
pygame.display.flip()
124+
clock.tick(2)
125+
126+
127+
elapsed_time = (pygame.time.get_ticks() - start_time) / 1000
128+
screen.fill(BLACK)
129+
font = pygame.font.Font(None, 36)
130+
score_text = font.render(f"Score: {score}", True, WHITE)
131+
time_text = font.render(f"Time: {elapsed_time} seconds", True, WHITE)
132+
screen.blit(score_text, (SCREEN_WIDTH // 2 - score_text.get_width() // 2, SCREEN_HEIGHT // 2 - 50))
133+
screen.blit(time_text, (SCREEN_WIDTH // 2 - time_text.get_width() // 2, SCREEN_HEIGHT // 2))
134+
pygame.display.flip()
135+
pygame.time.wait(5000)
136+
pygame.quit()

0 commit comments

Comments
 (0)