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()