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

Simple Minecraft Game in Pygame

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)
66 views2 pages

Simple Minecraft Game in Pygame

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

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):
[Link] = width
[Link] = height
[Link] = [[GRASS for _ in range(height)] for _ in range(width)]

def generate_world(self):
for x in range([Link]):
for y in range([Link]):
if [Link]() < 0.2:
[Link][x][y] = GRASS
elif [Link]() < 0.4:
[Link][x][y] = STONE
elif [Link]() < 0.6:
[Link][x][y] = WATER

def draw_world(screen, world):


for x in range([Link]):
for y in range([Link]):
color = WHITE
if [Link][x][y] == GRASS:
color = GREEN
elif [Link][x][y] == STONE:
color = GRAY
elif [Link][x][y] == WATER:
color = BLUE
[Link](screen, color, (x * BLOCK_SIZE, y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE))

def main():
[Link]()

screen = [Link].set_mode((WORLD_WIDTH * BLOCK_SIZE, WORLD_HEIGHT *


BLOCK_SIZE))
[Link].set_caption("Simple Minecraft")

clock = [Link]()

world = World(WORLD_WIDTH, WORLD_HEIGHT)


world.generate_world()

player = Player([Link](0, WORLD_WIDTH - 1), [Link](0,


WORLD_HEIGHT - 1))

while True:
for event in [Link]():
if [Link] == [Link]:
[Link]()
[Link]()

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

[Link]((0, 0, 0))
draw_world(screen, world)
[Link](screen, (255, 0, 0), (player.x * BLOCK_SIZE, player.y *
BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
[Link]()

[Link](10)

if __name__ == "__main__":
main()

You might also like