gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)
sample python code for tetris
0
Download- User
sample python code for tetris
- Assistant
Here is a simple version of Tetris game implemented in Python:
import pygame import random pygame.init() # Constants WIDTH = 600 HEIGHT = 800 ROWS = 20 COLUMNS = 10 # Colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) GREEN = (0, 255, 0) YELLOW = (255, 255, 0) CYAN = (0, 255, 255) MAGENTA = (255, 0, 255) # Set up the game window win = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Tetris") # Set up the game clock clock = pygame.time.Clock() # Define the shapes O_SHAPE = [[1, 1], [1, 1]] I_SHAPE = [[1], [1], [1], [1]] S_SHAPE = [[0, 1, 1], [1, 1, 0]] Z_SHAPE = [[1, 1, 0], [0, 1, 1]] L_SHAPE = [[1, 0], [1, 0], [1, 1]] J_SHAPE = [[0, 1], [0, 1], [1, 1]] T_SHAPE = [[0, 1, 0], [1, 1, 1]] # Store the shapes in a list SHAPES = [O_SHAPE, I_SHAPE, S_SHAPE, Z_SHAPE, L_SHAPE, J_SHAPE, T_SHAPE] # Define the colors of the shapes COLORS = [YELLOW, CYAN, GREEN, RED, BLUE, MAGENTA, WHITE] # Define the size of a block BLOCK_SIZE = 40 # Define the positions of the top-left corners of the game board and the hold box GAME_BOARD_X = 100 GAME_BOARD_Y = 100 HOLD_BOX_X = 100 HOLD_BOX_Y = 500 # Define the font for texts FONT = pygame.font.SysFont('comicsans', 30) class Block: def __init__(self, shape): self.shape = shape self.color = COLORS[SHAPES.index(self.shape)] self.x = COLUMNS // 2 - len(self.shape[0]) // 2 self.y = 0 def move_down(self): self.y += 1 def move_left(self): if self.x > 0: self.x -= 1 def move_right(self): if self.x < COLUMNS - len(self.shape[0]): self.x += 1 def rotate(self): self.shape = [[self.shape[j][i] for j in range(len(self.shape))] for i in range(len(self.shape[0]) - 1, -1, -1)] if self.x > COLUMNS - len(self.shape[0]): self.x = COLUMNS - len(self.shape[0]) if self.y > ROWS - len(self.shape): self.y = ROWS - len(self.shape) def draw(self, x, y): for i in range(len(self.shape)): for j in range(len(self.shape[0])): if self.shape[i][j] == 1: pygame.draw.rect(win, self.color, (x + j * BLOCK_SIZE, y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(win, BLACK, (x + j * BLOCK_SIZE, y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1) class GameBoard: def __init__(self): self.board = [[0 for j in range(COLUMNS)] for i in range(ROWS)] def add_block(self, block): for i in range(len(block.shape)): for j in range(len(block.shape[0])): if block.shape[i][j] == 1: self.board[block.y + i][block.x + j] = block.color def remove_full_rows(self): num_removed = 0 for i in range(ROWS): if all(self.board[i]): self.board.pop(i) self.board.insert(0, [0 for j in range(COLUMNS)]) num_removed += 1 return num_removed def is_game_over(self): return any(self.board[0]) def draw(self): for i in range(ROWS): for j in range(COLUMNS): if self.board[i][j] != 0: pygame.draw.rect(win, self.board[i][j], (GAME_BOARD_X + j * BLOCK_SIZE, GAME_BOARD_Y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(win, BLACK, (GAME_BOARD_X + j * BLOCK_SIZE, GAME_BOARD_Y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1) class HoldBox: def __init__(self): self.block = None def hold(self, block): temp = self.block self.block = block return temp def draw(self): pygame.draw.rect(win, WHITE, (HOLD_BOX_X, HOLD_BOX_Y, 160, 160)) pygame.draw.rect(win, BLACK, (HOLD_BOX_X, HOLD_BOX_Y, 160, 160), 1) if self.block is not None: self.block.draw(HOLD_BOX_X + BLOCK_SIZE * 2, HOLD_BOX_Y + BLOCK_SIZE * 2) def draw_text(text, x, y): text = FONT.render(text, True, BLACK) win.blit(text, (x, y)) def draw_game_over(): draw_text("GAME OVER", WIDTH // 2 - 80, HEIGHT // 2 - 30)) draw_text("Press R to Restart", WIDTH // 2 - 110, HEIGHT // 2 + 10) def draw_stats(num_lines, level): draw_text(f"Lines: {num_lines}", HOLD_BOX_X, HOLD_BOX_Y - 60) draw_text(f"Level: {level}", HOLD_BOX_X, HOLD_BOX_Y - 30) def draw(): win.fill(WHITE) game_board.draw() current_block.draw(GAME_BOARD_X, GAME_BOARD_Y) hold_box.draw() draw_stats(num_lines, level) pygame.display.update() # Initialize the game game_board = GameBoard() hold_box = HoldBox() current_block = None next_block = Block(random.choice(SHAPES)) num_lines = 0 num_lines_to_level_up = 10 level = 1 fall_time = 500 time_since_last_fall = 0 game_over = False # Main game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: quit() if not game_over: if event.key == pygame.K_LEFT: current_block.move_left() elif event.key == pygame.K_RIGHT: current_block.move_right() elif event.key == pygame.K_DOWN: current_block.move_down() num_lines += game_board.remove_full_rows() if current_block.y >= ROWS - len(current_block.shape): current_block.y = ROWS - len(current_block.shape) game_board.add_block(current_block) current_block = next_block next_block = Block(random.choice(SHAPES)) hold_box.block = None time_since_last_fall = 0 elif event.key == pygame.K_UP: current_block.rotate() elif event.key == pygame.K_SPACE: while current_block.y < ROWS - len(current_block.shape): current_block.move_down() num_lines += game_board.remove_full_rows() if current_block.y >= ROWS - len(current_block.shape): current_block.y = ROWS - len(current_block.shape) game_board.add_block(current_block) current_block = next_block next_block = Block(random.choice(SHAPES)) hold_box.block = None time_since_last_fall = 0 elif event.key == pygame.K_c and hold_box.block is not None: current_block, hold_box.block = hold_box.hold(current_block), current_block elif event.key == pygame.K_r: game_board = GameBoard() hold_box = HoldBox() current_block = None next_block = Block(random.choice(SHAPES)) num_lines = 0 level = 1 fall_time = 500 time_since_last_fall = 0 game_over = False # Update game logic if not game_over: time_since_last_fall += clock.tick() if time_since_last_fall > fall_time: current_block.move_down() num_lines += game_board.remove_full_rows() if current_block.y >= ROWS - len(current_block.shape): current_block.y = ROWS - len(current_block.shape) game_board.add_block(current_block) current_block = next_block next_block = Block(random.choice(SHAPES)) hold_box.block = None time_since_last_fall = 0 if num_lines >= num_lines_to_level_up: level += 1 fall_time -= 50 num_lines_to_level_up += 10 if game_board.is_game_over(): game_over = True # Draw the game draw() if game_over: draw_game_over() pygame.display.update()