Mostly implement map generator
parent
a99f81de0c
commit
0acd41eacc
|
@ -5,7 +5,7 @@ from .map import Map
|
|||
from .player import Player
|
||||
|
||||
# Constants
|
||||
SCALE = 4
|
||||
SCALE = 1
|
||||
METER = 32
|
||||
FPS = 60
|
||||
|
||||
|
@ -36,6 +36,8 @@ player.sprite.set_rect((0, 0, 16, 24))
|
|||
player.sprite.texture.set_animation(0, 0, 0)
|
||||
player.set_pos(1.1, 1.1)
|
||||
|
||||
player.set_pos(map.generator.rooms[0].x, map.generator.rooms[0].y)
|
||||
|
||||
CENTER = [winsize[0] / 2, winsize[1] / 2]
|
||||
|
||||
# Mainloop
|
||||
|
|
251
src/generator.py
251
src/generator.py
|
@ -1,5 +1,8 @@
|
|||
import random
|
||||
import time
|
||||
import math
|
||||
|
||||
MSIZE = 80
|
||||
|
||||
def rand(*args):
|
||||
random.seed(time.clock())
|
||||
|
@ -17,60 +20,222 @@ class Room():
|
|||
self.top = y
|
||||
self.bottom = y + height
|
||||
|
||||
def generate():
|
||||
self.cx = math.floor(self.x + (width / 2))
|
||||
self.cy = math.floor(self.y + (height / 2))
|
||||
|
||||
def intersects(self, other):
|
||||
return self.left <= other.right and self.right >= other.left and self.top <= other.bottom and self.bottom >= other.top
|
||||
|
||||
class Generator():
|
||||
rooms = []
|
||||
for _ in range(rand(4, 15)):
|
||||
while True:
|
||||
board = []
|
||||
|
||||
def __init__(self, width, height = None):
|
||||
self.width = width
|
||||
self.height = height or width
|
||||
|
||||
for y in range(self.height):
|
||||
self.board.append([])
|
||||
for _ in range(self.width):
|
||||
self.board[y].append(0)
|
||||
|
||||
self.place_rooms()
|
||||
self.place_corridors()
|
||||
|
||||
def place_rooms(self):
|
||||
for _ in range(rand(4, 15)):
|
||||
width = rand(6, 16)
|
||||
height = rand(6, 16)
|
||||
x = rand(0, 60)
|
||||
y = rand(0, 60)
|
||||
|
||||
overlaps = False
|
||||
|
||||
room1 = Room(x, y, width, height)
|
||||
for room2 in rooms:
|
||||
if room1.right >= room2.left and room1.left <= room1.right and \
|
||||
room1.bottom >= room2.top and room1.top <= room2.bottom:
|
||||
overlaps = True
|
||||
if x + width > self.width:
|
||||
x = self.width - width
|
||||
|
||||
if not overlaps:
|
||||
rooms.append(room1)
|
||||
break
|
||||
if y + height > self.height:
|
||||
y = self.height - height
|
||||
|
||||
collides = False
|
||||
room = Room(x, y, width, height)
|
||||
|
||||
for other_room in self.rooms:
|
||||
if room.intersects(other_room):
|
||||
collides = True
|
||||
break
|
||||
|
||||
if not collides:
|
||||
self.place_room(room)
|
||||
|
||||
return rooms
|
||||
def place_room(self, room):
|
||||
for row in range(room.height):
|
||||
for col in range(room.width):
|
||||
y = room.y + row
|
||||
x = room.x + col
|
||||
|
||||
self.board[y][x] = 1
|
||||
|
||||
self.rooms.append(room)
|
||||
|
||||
def place_corridors(self):
|
||||
for i in range(0, len(self.rooms) - 1):
|
||||
room1 = self.rooms[i]
|
||||
room2 = self.rooms[i + 1]
|
||||
|
||||
if rand(0, 2) == 0:
|
||||
if room1.cx <= room2.cx:
|
||||
self.horiz_corridor(room1.cx, room2.cx, room1.cy)
|
||||
else:
|
||||
self.horiz_corridor(room2.cx, room1.cx, room1.cy)
|
||||
if room1.cy <= room2.cy:
|
||||
self.vert_corridor(room1.cy, room2.cy, room2.cx)
|
||||
else:
|
||||
self.vert_corridor(room2.cy, room1.cy, room2.cx)
|
||||
else:
|
||||
if room1.cy <= room2.cy:
|
||||
self.vert_corridor(room1.cy, room2.cy, room2.cx)
|
||||
else:
|
||||
self.vert_corridor(room2.cy, room1.cy, room2.cx)
|
||||
if room1.cx <= room2.cx:
|
||||
self.horiz_corridor(room1.cx, room2.cx, room1.cy)
|
||||
else:
|
||||
self.horiz_corridor(room2.cx, room1.cx, room1.cy)
|
||||
|
||||
def horiz_corridor(self, x1, x2, y):
|
||||
for row in range(y - 1, y + 2):
|
||||
for col in range(x1 - 1, x2 + 2):
|
||||
self.board[row][col] = 1
|
||||
|
||||
def vert_corridor(self, y1, y2, x):
|
||||
for row in range(y1, y2 + 2):
|
||||
for col in range(x - 1, x + 2):
|
||||
self.board[row][col] = 1
|
||||
|
||||
def get_map(self):
|
||||
map = {
|
||||
"tiles": [
|
||||
"floor_cobble.png",
|
||||
"wall_cobble_down.png",
|
||||
"wall_cobble_right.png",
|
||||
"wall_cobble_up.png",
|
||||
"wall_cobble_left.png",
|
||||
"wall_cobble_corner_nw_inner.png",
|
||||
"wall_cobble_corner_ne_inner.png",
|
||||
"wall_cobble_corner_se_inner.png",
|
||||
"wall_cobble_corner_sw_inner.png",
|
||||
"wall_cobble_corner_nw_outer.png",
|
||||
"wall_cobble_corner_ne_outer.png",
|
||||
"wall_cobble_corner_se_outer.png",
|
||||
"wall_cobble_corner_sw_outer.png"
|
||||
],
|
||||
"renderLayers": []
|
||||
}
|
||||
|
||||
layer1 = self.board.copy()
|
||||
map["renderLayers"].append(layer1)
|
||||
|
||||
layer2 = self.board.copy()
|
||||
for y in range(1, self.height - 1):
|
||||
for x in range(1, self.width - 1):
|
||||
if self.board[y][x] == 1:
|
||||
tile = 1
|
||||
if self.board[y][x - 1] == 1 and self.board[y][x + 1] == 1:
|
||||
if self.board[y - 1][x] == 0:
|
||||
tile = 2
|
||||
elif self.board[y + 1][x] == 0:
|
||||
tile = 4
|
||||
|
||||
if self.board[y - 1][x] == 1 and self.board[y + 1][x] == 1:
|
||||
if self.board[y][x - 1] == 0:
|
||||
tile = 3
|
||||
elif self.board[y][x + 1] == 0:
|
||||
tile = 5
|
||||
|
||||
layer2[y][x] = tile
|
||||
|
||||
# for y in range(1, len(layer2) - 1):
|
||||
# for x in range(1, len(layer2[y]) - 1):
|
||||
# if layer2[y][x] == 0:
|
||||
# tile = 0
|
||||
# if layer2[y - 1][x] != 0 and layer2[y][x - 1] == 0:
|
||||
# tile = 8
|
||||
# elif layer2[y - 1][x] != 0 and layer2[y][x + 1] != 0:
|
||||
# tile = 9
|
||||
# elif layer2[y + 1][x] != 0 and layer2[y][x - 1] != 0:
|
||||
# tile = 7
|
||||
# elif layer2[y + 1][x] != 0 and layer2[y][x + 1] != 0:
|
||||
# tile = 6
|
||||
|
||||
# layer2[y][x] = tile
|
||||
|
||||
map["renderLayers"].append(layer2)
|
||||
|
||||
bounds = []
|
||||
for y in range(self.height):
|
||||
bounds.append([])
|
||||
for _ in range(self.width):
|
||||
bounds[y].append(0)
|
||||
map["boundaries"] = bounds
|
||||
|
||||
return map
|
||||
|
||||
# Drawing
|
||||
if __name__ == "__main__":
|
||||
rooms = generate()
|
||||
map = []
|
||||
gen = Generator(80)
|
||||
|
||||
for y in range(80):
|
||||
map.append([])
|
||||
for _ in range(80):
|
||||
map[y].append('.')
|
||||
|
||||
for room in rooms:
|
||||
print(room.x, room.y, room.width, room.height)
|
||||
|
||||
for room in rooms:
|
||||
for w in range(room.width):
|
||||
map[room.y][room.x + w] = "═"
|
||||
map[room.y + room.height - 1][room.x + w] = "═"
|
||||
|
||||
for h in range(room.height):
|
||||
map[room.y + h][room.x] = "║"
|
||||
map[room.y + h][room.x + room.width - 1] = "║"
|
||||
|
||||
for h in range(room.height - 2):
|
||||
for w in range(room.width - 2):
|
||||
map[room.y + h + 1][room.x + w + 1] = " "
|
||||
|
||||
map[room.y][room.x] = "╔"
|
||||
map[room.y][room.x + room.width - 1] = "╗"
|
||||
map[room.y + room.height - 1][room.x] = "╚"
|
||||
map[room.y + room.height - 1][room.x + room.width - 1] = "╝"
|
||||
draw = []
|
||||
for y in range(gen.height):
|
||||
draw.append([])
|
||||
for _ in range(gen.width):
|
||||
draw[y].append(' ')
|
||||
|
||||
for y in map:
|
||||
print(''.join(y))
|
||||
for y in range(len(gen.board)):
|
||||
for x in range(len(gen.board[y])):
|
||||
if gen.board[y][x] == 1:
|
||||
char = '.'
|
||||
if gen.board[y - 1][x] == 0 or gen.board[y + 1][x] == 0:
|
||||
if gen.board[y][x - 1] == 1 and gen.board[y][x + 1] == 1:
|
||||
char = '═'
|
||||
elif gen.board[y][x - 1] == 0 or gen.board[y][x + 1] == 0:
|
||||
if gen.board[y - 1][x] == 1 and gen.board[y + 1][x] == 1:
|
||||
char = '║'
|
||||
|
||||
draw[y][x] = char
|
||||
|
||||
for y in range(len(draw)):
|
||||
for x in range(len(draw[y])):
|
||||
if draw[y][x] == '.':
|
||||
char = '.'
|
||||
if draw[y - 1][x] == '║' and draw[y][x - 1] == '═':
|
||||
char = '╝'
|
||||
elif draw[y - 1][x] == '║' and draw[y][x + 1] == '═':
|
||||
char = '╚'
|
||||
elif draw[y + 1][x] == '║' and draw[y][x - 1] == '═':
|
||||
char = '╗'
|
||||
elif draw[y + 1][x] == '║' and draw[y][x + 1] == '═':
|
||||
char = '╔'
|
||||
|
||||
draw[y][x] = char
|
||||
|
||||
# for room in rooms:
|
||||
# for w in range(room.width):
|
||||
# map[room.y][room.x + w] = "═"
|
||||
# map[room.y + room.height - 1][room.x + w] = "═"
|
||||
|
||||
# for h in range(room.height):
|
||||
# map[room.y + h][room.x] = "║"
|
||||
# map[room.y + h][room.x + room.width - 1] = "║"
|
||||
|
||||
# for h in range(room.height - 2):
|
||||
# for w in range(room.width - 2):
|
||||
# map[room.y + h + 1][room.x + w + 1] = " "
|
||||
|
||||
# map[room.y][room.x] = "╔"
|
||||
# map[room.y][room.x + room.width - 1] = "╗"
|
||||
# map[room.y + room.height - 1][room.x] = "╚"
|
||||
# map[room.y + room.height - 1][room.x + room.width - 1] = "╝"
|
||||
|
||||
# for y in map:
|
||||
# print(''.join(y))
|
||||
|
||||
for y in draw:
|
||||
print(''.join(str(i) for i in y))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import pygame
|
||||
import json, math
|
||||
from . import assets, generator
|
||||
from . import assets
|
||||
from .generator import Generator
|
||||
from .vector import Vector
|
||||
|
||||
class Map:
|
||||
|
@ -14,7 +15,8 @@ class Map:
|
|||
self.map = json.load(file)
|
||||
|
||||
def generate(self):
|
||||
self.map = generator.new()
|
||||
self.generator = Generator(80)
|
||||
self.map = self.generator.get_map()
|
||||
|
||||
def collides(self, pos, rect):
|
||||
for y in range(int(math.floor(pos.y)) - 1, int(math.floor(pos.y)) + 2):
|
||||
|
|
|
@ -15,7 +15,7 @@ class Player:
|
|||
vel = Vector(0, 0)
|
||||
|
||||
# TODO: Fix diagonal speed
|
||||
speed = 1.5 # meters per second
|
||||
speed = 15 # meters per second
|
||||
|
||||
def __init__(self):
|
||||
self.sprite = Sprite()
|
||||
|
|
Loading…
Reference in New Issue