- import pygame
- import random
- # Initialize Pygame
- pygame.init()
- # Define constants
- SCREEN_WIDTH = 300
- SCREEN_HEIGHT = 600
- BLOCK_SIZE = 30
- BOARD_WIDTH = 10
- BOARD_HEIGHT = 20
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- # Define shapes of Tetrominos
- tetrominos = [
- [[1, 1, 1],
- [0, 1, 0]],
- [[0, 1, 1],
- [1, 1, 0]],
- [[1, 1, 0],
- [0, 1, 1]],
- [[1, 1],
- [1, 1]],
- [[1, 1, 1, 1]],
- [[1, 1, 1],
- [0, 1, 0]]
- ]
- # Define colors for each Tetromino
- colors = [
- (255, 0, 0),
- (0, 255, 0),
- (0, 0, 255),
- (255, 255, 0),
- (255, 0, 255),
- (0, 255, 255)
- ]
- # Function to create a random Tetromino
- def create_tetromino():
- index = random.randint(0, len(tetrominos) - 1)
- shape = tetrominos[index]
- color = colors[index]
- return shape, color
- # Function to draw the Tetromino
- def draw_tetromino(surface, tetromino, position, color):
- for y in range(len(tetromino)):
- for x in range(len(tetromino[y])):
- if tetromino[y][x]:
- pygame.draw.rect(surface, color, (position[0] * BLOCK_SIZE + x * BLOCK_SIZE,
- position[1] * BLOCK_SIZE + y * BLOCK_SIZE,
- BLOCK_SIZE, BLOCK_SIZE))
- # Function to check if a position is valid on the board
- def is_valid_position(board, tetromino, position):
- for y in range(len(tetromino)):
- for x in range(len(tetromino[y])):
- if tetromino[y][x]:
- if position[0] + x < 0 or position[0] + x >= BOARD_WIDTH or \
- position[1] + y >= BOARD_HEIGHT or \
- board[position[1] + y][position[0] + x]:
- return False
- return True
- # Function to merge the Tetromino with the board
- def merge_tetromino(board, tetromino, position):
- for y in range(len(tetromino)):
- for x in range(len(tetromino[y])):
- if tetromino[y][x]:
- board[position[1] + y][position[0] + x] = 1
- # Function to remove completed rows
- def remove_completed_rows(board):
- completed_rows = 0
- new_board = []
- for row in board:
- if 0 not in row:
- completed_rows += 1
- else:
- new_board.append(row)
- new_board = [[0] * BOARD_WIDTH for _ in range(completed_rows)] + new_board
- return new_board, completed_rows
- # Function to draw the board
- def draw_board(surface, board):
- for y in range(BOARD_HEIGHT):
- for x in range(BOARD_WIDTH):
- if board[y][x]:
- pygame.draw.rect(surface, WHITE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
- # Main function
- def main():
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Tetris")
- clock = pygame.time.Clock()
- board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)]
- current_tetromino, current_color = create_tetromino()
- current_position = [4, 0]
- score = 0
- while True:
- screen.fill(BLACK)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- return
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- if is_valid_position(board, current_tetromino, [current_position[0] - 1, current_position[1]]):
- current_position[0] -= 1
- elif event.key == pygame.K_RIGHT:
- if is_valid_position(board, current_tetromino, [current_position[0] + 1, current_position[1]]):
- current_position[0] += 1
- elif event.key == pygame.K_DOWN:
- if is_valid_position(board, current_tetromino, [current_position[0], current_position[1] + 1]):
- current_position[1] += 1
- # Move the Tetromino down
- if is_valid_position(board, current_tetromino, [current_position[0], current_position[1] + 1]):
- current_position[1] += 1
- else:
- merge_tetromino(board, current_tetromino, current_position)
- board, completed_rows = remove_completed_rows(board)
- score += completed_rows * 10
- current_tetromino, current_color = create_tetromino()
- current_position = [4, 0]
- # Draw everything
- draw_board(screen, board)
- draw_tetromino(screen, current_tetromino, current_position, current_color)
- pygame.display.update()
- clock.tick(10) # Adjust the speed of the game
- if __name__ == "__main__":
- main()
[text] monkey
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: