-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.py
133 lines (116 loc) · 4.08 KB
/
Tetris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#install theses libraries
import pygame
import random
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 600
GRID_SIZE = 30
GRID_WIDTH = SCREEN_WIDTH // GRID_SIZE
GRID_HEIGHT = SCREEN_HEIGHT // GRID_SIZE
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Tetris shapes (Tetrominoes)
tetrominoes = [
[[1, 1, 1, 1]], # I
[[1, 1, 1], [0, 1, 0]], # T
[[1, 1, 1], [1, 0, 0]], # L
[[1, 1, 1], [0, 0, 1]], # J
[[1, 1], [1, 1]], # O
[[1, 1, 0], [0, 1, 1]], # Z
[[0, 1, 1], [1, 1, 0]] # S
]
# Initialize game variables
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
grid = [[0] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
current_piece = None
piece_x, piece_y = 0, 0
score = 0
last_move_time = 0
# Functions
def new_piece():
global current_piece, piece_x, piece_y
current_piece = random.choice(tetrominoes)
piece_x = GRID_WIDTH // 2 - len(current_piece[0]) // 2
piece_y = 0
def draw_grid():
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
if grid[y][x] != 0:
pygame.draw.rect(screen, WHITE, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
def draw_piece():
for y in range(len(current_piece)):
for x in range(len(current_piece[y])):
if current_piece[y][x] != 0:
pygame.draw.rect(screen, WHITE, ((piece_x + x) * GRID_SIZE, (piece_y + y) * GRID_SIZE, GRID_SIZE, GRID_SIZE))
def check_collision():
for y in range(len(current_piece)):
for x in range(len(current_piece[y])):
if current_piece[y][x] != 0:
if piece_y + y >= GRID_HEIGHT or piece_x + x < 0 or piece_x + x >= GRID_WIDTH or grid[piece_y + y][piece_x + x] != 0:
return True
return False
def merge_piece():
global grid, current_piece, score, piece_x, piece_y
# Merge the current piece into the grid
for y in range(len(current_piece)):
for x in range(len(current_piece[y])):
if current_piece[y][x] != 0:
grid[piece_y + y][piece_x + x] = 1
# Check for completed lines and clear them
clear_lines = 0
for y in range(GRID_HEIGHT):
if all(grid[y]):
del grid[y]
grid.insert(0, [0] * GRID_WIDTH)
clear_lines += 1
# Update the score based on cleared lines
score += clear_lines * 100
# Main game loop
new_piece()
running = True
while running:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and piece_x > 0:
piece_x -= 1
if check_collision():
piece_x += 1
elif event.key == pygame.K_RIGHT and piece_x < GRID_WIDTH - len(current_piece[0]):
piece_x += 1
if check_collision():
piece_x -= 1
elif event.key == pygame.K_DOWN:
piece_y += 1
if check_collision():
piece_y -= 1
elif event.key == pygame.K_UP:
rotated_piece = [[current_piece[y][x] for y in range(len(current_piece))] for x in range(len(current_piece[0])-1, -1, -1)]
old_piece = current_piece
current_piece = rotated_piece
if check_collision():
current_piece = old_piece
current_time = pygame.time.get_ticks()
if current_time - last_move_time > 500: # Move piece down every 500ms
last_move_time = current_time
piece_y += 1
if check_collision():
piece_y -= 1
merge_piece()
new_piece()
if check_collision():
running = False
draw_grid()
draw_piece()
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {score}", True, WHITE)
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(60) # Adjust speed by changing tick value
pygame.quit()
print(f"Game over! Score: {score}")