0% found this document useful (0 votes)
28 views4 pages

Python Paper Minecraft Code

This document contains a Python script that creates a simple 2D game using the Pygame library, resembling Minecraft. It initializes a game window, generates a terrain with varying heights, and allows player movement and interaction with the environment. The player can jump, move left or right, and modify the terrain by clicking on it with the mouse.

Uploaded by

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

Python Paper Minecraft Code

This document contains a Python script that creates a simple 2D game using the Pygame library, resembling Minecraft. It initializes a game window, generates a terrain with varying heights, and allows player movement and interaction with the environment. The player can jump, move left or right, and modify the terrain by clicking on it with the mouse.

Uploaded by

theweason1309
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

pip install pygame


2.
import pygame
import random
import sys

[Link]()

WIDTH, HEIGHT = 900, 600


BLOCK = 30

screen = [Link].set_mode((WIDTH, HEIGHT))


[Link].set_caption("Paper Minecraft Python")

clock = [Link]()

cols = 200
rows = 40

camera_x = 0

# world
world = [[0 for x in range(cols)] for y in range(rows)]

# terrain generation
ground_height = 20

for x in range(cols):

ground_height += [Link](-1,1)

ground_height = max(10,min(30,ground_height))
for y in range(ground_height,rows):

if y == ground_height:
world[y][x] = 2
else:
world[y][x] = 1

# player
player = [Link](200,100,28,28)

vel_y = 0
gravity = 0.5
jump_power = -10
on_ground = False

colors = {
0:(135,206,235),
1:(120,72,32),
2:(50,200,50)
}

running = True

while running:

for event in [Link]():

if [Link] == [Link]:
running = False

if [Link] == [Link]:
mx,my = [Link].get_pos()

gx = int((mx + camera_x)/BLOCK)
gy = int(my/BLOCK)

if 0 <= gx < cols and 0 <= gy < rows:

if [Link] == 1:
world[gy][gx] = 0

if [Link] == 3:
world[gy][gx] = 1

keys = [Link].get_pressed()

if keys[pygame.K_a]:
player.x -= 5

if keys[pygame.K_d]:
player.x += 5

if keys[pygame.K_SPACE] and on_ground:


vel_y = jump_power

vel_y += gravity
player.y += vel_y

on_ground = False

gx = [Link] // BLOCK
gy = [Link] // BLOCK

if 0 <= gx < cols and 0 <= gy < rows:


if world[gy][gx] != 0:
[Link] = gy * BLOCK
vel_y = 0
on_ground = True

camera_x = player.x - WIDTH//2

[Link]((135,206,235))

for y in range(rows):
for x in range(cols):

block = world[y][x]

if block != 0:

[Link](
screen,
colors[block],
(x*BLOCK-camera_x,y*BLOCK,BLOCK,BLOCK)
)

[Link](screen,(255,60,60),(player.x-camera_x,player.y,28,28))

[Link]()
[Link](60)

[Link]()
[Link]()

You might also like