0% found this document useful (0 votes)
10 views

minecraft lite

Uploaded by

divyanshp1404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

minecraft lite

Uploaded by

divyanshp1404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import random

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLOCK_SIZE = 40
WORLD_WIDTH = SCREEN_WIDTH // BLOCK_SIZE
WORLD_HEIGHT = SCREEN_HEIGHT // BLOCK_SIZE

# Colors
BACKGROUND_COLOR = (135, 206, 235) # Sky blue background
BLOCK_COLOR = (34, 139, 34) # Grass block color
PLAYER_COLOR = (255, 0, 0) # Red player color

# Initialize pygame
pygame.init()

# Create game window


screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Minecraft Lite")

# Player setup
player_x, player_y = WORLD_WIDTH // 2, WORLD_HEIGHT // 2
player_speed = 2

# World generation (randomly place blocks)


world = [[random.choice([0, 1]) for _ in range(WORLD_WIDTH)] for _ in
range(WORLD_HEIGHT)]

# Function to draw the world


def draw_world():
for y in range(WORLD_HEIGHT):
for x in range(WORLD_WIDTH):
if world[y][x] == 1: # Block exists
pygame.draw.rect(screen, BLOCK_COLOR, (x * BLOCK_SIZE, y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE))

# Function to draw the player


def draw_player(x, y):
pygame.draw.rect(screen, PLAYER_COLOR, (x * BLOCK_SIZE, y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE))

# Function to handle player movement


def move_player(keys):
global player_x, player_y
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= 1
if keys[pygame.K_RIGHT] and player_x < WORLD_WIDTH - 1:
player_x += 1
if keys[pygame.K_UP] and player_y > 0:
player_y -= 1
if keys[pygame.K_DOWN] and player_y < WORLD_HEIGHT - 1:
player_y += 1

# Main game loop


running = True
while running:
screen.fill(BACKGROUND_COLOR)

# Handle events (quitting the game)


for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# Mouse click to break/place blocks
x, y = pygame.mouse.get_pos()
grid_x, grid_y = x // BLOCK_SIZE, y // BLOCK_SIZE
if event.button == 1: # Left-click to break blocks
world[grid_y][grid_x] = 0
elif event.button == 3: # Right-click to place blocks
world[grid_y][grid_x] = 1

# Move player based on key inputs


keys = pygame.key.get_pressed()
move_player(keys)

# Draw the world and player


draw_world()
draw_player(player_x, player_y)

# Update the screen


pygame.display.update()

# Frame rate control


pygame.time.Clock().tick(30)

# Quit pygame
pygame.quit()

You might also like