import pygame import random pygame.init() set game window size WIDTH = 800 HEIGHT = 600…
- User
import pygame import random
pygame.init()
set game window size
WIDTH = 800 HEIGHT = 600
set block size
BLOCK_SIZE = 30
set game board width and height
BOARD_WIDTH = WIDTH // BLOCK_SIZE BOARD_HEIGHT = HEIGHT // BLOCK_SIZE
set game colors
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) ORANGE = (255, 165, 0) PURPLE = (128, 0, 128) CYAN = (0, 255, 255)
define game board
board = [[BLACK for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
define block shapes
BLOCKS = [ [[YELLOW, YELLOW], [YELLOW, YELLOW]], [[RED, RED, RED, RED]], [[GREEN, GREEN, 0], [0, GREEN, GREEN]], [[0, BLUE, BLUE], [BLUE, BLUE, 0]], [[ORANGE, ORANGE, ORANGE], [0, 0, ORANGE], [0, 0, 0]], [[0, PURPLE, 0], [PURPLE, PURPLE, PURPLE], [0, 0, 0]], [[0, CYAN, CYAN], [CYAN, CYAN, 0], [0, 0, 0]] ]
define game classes and functions
class Block: def init(self, shape): self.shape = shape self.color = shape[0][0] self.x = BOARD_WIDTH // 2 - 1 self.y = 0
def draw(self, surface): for i in range(len(self.shape)): for j in range(len(self.shape[i])): if self.shape[i][j] != 0: pygame.draw.rect(surface, self.color, ((self.x + j) * BLOCK_SIZE, (self.y + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)) def move_left(self): if not self.wall_collide(-1, 0) and not self.block_collide(-1, 0): self.x -= 1 def move_right(self): if not self.wall_collide(1, 0) and not self.block_collide(1, 0): self.x += 1 def move_down(self): if not self.wall_collide(0, 1) and not self.block_collide(0, 1): self.y += 1 else: self.lock() def wall_collide(self, dx, dy): for i in range(len(self.shape)): for j in range(len(self.shape[i])): if self.shape[i][j] != 0: x = self.x + j + dx y = self.y + i + dy if x < 0 or x >= BOARD_WIDTH or y >= BOARD_HEIGHT: return True return False def block_collide(self, dx, dy): for i in range(len(self.shape)): for j in range(len(self.shape[i])): if self.shape[i][j] != 0: x = self.x + j + dx y = self.y + i + dy if y >= 0 and board[y][x] != BLACK: return True return False def rotate(self): new_shape = [[0 for _ in range(len(self.shape))] for _ in range(len(self.shape[0]))] for i in range(len(self.shape)): for j in range(len(self.shape[i])): new_shape[j][len(self.shape) - i - 1] = self.shape[i][j] if not self.wall_collide(0, 0) and not self.block_collide(0, 0): self.shape = new_shape def lock(self): for i in range(len(self.shape)): for j in range(len(self.shape[i])): if self.shape[i][j] != 0: x = self.x + j y = self.y + i board[y][x] = self.color new_block() def delete_row(self): rows_deleted = 0 for i in range(BOARD_HEIGHT): if BLACK not in board[i]: board.pop(i) board.insert(0, [BLACK for _ in range(BOARD_WIDTH)]) rows_deleted += 1 return rows_deleted - Assistant
The issue is with the
__init__method of theBlockclass. It is missing the double underscore before and after "init", making it an invalid constructor method. Replace the line:def init(self, shape):with:
def __init__(self, shape):This should fix the error.