[text] monkey

Viewer

  1. import pygame
  2. import random
  3.  
  4. # Initialize Pygame
  5. pygame.init()
  6.  
  7. # Define constants
  8. SCREEN_WIDTH = 300
  9. SCREEN_HEIGHT = 600
  10. BLOCK_SIZE = 30
  11. BOARD_WIDTH = 10
  12. BOARD_HEIGHT = 20
  13. WHITE = (255, 255, 255)
  14. BLACK = (0, 0, 0)
  15.  
  16. # Define shapes of Tetrominos
  17. tetrominos = [
  18.     [[1, 1, 1],
  19.      [0, 1, 0]],
  20.  
  21.     [[0, 1, 1],
  22.      [1, 1, 0]],
  23.  
  24.     [[1, 1, 0],
  25.      [0, 1, 1]],
  26.  
  27.     [[1, 1],
  28.      [1, 1]],
  29.  
  30.     [[1, 1, 1, 1]],
  31.  
  32.     [[1, 1, 1],
  33.      [0, 1, 0]]
  34. ]
  35.  
  36. # Define colors for each Tetromino
  37. colors = [
  38.     (255, 0, 0),
  39.     (0, 255, 0),
  40.     (0, 0, 255),
  41.     (255, 255, 0),
  42.     (255, 0, 255),
  43.     (0, 255, 255)
  44. ]
  45.  
  46. # Function to create a random Tetromino
  47. def create_tetromino():
  48.     index = random.randint(0, len(tetrominos) - 1)
  49.     shape = tetrominos[index]
  50.     color = colors[index]
  51.     return shape, color
  52.  
  53. # Function to draw the Tetromino
  54. def draw_tetromino(surface, tetromino, position, color):
  55.     for y in range(len(tetromino)):
  56.         for x in range(len(tetromino[y])):
  57.             if tetromino[y][x]:
  58.                 pygame.draw.rect(surface, color, (position[0] * BLOCK_SIZE + x * BLOCK_SIZE,
  59.                                                    position[1] * BLOCK_SIZE + y * BLOCK_SIZE,
  60.                                                    BLOCK_SIZE, BLOCK_SIZE))
  61.  
  62. # Function to check if a position is valid on the board
  63. def is_valid_position(board, tetromino, position):
  64.     for y in range(len(tetromino)):
  65.         for x in range(len(tetromino[y])):
  66.             if tetromino[y][x]:
  67.                 if position[0] + x < 0 or position[0] + x >= BOARD_WIDTH or \
  68.                    position[1] + y >= BOARD_HEIGHT or \
  69.                    board[position[1] + y][position[0] + x]:
  70.                     return False
  71.     return True
  72.  
  73. # Function to merge the Tetromino with the board
  74. def merge_tetromino(board, tetromino, position):
  75.     for y in range(len(tetromino)):
  76.         for x in range(len(tetromino[y])):
  77.             if tetromino[y][x]:
  78.                 board[position[1] + y][position[0] + x] = 1
  79.  
  80. # Function to remove completed rows
  81. def remove_completed_rows(board):
  82.     completed_rows = 0
  83.     new_board = []
  84.     for row in board:
  85.         if 0 not in row:
  86.             completed_rows += 1
  87.         else:
  88.             new_board.append(row)
  89.     new_board = [[0] * BOARD_WIDTH for _ in range(completed_rows)] + new_board
  90.     return new_board, completed_rows
  91.  
  92. # Function to draw the board
  93. def draw_board(surface, board):
  94.     for y in range(BOARD_HEIGHT):
  95.         for x in range(BOARD_WIDTH):
  96.             if board[y][x]:
  97.                 pygame.draw.rect(surface, WHITE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
  98.  
  99. # Main function
  100. def main():
  101.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  102.     pygame.display.set_caption("Tetris")
  103.     clock = pygame.time.Clock()
  104.  
  105.     board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)]
  106.     current_tetromino, current_color = create_tetromino()
  107.     current_position = [4, 0]
  108.     score = 0
  109.  
  110.     while True:
  111.         screen.fill(BLACK)
  112.  
  113.         for event in pygame.event.get():
  114.             if event.type == pygame.QUIT:
  115.                 pygame.quit()
  116.                 return
  117.  
  118.             if event.type == pygame.KEYDOWN:
  119.                 if event.key == pygame.K_LEFT:
  120.                     if is_valid_position(board, current_tetromino, [current_position[0] - 1, current_position[1]]):
  121.                         current_position[0] -= 1
  122.                 elif event.key == pygame.K_RIGHT:
  123.                     if is_valid_position(board, current_tetromino, [current_position[0] + 1, current_position[1]]):
  124.                         current_position[0] += 1
  125.                 elif event.key == pygame.K_DOWN:
  126.                     if is_valid_position(board, current_tetromino, [current_position[0], current_position[1] + 1]):
  127.                         current_position[1] += 1
  128.  
  129.         # Move the Tetromino down
  130.         if is_valid_position(board, current_tetromino, [current_position[0], current_position[1] + 1]):
  131.             current_position[1] += 1
  132.         else:
  133.             merge_tetromino(board, current_tetromino, current_position)
  134.             board, completed_rows = remove_completed_rows(board)
  135.             score += completed_rows * 10
  136.             current_tetromino, current_color = create_tetromino()
  137.             current_position = [4, 0]
  138.  
  139.         # Draw everything
  140.         draw_board(screen, board)
  141.         draw_tetromino(screen, current_tetromino, current_position, current_color)
  142.  
  143.         pygame.display.update()
  144.         clock.tick(10)  # Adjust the speed of the game
  145.  
  146. if __name__ == "__main__":
  147.     main()
  148.  

Editor

You can edit this paste and save as new:


File Description
  • monkey
  • Paste Code
  • 05 May-2024
  • 4.6 Kb
You can Share it: