Skip to content

Commit bd8c30a

Browse files
committed
deleted
0 parents  commit bd8c30a

File tree

9 files changed

+267
-0
lines changed

9 files changed

+267
-0
lines changed

main.py

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
import pygame
2+
from pygame.locals import *
3+
from random import *
4+
from math import *
5+
import perlin
6+
import json
7+
import os
8+
9+
FPS = 60
10+
WIDTH, HEIGHT = 600, 300
11+
SCR_DIM = (WIDTH, HEIGHT)
12+
GRAVITY = 0.25
13+
SLIDE = 0.15
14+
TERMINAL_VEL = 18
15+
BLOCK_SIZE = 32
16+
CHUNK_SIZE = 8
17+
BLOCK_TEXTURES_DIR = "res/textures/blocks/"
18+
BLOCK_DATA_DIR = "res/block_data/"
19+
20+
pygame.init()
21+
display = pygame.display.set_mode((WIDTH*2, HEIGHT*2), HWSURFACE | DOUBLEBUF)
22+
screen = pygame.Surface(SCR_DIM)
23+
clock = pygame.time.Clock()
24+
mixer = pygame.mixer.init()
25+
vec = pygame.math.Vector2
26+
noise = perlin.Perlin(randint(0, 1000000))
27+
28+
font = pygame.font.SysFont('Comic Sans MS', 100)
29+
textsurface = font.render('Generating World...', True, (255, 255, 255))
30+
screen.blit(textsurface, (50, 200))
31+
pygame.display.flip()
32+
33+
block_textures = {}
34+
for img in os.listdir(BLOCK_TEXTURES_DIR):
35+
block_textures[img[:-4]] = pygame.image.load(BLOCK_TEXTURES_DIR+img).convert_alpha()
36+
for image in block_textures:
37+
block_textures[image] = pygame.transform.scale(block_textures[image], (BLOCK_SIZE, BLOCK_SIZE))
38+
block_data = {}
39+
for j in os.listdir(BLOCK_DATA_DIR):
40+
block_data[j[:-5]] = json.loads(open(BLOCK_DATA_DIR+j, "r").read())
41+
42+
def intv(vector):
43+
return vec(int(vector.x), int(vector.y))
44+
45+
def block_collide(ax, ay, width, height, b):
46+
a_rect = pygame.Rect(ax-camera.offset.x, ay-camera.offset.y, width, height)
47+
b_rect = pygame.Rect(b.pos.x-camera.offset.x, b.pos.y-camera.offset.y, BLOCK_SIZE, BLOCK_SIZE)
48+
if a_rect.colliderect(b_rect):
49+
return True
50+
return False
51+
52+
class Camera(pygame.sprite.Sprite):
53+
def __init__(self, master):
54+
self.master = master
55+
self.actual_offset = self.master.size / 2
56+
self.offset = intv(self.actual_offset)
57+
self.actual_offset = self.master.pos - self.offset - vec(SCR_DIM) / 2 + self.master.size / 2
58+
59+
def update(self):
60+
tick_offset = self.master.pos - self.offset - vec(SCR_DIM) / 2 + self.master.size / 2
61+
if -1 < tick_offset.x < 1:
62+
tick_offset.x = 0
63+
if -1 < tick_offset.y < 1:
64+
tick_offset.y = 0
65+
self.actual_offset += tick_offset / 10
66+
self.offset = intv(self.actual_offset)
67+
68+
class Player(pygame.sprite.Sprite):
69+
def __init__(self):
70+
pygame.sprite.Sprite.__init__(self)
71+
self.image = pygame.Surface((0.6*BLOCK_SIZE, 1.8*BLOCK_SIZE))
72+
self.size = vec(self.image.get_size())
73+
self.width, self.height = self.size.x, self.size.y
74+
self.image.fill((40, 40, 40))
75+
self.start_pos = vec(0, 0) * BLOCK_SIZE
76+
self.pos = vec(self.start_pos)
77+
self.vel = vec(0, 0)
78+
self.max_speed = 2.6
79+
self.jumping_max_speed = 3.2
80+
self.rect = self.image.get_rect()
81+
self.bottom_bar = pygame.Rect((self.rect.x+1, self.rect.bottom), (self.width-2, 1))
82+
self.on_ground = False
83+
84+
def update(self):
85+
keys = pygame.key.get_pressed()
86+
if keys[K_a]:
87+
if self.vel.x > -self.max_speed:
88+
self.vel.x -= SLIDE
89+
elif self.vel.x < 0:
90+
self.vel.x += SLIDE
91+
if keys[K_d]:
92+
if self.vel.x < self.max_speed:
93+
self.vel.x += SLIDE
94+
elif self.vel.x > 0:
95+
self.vel.x -= SLIDE
96+
if keys[K_w] and self.on_ground:
97+
self.vel.y = -4.5
98+
self.vel.x *= 1.1
99+
if self.vel.x > self.jumping_max_speed:
100+
self.vel.x = self.jumping_max_speed
101+
elif self.vel.x < -self.jumping_max_speed:
102+
self.vel.x = -self.jumping_max_speed
103+
if -SLIDE < self.vel.x < SLIDE:
104+
self.vel.x = 0
105+
106+
if not self.on_ground:
107+
self.vel.y += GRAVITY
108+
if self.vel.y > TERMINAL_VEL:
109+
self.vel.y = TERMINAL_VEL
110+
self.move()
111+
self.bottom_bar = pygame.Rect((self.rect.left+1, self.rect.bottom), (self.width-2, 1))
112+
for block in blocks:
113+
if self.bottom_bar.colliderect(blocks[block].rect):
114+
self.on_ground = True
115+
break
116+
else:
117+
self.on_ground = False
118+
self.rect.topleft = self.pos - camera.offset
119+
120+
def draw(self, screen):
121+
screen.blit(self.image, self.rect.topleft)
122+
123+
def move(self):
124+
for b in blocks:
125+
block = blocks[b]
126+
if block.data["collision_box"] == "full":
127+
if block_collide(self.pos.x+self.vel.x, self.pos.y, self.width, self.height, block):
128+
if self.vel.x < 0:
129+
self.pos.x -= self.pos.x - (block.pos.x + BLOCK_SIZE)
130+
elif self.vel.x >= 0:
131+
self.pos.x += block.pos.x - (self.pos.x + self.width)
132+
self.vel.x = 0
133+
if block_collide(self.pos.x, self.pos.y+self.vel.y, self.width, self.height, block):
134+
if self.vel.y < 0:
135+
self.pos.y -= self.pos.y - (block.pos.y + BLOCK_SIZE)
136+
elif self.vel.y >= 0:
137+
self.pos.y += block.pos.y - (self.pos.y + self.height)
138+
self.vel.y = 0
139+
self.pos += self.vel
140+
141+
class Block(pygame.sprite.Sprite):
142+
def __init__(self, pos, name):
143+
pygame.sprite.Sprite.__init__(self)
144+
blocks[tuple(pos)] = self
145+
self.name = name
146+
self.data = block_data[self.name]
147+
self.pos = vec(pos) * BLOCK_SIZE
148+
self.image = block_textures[self.name]
149+
if self.data["collision_box"] == "full":
150+
self.rect = pygame.Rect(self.pos, (BLOCK_SIZE, BLOCK_SIZE))
151+
elif self.data["collision_box"] == "none":
152+
self.rect = pygame.Rect(self.pos, (0, 0))
153+
154+
def update(self):
155+
self.rect.topleft = self.pos - camera.offset
156+
157+
def draw(self, screen):
158+
screen.blit(self.image, self.rect.topleft)
159+
160+
class Chunk(object):
161+
def __init__(self, pos):
162+
self.block_data = generate_chunk(pos[0], pos[1])
163+
self.blocks = {}
164+
for block in self.block_data:
165+
self.blocks[block] = Block(block, self.block_data[block])
166+
167+
def render(self):
168+
for block in self.blocks:
169+
for chunk in rendered_chunks:
170+
if block in chunks[chunk].block_data:
171+
self.blocks[block] = Block(block, chunks[chunk].block_data[block])
172+
self.blocks[block].update()
173+
self.blocks[block].draw(screen)
174+
175+
def generate_chunk(x, y):
176+
chunk_data = {}
177+
chunk_data_pos_only = {}
178+
for y_pos in range(CHUNK_SIZE):
179+
for x_pos in range(CHUNK_SIZE):
180+
target_x = x * CHUNK_SIZE + x_pos
181+
target_y = y * CHUNK_SIZE + y_pos
182+
block_name = ""
183+
height = int(noise.one(target_x * 0.8))
184+
if target_y == 5-height:
185+
block_name = "grass_block"
186+
elif 5-height < target_y < 10-height:
187+
block_name = "dirt"
188+
elif target_y >= 10-height:
189+
block_name = "stone"
190+
elif target_y == 4-height:
191+
if randint(0, 2) == 0:
192+
block_name = "grass"
193+
if block_name != "":
194+
chunk_data[(target_x, target_y)] = block_name
195+
return chunk_data
196+
197+
blocks = {}
198+
chunks = {}
199+
player = Player()
200+
camera = Camera(player)
201+
202+
def draw():
203+
screen.fill((135, 206, 250))
204+
for chunk in rendered_chunks:
205+
chunks[chunk].render()
206+
player.draw(screen)
207+
display.blit(pygame.transform.scale(screen, (WIDTH*2, HEIGHT*2)), (0, 0))
208+
pygame.display.flip()
209+
210+
running = True
211+
while running:
212+
clock.tick(FPS)
213+
try:
214+
pygame.display.set_caption(f"FPS: {round(clock.get_fps(), 1)} #blocks: {len(blocks)} #chunks: {len(chunks)}")
215+
except:
216+
pass
217+
218+
for event in pygame.event.get():
219+
if event.type == QUIT:
220+
running = False
221+
222+
rendered_chunks = {}
223+
for y in range(ceil(HEIGHT/(CHUNK_SIZE*BLOCK_SIZE)+1)):
224+
for x in range(ceil(WIDTH/(CHUNK_SIZE*BLOCK_SIZE)+1)):
225+
chunk = (
226+
x - 1 + int(round(camera.offset.x / (CHUNK_SIZE * BLOCK_SIZE))),
227+
y - 1 + int(round(camera.offset.y / (CHUNK_SIZE * BLOCK_SIZE)))
228+
)
229+
rendered_chunks[chunk] = None
230+
if chunk not in chunks:
231+
chunks[chunk] = Chunk(chunk)
232+
unrendered_chunks = {}
233+
for chunk in chunks:
234+
if chunk not in rendered_chunks:
235+
unrendered_chunks[chunk] = None
236+
for chunk in unrendered_chunks:
237+
for block in chunks[chunk].block_data:
238+
if block in blocks:
239+
blocks[block].kill()
240+
del blocks[block]
241+
242+
camera.update()
243+
player.update()
244+
draw()
245+
246+
pygame.quit()
247+
quit()

res/block_data/dirt.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "dirt",
3+
"collision_box": "full",
4+
"bounding_box": "full"
5+
}

res/block_data/grass.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "grass",
3+
"collision_box": "none",
4+
"bounding_box": "full"
5+
}

res/block_data/grass_block.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "grass_block",
3+
"collision_box": "full",
4+
"bounding_box": "full"
5+
}

res/block_data/stone.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "stone",
3+
"collision_box": "full",
4+
"bounding_box": "full"
5+
}

res/textures/blocks/dirt.png

1.12 KB
Loading

res/textures/blocks/grass.png

1.16 KB
Loading

res/textures/blocks/grass_block.png

1.21 KB
Loading

res/textures/blocks/stone.png

501 Bytes
Loading

0 commit comments

Comments
 (0)