0% found this document useful (0 votes)
2 views2 pages

minecraft code

This document contains a Python script that uses Pygame to create a simple 2D game resembling Minecraft. It defines a player class, a world class with different block types (grass, stone, water), and includes functionality for generating and drawing the game world. The main loop handles player movement and updates the display accordingly.

Uploaded by

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

minecraft code

This document contains a Python script that uses Pygame to create a simple 2D game resembling Minecraft. It defines a player class, a world class with different block types (grass, stone, water), and includes functionality for generating and drawing the game world. The main loop handles player movement and updates the display accordingly.

Uploaded by

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

import pygame

import sys
import random

# Constants for block types


GRASS = 0
STONE = 1
WATER = 2

# Define colors for blocks


WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
GRAY = (128, 128, 128)
BLUE = (0, 0, 255)

# Define block size and world size


BLOCK_SIZE = 50
WORLD_WIDTH = 10
WORLD_HEIGHT = 10

class Player:
def __init__(self, x, y):
self.x = x
self.y = y

def move(self, dx, dy):


self.x += dx
self.y += dy

class World:
def __init__(self, width, height):
self.width = width
self.height = height
self.grid = [[GRASS for _ in range(height)] for _ in range(width)]

def generate_world(self):
for x in range(self.width):
for y in range(self.height):
if random.random() < 0.2:
self.grid[x][y] = GRASS
elif random.random() < 0.4:
self.grid[x][y] = STONE
elif random.random() < 0.6:
self.grid[x][y] = WATER

def draw_world(screen, world):


for x in range(world.width):
for y in range(world.height):
color = WHITE
if world.grid[x][y] == GRASS:
color = GREEN
elif world.grid[x][y] == STONE:
color = GRAY
elif world.grid[x][y] == WATER:
color = BLUE
pygame.draw.rect(screen, color, (x * BLOCK_SIZE, y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE))

def main():
pygame.init()

screen = pygame.display.set_mode((WORLD_WIDTH * BLOCK_SIZE, WORLD_HEIGHT *


BLOCK_SIZE))
pygame.display.set_caption("Simple Minecraft")

clock = pygame.time.Clock()

world = World(WORLD_WIDTH, WORLD_HEIGHT)


world.generate_world()

player = Player(random.randint(0, WORLD_WIDTH - 1), random.randint(0,


WORLD_HEIGHT - 1))

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

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(-1, 0)
elif keys[pygame.K_RIGHT]:
player.move(1, 0)
elif keys[pygame.K_UP]:
player.move(0, -1)
elif keys[pygame.K_DOWN]:
player.move(0, 1)

screen.fill((0, 0, 0))
draw_world(screen, world)
pygame.draw.rect(screen, (255, 0, 0), (player.x * BLOCK_SIZE, player.y *
BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
pygame.display.flip()

clock.tick(10)

if __name__ == "__main__":
main()

You might also like