Can anyone spot what's wrong with my code it won't work I've gone through it at least 20 times

import pygame
import sys

— CONFIG —

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLOCK_SIZE = 40
WORLD_WIDTH = 20
WORLD_HEIGHT = 15

Colors

SKY = (135, 206, 235)
GRASS = (34, 139, 34)
DIRT = (139, 69, 19)
STONE = (100, 100, 100)
PLAYER_COLOR = (255, 0, 0)

Block IDs

AIR = 0
GRASS_BLOCK = 1
DIRT_BLOCK = 2
STONE_BLOCK = 3

BLOCK_COLORS = {
GRASS_BLOCK: GRASS,
DIRT_BLOCK: DIRT,
STONE_BLOCK: STONE
}

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(“Mini Minecraft”)
clock = pygame.time.Clock()

— WORLD GENERATION —

world = [[AIR for _ in range(WORLD_WIDTH)] for _ in range(WORLD_HEIGHT)]

for y in range(WORLD_HEIGHT):
for x in range(WORLD_WIDTH):
if y > 10:
world[y] = STONE_BLOCK
elif y > 8:
world[y] = DIRT_BLOCK
elif y == 8:
world[y] = GRASS_BLOCK

Player

player_x = 5
player_y = 5

— MAIN LOOP —

while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

    # Mouse: break / place blocks
    if event.type == pygame.MOUSEBUTTONDOWN:
        mx, my = pygame.mouse.get_pos()
        gx = mx // BLOCK_SIZE
        gy = my // BLOCK_SIZE

        if 0 <= gx < WORLD_WIDTH and 0 <= gy < WORLD_HEIGHT:
            if event.button == 1:  # Left click breaks
                world[gy][gx] = AIR
            elif event.button == 3:  # Right click places dirt
                world[gy][gx] = DIRT_BLOCK

# Movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
    player_x -= 0.1
if keys[pygame.K_d]:
    player_x += 0.1
if keys[pygame.K_w]:
    player_y -= 0.1
if keys[pygame.K_s]:
    player_y += 0.1

screen.fill(SKY)

# Draw world
for y in range(WORLD_HEIG
HT):import pygame
import sys

# --- CONFIG ---
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLOCK_SIZE = 40
WORLD_WIDTH = 20
WORLD_HEIGHT = 15

# Colors
SKY = (135, 206, 235)
GRASS = (34, 139, 34)
DIRT = (139, 69, 19)
STONE = (100, 100, 100)
PLAYER_COLOR = (255, 0, 0)

# Block IDs
AIR = 0
GRASS_BLOCK = 1
DIRT_BLOCK = 2
STONE_BLOCK = 3

BLOCK_COLORS = {
    GRASS_BLOCK: GRASS,
    DIRT_BLOCK: DIRT,
    STONE_BLOCK: STONE
}

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Mini Minecraft")
clock = pygame.time.Clock()

# --- WORLD GENERATION ---
world = [[AIR for _ in range(WORLD_WIDTH)] for _ in range(WORLD_HEIGHT)]

for y in range(WORLD_HEIGHT):
    for x in range(WORLD_WIDTH):
        if y > 10:
            world[y][x] = STONE_BLOCK
        elif y > 8:
            world[y][x] = DIRT_BLOCK
        elif y == 8:
            world[y][x] = GRASS_BLOCK

# Player
player_x = 5
player_y = 5

# --- MAIN LOOP ---
while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # Mouse: break / place blocks
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = pygame.mouse.get_pos()
            gx = mx // BLOCK_SIZE
            gy = my // BLOCK_SIZE

            if 0 <= gx < WORLD_WIDTH and 0 <= gy < WORLD_HEIGHT:
                if event.button == 1:  # Left click breaks
                    world[gy][gx] = AIR
                elif event.button == 3:  # Right click places dirt
                    world[gy][gx] = DIRT_BLOCK

    # Movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        player_x -= 0.1
    if keys[pygame.K_d]:
        player_x += 0.1
    if keys[pygame.K_w]:
        player_y -= 0.1
    if keys[pygame.K_s]:
        player_y += 0.1

    screen.fill(SKY)

    # Draw world
    for y in range(WORLD_HEIGHT):
        for x in range(WORLD_WIDTH):
            block = world[y][x]
            if block != AIR:
                pygame.draw.rect(
                    screen,
                    BLOCK_COLORS[block],
                    (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
                )

    # Draw player
    pygame.draw.rect(
        screen,
        PLAYER_COLOR,
        (int(player_x * BLOCK_SIZE), int(player_y * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE)
    )

    pygame.display.flip()

    for x in range(WORLD_WIDTH):
        block = world[y][x]
        if block != AIR:
            pygame.draw.rect(
                screen,
                BLOCK_COLORS[block],
                (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
            )

# Draw player
pygame.draw.rect(
    screen,
    PLAYER_COLOR,
    (int(player_x * BLOCK_SIZE), int(player_y * BLOCK_SIZE), BLOCK_SIZE, BLOCK_SIZE)
)

pygame.display.flip()

You just dumped your code without stating what it is that is not working. The way it works is that you provide a specific description of the issue that you are experiencing that is out of character with the expected output.

However, at a passing glance, why would you import a library multiple times as per the for loop highlighted here.

Please repaste the ENTIRE script so that it is shown as the bottom half. This will help to analyze to see if the script is following the correct syntax, indentation, etc.

The indentation in the code has been lost, but if the line above really is dedented to the left most level, the code will always call sys.exit.

I suppose the loop with imports is due to the script likely being LLM generated, which it looks like to me.

@po6 if this code is LLM generated, why don’t ask the LLM to fix it. If it is not, and I am wrong (which is unlikely IMHO), it would still benefit everyone if you’d paste the code formated, and state the issues you are experiencing, as @onePythonUser mentioned.