Open sirkeygun opened 5 months ago
import sys import pygame import random
pygame.init()
WIDTH, HEIGHT = 400, 600 GRID_SIZE = 30 WHITE = (255, 255, 255)
SHAPES = [ [ ['X', 'X', 'X', 'X'] ], [ ['.', '.', 'X'], ['X', 'X', 'X'] ], [ ['X', '.', '.'], ['X', 'X', 'X'] ], [ ['X', 'X'], ['X', 'X'] ], [ ['.', 'X', 'X'], ['X', 'X', '.'] ], [ ['.', 'X', '.'], ['X', 'X', 'X'] ], [ ['X', 'X', '.'], ['.', 'X', 'X'] ] ]
tetromino_x, tetromino_y = WIDTH // 2, 0 current_tetromino = random.choice(SHAPES) #This line selects a random tetromino shape from the list of predefined shapes (SHAPES).
screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Tetris Game")
grid = [[None] * (WIDTH // GRIDSIZE) for in range(HEIGHT // GRID_SIZE)]
clock = pygame.time.Clock()
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
screen.fill((0, 0, 0)) # Clear the screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Handle user input
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
tetromino_x -= GRID_SIZE
if keys[pygame.K_RIGHT]:
tetromino_x += GRID_SIZE
if keys[pygame.K_DOWN]:
tetromino_y += GRID_SIZE
# Check collision with the bottom or other blocks
if tetromino_y + len(current_tetromino) * GRID_SIZE >= HEIGHT:
# Place the tetromino on the grid
for row in range(len(current_tetromino)):
for col in range(len(current_tetromino[0])):
if current_tetromino[row][col] == 'X':
grid[(tetromino_y + row * GRID_SIZE) // GRID_SIZE][(tetromino_x + col * GRID_SIZE) // GRID_SIZE] = 'X'
# Reset tetromino position
tetromino_x, tetromino_y = WIDTH // 2, 0
current_tetromino = random.choice(SHAPES)
# Draw the current tetromino
for row in range(len(current_tetromino)):
for col in range(len(current_tetromino[0])):
if current_tetromino[row][col] == 'X':
pygame.draw.rect(screen, WHITE, (tetromino_x + col * GRID_SIZE, tetromino_y + row * GRID_SIZE, GRID_SIZE, GRID_SIZE))
# Draw the placed blocks on the grid
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 'X':
pygame.draw.rect(screen, WHITE, (col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE))
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS
# Update the screen
pygame.display.flip()
pygame.quit() sys.exit()
this is not what i had in mind