Advertisement
EdmundC

tetris

Sep 4th, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Initialize pygame
  5. pygame.init()
  6.  
  7. # Constants
  8. SCREEN_WIDTH, SCREEN_HEIGHT = 300, 600
  9. GRID_SIZE = 30
  10. COLUMNS, ROWS = SCREEN_WIDTH // GRID_SIZE, SCREEN_HEIGHT // GRID_SIZE
  11. WHITE = (255, 255, 255)
  12. BLACK = (0, 0, 0)
  13. FPS = 60
  14. FALL_SPEED = 500  # Tetromino falls every 500ms
  15. CEMENT_DELAY = 100  # Delay of 100ms before cementing
  16.  
  17. # Tetromino shapes
  18. SHAPES = [
  19.     [[1, 1, 1],
  20.      [0, 1, 0]],
  21.    
  22.     [[0, 1, 1],
  23.      [1, 1, 0]],
  24.    
  25.     [[1, 1, 0],
  26.      [0, 1, 1]],
  27.    
  28.     [[1, 1],
  29.      [1, 1]],
  30.    
  31.     [[1, 1, 1, 1]],
  32.    
  33.     [[1, 1, 1],
  34.      [1, 0, 0]],
  35.    
  36.     [[1, 1, 1],
  37.      [0, 0, 1]],
  38. ]
  39.  
  40. class Tetromino:
  41.     def __init__(self):
  42.         self.shape = random.choice(SHAPES)
  43.         self.x = COLUMNS // 2 - len(self.shape[0]) // 2
  44.         self.y = 0
  45.         self.hit_ground_time = None  # Track when the piece first hits the ground
  46.    
  47.     def rotate(self):
  48.         new_shape = [list(row) for row in zip(*self.shape[::-1])]
  49.         if self.can_move(0, 0, new_shape):
  50.             self.shape = new_shape
  51.  
  52.     def can_move(self, dx, dy, shape=None):
  53.         shape = shape or self.shape
  54.         for y, row in enumerate(shape):
  55.             for x, cell in enumerate(row):
  56.                 if cell:
  57.                     new_x = self.x + x + dx
  58.                     new_y = self.y + y + dy
  59.                     if new_x < 0 or new_x >= COLUMNS or new_y >= ROWS or grid[new_y][new_x]:
  60.                         return False
  61.         return True
  62.  
  63.     def move(self, dx, dy):
  64.         if self.can_move(dx, dy):
  65.             self.x += dx
  66.             self.y += dy
  67.             return True
  68.         return False
  69.  
  70.     def cement(self):
  71.         for y, row in enumerate(self.shape):
  72.             for x, cell in enumerate(row):
  73.                 if cell:
  74.                     grid[self.y + y][self.x + x] = 1
  75.  
  76. def create_grid():
  77.     return [[0 for _ in range(COLUMNS)] for _ in range(ROWS)]
  78.  
  79. def draw_grid(screen):
  80.     for y, row in enumerate(grid):
  81.         for x, cell in enumerate(row):
  82.             if cell:
  83.                 pygame.draw.rect(screen, WHITE, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
  84.  
  85. def clear_lines():
  86.     global grid
  87.     new_grid = [row for row in grid if any(cell == 0 for cell in row)]
  88.     lines_cleared = ROWS - len(new_grid)
  89.     for _ in range(lines_cleared):
  90.         new_grid.insert(0, [0] * COLUMNS)
  91.     grid = new_grid
  92.     return lines_cleared
  93.  
  94. def main():
  95.     global grid
  96.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  97.     clock = pygame.time.Clock()
  98.  
  99.     grid = create_grid()
  100.     tetromino = Tetromino()
  101.     fall_time = 0
  102.     cement_time = None  # Timer for cementing the piece
  103.  
  104.     running = True
  105.     while running:
  106.         screen.fill(BLACK)
  107.        
  108.         # Game loop timing
  109.         fall_time += clock.get_rawtime()
  110.         clock.tick(FPS)
  111.  
  112.         # Automatically move the tetromino down based on fall speed
  113.         if fall_time > FALL_SPEED:
  114.             if not tetromino.move(0, 1):  # If it can't move down
  115.                 if cement_time is None:  # Start the cement timer if not already started
  116.                     cement_time = pygame.time.get_ticks()
  117.                 else:
  118.                     # If 100ms have passed since it hit the ground, cement the piece
  119.                     if pygame.time.get_ticks() - cement_time >= CEMENT_DELAY:
  120.                         tetromino.cement()  # Cement the piece
  121.                         clear_lines()  # Clear any full lines
  122.                         tetromino = Tetromino()  # Spawn a new piece
  123.                         cement_time = None  # Reset cement timer
  124.             else:
  125.                 cement_time = None  # Reset if it moved down successfully
  126.  
  127.             fall_time = 0
  128.  
  129.         # Handle events
  130.         for event in pygame.event.get():
  131.             if event.type == pygame.QUIT:
  132.                 running = False
  133.             elif event.type == pygame.KEYDOWN:
  134.                 if event.key == pygame.K_LEFT:
  135.                     tetromino.move(-1, 0)
  136.                 elif event.key == pygame.K_RIGHT:
  137.                     tetromino.move(1, 0)
  138.                 elif event.key == pygame.K_DOWN:
  139.                     tetromino.move(0, 1)
  140.                 elif event.key == pygame.K_UP:
  141.                     tetromino.rotate()
  142.  
  143.         draw_grid(screen)
  144.  
  145.         # Draw current tetromino
  146.         for y, row in enumerate(tetromino.shape):
  147.             for x, cell in enumerate(row):
  148.                 if cell:
  149.                     pygame.draw.rect(screen, WHITE,
  150.                                      ((tetromino.x + x) * GRID_SIZE,
  151.                                       (tetromino.y + y) * GRID_SIZE,
  152.                                       GRID_SIZE, GRID_SIZE), 0)
  153.  
  154.         pygame.display.flip()
  155.  
  156.     pygame.quit()
  157.  
  158. if __name__ == "__main__":
  159.     main()
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement