main.pyimport pygame
import random
pygame.init()
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# Set the width and height of the screen [width, height]
WIDTH = 300
HEIGHT = 600
MARGIN = 30
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
SQUARE_SIZE = 30
# Set the title of the window
pygame.display.set_caption("Tetris")
# Create the board
board = []
for row in range(BOARD_HEIGHT):
board.append([0] * BOARD_WIDTH)
# Define block shapes
shapes = [
[[1, 1],
[1, 1]],
[[0, 2, 0],
[2, 2, 2]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 0, 0],
[4, 4, 4]],
[[0, 0, 5],
[5, 5, 5]],
[[6, 0, 0],
[6, 6, 6]],
[[7, 7, 7, 7]]
]
# Define block colors
colors = [
WHITE,
GREEN,
RED,
BLUE,
YELLOW,
GRAY,
(255, 140, 0)
]
# Define block class
class Block():
def __init__(self, x, y):
self.shape = random.choice(shapes)
self.color = random.choice(colors)
self.x = x
self.y = y
def draw(self, screen):
for row in range(len(self.shape)):
for col in range(len(self.shape[0])):
if self.shape[row][col] > 0:
pygame.draw.rect(screen, self.color, [(self.x + col) * SQUARE_SIZE + MARGIN, (self.y + row) * SQUARE_SIZE + MARGIN, SQUARE_SIZE, SQUARE_SIZE])
def move_down(self):
self.y += 1
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def rotate(self):
self.shape = list(zip(*self.shape[::-1]))
def get_blocks(self):
block_list = []
for row in range(len(self.shape)):
for col in range(len(self.shape[0])):
if self.shape[row][col] > 0:
block_list.append([self.x + col, self.y + row])
return block_list
# Define game functions
def check_collision():
for block in current_blocks:
if not 0 <= block.x < BOARD_WIDTH or not 0 <= block.y < BOARD_HEIGHT or board[block.y][block.x] != 0:
return True
return False
def place_blocks():
for block in current_blocks:
board[block.y][block.x] = block.color
def remove_full_rows():
full_rows = []
for row_idx, row in enumerate(board):
if all(row):
full_rows.append(row_idx)
for row_idx in full_rows:
del board[row_idx]
board.insert(0, [0] * BOARD_WIDTH)
def draw_board(screen):
for row in range(BOARD_HEIGHT):
for col in range(BOARD_WIDTH):
pygame.draw.rect(screen, colors[board[row][col]], [(MARGIN) + col * SQUARE_SIZE, (MARGIN) + row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE])
# Set up the drawing window
screen = pygame.display.set_mode([WIDTH + 2*MARGIN, HEIGHT + 2*MARGIN])
# Set up the clock
clock = pygame.time.Clock()
# Initialize variables
game_over = False
score = 0
current_blocks = [Block(5, 0)]
next_block = Block(5, 0)
fall_time = 0
fall_speed = 0.75
# Game loop
while not game_over:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and all(block.x > 0 for block in current_blocks):
for block in current_blocks:
block.move_left()
if event.key == pygame.K_RIGHT and all(block.x < BOARD_WIDTH - 1 for block in current_blocks):
for block in current_blocks:
block.move_right()
if event.key == pygame.K_DOWN:
fall_speed *= 10
if event.key == pygame.K_UP or event.key == pygame.K_SPACE:
for i in range(3):
if not check_collision():
current_blocks = [Block(block.x, block.y) for block in current_blocks]
for block in current_blocks:
block.rotate()
while not all(0 <= block.x < BOARD_WIDTH for block in current_blocks):
for block in current_blocks:
block.move_right() if block.x < BOARD_WIDTH - 1 else block.move_left()
if event.key == pygame.K_ESCAPE:
game_over = True
# Update
current_time = pygame.time.get_ticks()
if current_time - fall_time > int(fall_speed * 1000):
for block in current_blocks:
block.move_down()
if check_collision():
for block in current_blocks:
block.move_up()
place_blocks()
remove_full_rows()
score += 10
fall_speed = 0.75
current_blocks = [next_block]
next_block = Block(5, 0)
if check_collision():
game_over = True
fall_time = current_time
# Draw
screen.fill(BLACK)
draw_board(screen)
for block in current_blocks:
block.draw(screen)
pygame.draw.rect(screen, GRAY, [(WIDTH/2 + 25), MARGIN, 75, 75])
font = pygame.font.Font(None, 30)
title_text = font.render("Score: " + str(score), True, WHITE)
screen.blit(title_text, [(MARGIN), (MARGIN/2)])
next_text = font.render("Next", True, WHITE)
screen.blit(next_text, [WIDTH/2 + 40, MARGIN/2])
pygame.display.flip()
# Set the framerate
clock.tick(60)
# Quit the game
pygame.quit()
5437 chars201 lines
gistlibby LogSnag