From 4c8380b36ee84667bc1953829e7d4e111341cd1d Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 16 Oct 2017 02:03:22 +0200 Subject: [PATCH] - refactor seed generation - add grid (G key) --- 03_map.py | 57 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/03_map.py b/03_map.py index aa0b8d2..f1adc5f 100644 --- a/03_map.py +++ b/03_map.py @@ -93,7 +93,12 @@ class Room: tilemap[self.x + x][self.y + self.height - 1] = Level.TILE_WALL def center(self): - return round(self.x + self.width/2), round(self.y + self.height/2) + cx = 0 + cy = 0 + cx = round(self.x + self.width / 2) + cy = round(self.y + self.height / 2) + + return cx, cy class Level: @@ -102,7 +107,7 @@ class Level: TILE_GROUND = 2 TILE_HALLWAY = 3 - def __init__(self, sizex, sizey, tile_file=None): + def __init__(self, sizex, sizey, seed, tile_file=None): self.sizex, self.sizey = sizex, sizey self.tileset = [] self.tilemap = [] @@ -112,6 +117,7 @@ class Level: self.tree = None self.rooms = [] self.regenerate() + self.seed = seed def load_tileset(self, tile_file): resource_file = open(tile_file) @@ -125,7 +131,7 @@ class Level: def generate_tree(self): # init tree - tree = [Leaf(0, 0, 30, 30, 0)] + tree = [Leaf(0, 0, self.sizex, self.sizey, 0)] # split leaves until none succeed # Lists are ordered. Tree will be created and travel from left to right @@ -179,6 +185,16 @@ class Level: for y in range(0, self.sizey): for x in range(0, self.sizex): self.tileset[self.tilemap[x][y]].blit(x*TILE_SIZE_X, y*TILE_SIZE_Y) + if self.grid: + for x in range(0, self.sizex): + pyglet.graphics.draw(2, pyglet.gl.GL_LINES, + ('v2i', (x*TILE_SIZE_X, 0, x*TILE_SIZE_X, self.sizey*TILE_SIZE_Y)), + ('c3f', (0, 0, 1)*2 ) ) + + for y in range(0, self.sizey): + pyglet.graphics.draw(2, pyglet.gl.GL_LINES, + ('v2i', (0, y * TILE_SIZE_Y, self.sizex * TILE_SIZE_X, y * TILE_SIZE_Y)), + ('c3f', (0, 0, 1)*2 ) ) def dump_tilemap(self): for y in range(0, self.sizey): @@ -200,17 +216,12 @@ class Level: def generate_hallway(self, room_a, room_b): """ """ - print("hallway between room" + str(room_a.id) + " and room " + str(room_b.id)) + print("hallway between room " + str(room_a.id) + " and room " + str(room_b.id)) (xa, ya) = room_a.center() (xb, yb) = room_b.center() - # horizontal part - if xa < xb: - start_x = xa - end_x = xb - else: - start_x = xb - end_x = xa + start_x = xa + end_x = xb print("h_hw form " + str(start_x) + " and " + str(end_x)) for x in range(start_x, end_x): self.tilemap[x][ya] = self.TILE_ROAD @@ -245,10 +256,13 @@ def on_key_press(symbol, modifiers): sys.exit(0) if symbol == key.R: print('Regeneration') + level.seed = init_random() level.regenerate() window.invalid = True if symbol == key.D: level.dump_tilemap() + if symbol == key.G: + level.grid ^= 1 elif symbol == key.LEFT: print('Left arrow') elif symbol == key.ENTER: @@ -262,18 +276,21 @@ def on_draw(): label.draw() -# init random -if len(sys.argv) > 1: - seed = int(sys.argv[1]) -else: - seed = random.randint(0, sys.maxsize) -print("Using seed: " + str(seed)) -random.seed(seed) +def init_random(): + if len(sys.argv) > 1: + seed = int(sys.argv[1]) + else: + seed = random.randint(0, sys.maxsize) + print("Using seed: " + str(seed)) + random.seed(seed) + return seed -label_txt = 'Plop World seed: ' + str(seed) + +genseed = init_random() +label_txt = 'Plop World seed: ' + str(genseed) label = pyglet.text.Label(label_txt, x=window.width*5/6, y=window.height*5/6, anchor_x='center', anchor_y='center', multiline=True, width=window.width - window.width*5/6) -level = Level(30, 30, 'tiles.txt') +level = Level(30, 30, genseed, 'tiles.txt') pyglet.app.run()