This commit is contained in:
kleph 2016-03-12 15:22:58 +01:00
parent c64e376da4
commit b074d2de01

View file

@ -12,6 +12,7 @@ TILE_SIZE_Y = 16
MIN_LEAF_SIZE = 7 MIN_LEAF_SIZE = 7
class Leaf: class Leaf:
""" Leaf of the BSP """ """ Leaf of the BSP """
@ -79,6 +80,7 @@ class Leaf:
for x in range(0, self.width): for x in range(0, self.width):
ground.blit((self.x + x)*TILE_SIZE_X, (self.y+y)*TILE_SIZE_Y) ground.blit((self.x + x)*TILE_SIZE_X, (self.y+y)*TILE_SIZE_Y)
class Room: class Room:
""" room """ """ room """
@ -108,19 +110,20 @@ class Room:
for x in range(0, self.width): for x in range(0, self.width):
wall.blit((self.x+x)*TILE_SIZE_X, (self.y + self.height-1)*TILE_SIZE_Y) wall.blit((self.x+x)*TILE_SIZE_X, (self.y + self.height-1)*TILE_SIZE_Y)
def generate_tree(): def generate_tree():
# init tree # init tree
tree = [Leaf(0, 0, 30, 30, 0)] tree = [Leaf(0, 0, 30, 30, 0)]
#tree = [Leaf(0, 0, 10, 10, 0)] # tree = [Leaf(0, 0, 10, 10, 0)]
#tree = [Leaf(0, 0, 5, 5, 0)] # tree = [Leaf(0, 0, 5, 5, 0)]
# split leaves until none succeed # split leaves until none succeed
split_done = True split_done = True
while split_done: while split_done:
split_done = False split_done = False
for l in tree: for l in tree:
if l.left_leaf == None and l.right_leaf == None: if l.left_leaf and l.right_leaf:
if (l.split()): if l.split():
tree.append(l.left_leaf) tree.append(l.left_leaf)
tree.append(l.right_leaf) tree.append(l.right_leaf)
split_done = True split_done = True
@ -138,9 +141,10 @@ def generate_tree():
# main window # main window
window = pyglet.window.Window() window = pyglet.window.Window()
@window.event @window.event
def on_key_press(symbol, modifiers): def on_key_press(symbol, modifiers):
#TODO: ugly hack # TODO: ugly hack
global level_tree global level_tree
if symbol == key.Q: if symbol == key.Q:
@ -154,6 +158,7 @@ def on_key_press(symbol, modifiers):
elif symbol == key.ENTER: elif symbol == key.ENTER:
print('Enter !') print('Enter !')
@window.event @window.event
def on_draw(): def on_draw():
window.clear() window.clear()
@ -167,20 +172,20 @@ def draw_tree(t):
""" """
for l in t: for l in t:
# draw only last leaves # draw only last leaves
if l.left_leaf == None and l.right_leaf == None: if l.left_leaf and l.right_leaf:
l.draw(tiles[TILE_ROAD]) l.draw(tiles[TILE_ROAD])
for l in t: for l in t:
# draw only last leaves # draw only last leaves
if l.left_leaf == None and l.right_leaf == None: if l.left_leaf and l.right_leaf:
if l.room: if l.room:
l.room.draw(tiles[TILE_WALL], tiles[TILE_GROUND]) l.room.draw(tiles[TILE_WALL], tiles[TILE_GROUND])
# init random # init random
#TODO add seed mode (how ?) # TODO add seed mode (how ?)
random.seed() random.seed()
label = pyglet.text.Label('Plop World', x = window.width*5/6, y = window.height*5/6, anchor_x='center', anchor_y='center') label = pyglet.text.Label('Plop World', x=window.width*5/6, y=window.height*5/6, anchor_x='center', anchor_y='center')
# load images # load images
tiles = [] tiles = []