0% found this document useful (0 votes)
10 views3 pages

Tetris Pyrhon

Sd

Uploaded by

xekipe1273
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Tetris Pyrhon

Sd

Uploaded by

xekipe1273
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random

pygame.init()

WIDTH, HEIGHT = 300, 600


GRID_SIZE = 30
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
fall_speed = 6

SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]]
]

screen = pygame.display.set_mode((WIDTH, HEIGHT))


pygame.display.set_caption("Tetris")

clock = pygame.time.Clock()

def new_block():
shape = random.choice(SHAPES)
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0,
255))
block = {
'shape': shape,
'color': color,
'x': WIDTH // 2 - len(shape[0]) * GRID_SIZE // 2,
'y': 0
}
return block

def draw_block(block):
for row in range(len(block['shape'])):
for col in range(len(block['shape'][0])):
if block['shape'][row][col] == 1:
pygame.draw.rect(screen, block['color'], (block['x'] + col *
GRID_SIZE, block['y'] + row * GRID_SIZE, GRID_SIZE, GRID_SIZE))
pygame.draw.rect(screen, WHITE, (block['x'] + col * GRID_SIZE,
block['y'] + row * GRID_SIZE, GRID_SIZE, GRID_SIZE), 2)

def main():
running = True
current_block = new_block()
grid = [[False] * (WIDTH // GRID_SIZE) for _ in range(HEIGHT // GRID_SIZE)]
block_fallen = False

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and not block_fallen:
if event.key == pygame.K_LEFT and current_block['x'] > 0:
current_block['x'] -= GRID_SIZE
elif event.key == pygame.K_RIGHT and current_block['x'] < WIDTH -
len(current_block['shape'][0]) * GRID_SIZE:
current_block['x'] += GRID_SIZE
elif event.key == pygame.K_DOWN and current_block['y'] < HEIGHT -
len(current_block['shape']) * GRID_SIZE:
current_block['y'] += GRID_SIZE

if not block_fallen:
if current_block['y'] < HEIGHT - len(current_block['shape']) *
GRID_SIZE:
current_block['y'] += fall_speed
else:

for row in range(len(current_block['shape'])):


for col in range(len(current_block['shape'][0])):
if current_block['shape'][row][col] == 1:
grid[(current_block['y'] + row) // GRID_SIZE]
[(current_block['x'] + col) // GRID_SIZE] = True

for row in range(len(grid)):


if all(grid[row]):
del grid[row]
grid.insert(0, [False] * (WIDTH // GRID_SIZE))

block_fallen = True

for row in range(len(grid)):


for col in range(len(grid[0])):
if grid[row][col]:
pygame.draw.rect(screen, current_block['color'], (col *
GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE))
pygame.draw.rect(screen, WHITE, (col * GRID_SIZE, row *
GRID_SIZE, GRID_SIZE, GRID_SIZE), 2)

draw_block(current_block)

pygame.display.flip()

screen.fill(BLACK)

clock.tick(FPS)
pygame.quit()

if __name__ == "__main__":
main()

You might also like