From 1f6aec2a1a5dba2ef8a93a256deb9fb527b0fbc8 Mon Sep 17 00:00:00 2001 From: kleph Date: Tue, 1 Mar 2016 12:57:31 +0100 Subject: [PATCH] Initial import Static map file - test pyglet --- 00_static_map.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++ brick.png | Bin 0 -> 433 bytes road.png | Bin 0 -> 189 bytes test_map.txt | 21 ++++++++++ 4 files changed, 127 insertions(+) create mode 100644 00_static_map.py create mode 100644 brick.png create mode 100644 road.png create mode 100644 test_map.txt diff --git a/00_static_map.py b/00_static_map.py new file mode 100644 index 0000000..7a0c0d2 --- /dev/null +++ b/00_static_map.py @@ -0,0 +1,106 @@ +import pyglet +from pyglet.window import key + +import sys +import re + +# main window +window = pyglet.window.Window() + +TILE_SIZE_X = 16 +TILE_SIZE_Y = 16 + +class Level: + """ Level: mostly map related stuff for now """ + def __init__(self, mapfile): + self.mapfile = mapfile + self.tiles = [] + self.load_mapfile() + + def load_mapfile(self): + try: + maph = open(self.mapfile) + except Exception as error: + print('Error while loading mapfile: ' + error) + sys.exit(1) + self.load_tiles(maph) + self.load_map(maph) + + def load_tiles(self, maph): + line = maph.readline().strip() + while True: + if re.match('^-- tiles$', line): + print('Found tiles header: ' + line) + break + else: + line = maph.readline().strip() + + last_tile = maph.tell() + line = maph.readline().strip() + while not re.match('^--', line): + tile_file = re.compile('\d: ').split(line)[1] + print('loading tile: ' + str(tile_file)) + self.tiles.append(pyglet.resource.image(tile_file)) + # save stream position before reading the new line to rewind it later + last_tile = maph.tell() + line = maph.readline().strip() + print('Found other header, stop loading tiles') + # "rewind" the stream so the header can be read by the next function + maph.seek(last_tile) + + def load_map(self, maph): + line = maph.readline().strip() + while True: + if re.match('^-- map$', line): + print('Found map header: ' + line) + break + else: + line = maph.readline().strip() + + # read map size + line = maph.readline().strip() + self.mapx, self.mapy = map(int, line.split('x')) + print('map size: ' + str(self.mapx) + 'x' + str(self.mapy)) + + self.tile_map = [None] * self.mapy + i = 0 + while i < self.mapy: + line = maph.readline().strip() + line_lst = re.compile(',').split(line) + # print(str(line_lst)) + line_lst = [int(x) for x in line_lst] + self.tile_map[i] = (line_lst) + i += 1 + + def draw_map(self): + for y in range(0, self.mapy): + for x in range(0, self.mapx): + tile_num = self.tile_map[y][x] + self.tiles[tile_num].blit(x*TILE_SIZE_X, y*TILE_SIZE_Y) + +@window.event +def on_key_press(symbol, modifiers): + if symbol == key.Q: + print('Will quit') + elif symbol == key.LEFT: + print('Left arrow') + elif symbol == key.ENTER: + print('Enter !') + +@window.event +def on_draw(): + window.clear() + level.draw_map() + label.draw() + + + +label = pyglet.text.Label('Plop World', x = window.width/2, y = window.height/2, anchor_x='center', anchor_y='center') + +brick_img = pyglet.resource.image('brick.png') +road_img = pyglet.resource.image('road.png') + +level = Level('test_map.txt') + + +pyglet.app.run() \ No newline at end of file diff --git a/brick.png b/brick.png new file mode 100644 index 0000000000000000000000000000000000000000..6f90d556f2783f38f23b11523d8354fc054528bd GIT binary patch literal 433 zcmV;i0Z#sjP)mjTN76(cSr?C;IQ1l@);;a zh27$u;H6kgh+(49c%EhL``L96Aa(fC4+7L9Ae#w%mm56*DJA)BDF|+6z+qj3Btii2 zFVC`UCTTRwW+|o7Y&4V9-N8%lp!0dF2%$Tz>y*;qub0aJ10Ze2oT>$zS5MJ`Fq#3U zbxjQzpjd2NJJ{XK?r?&w0$wY6Zrv8z4|bG3+}$WRN2Bo>0DeUX^hF~~(^qGeCv{fr zmv#xV18FQn$)$nWEToixAuU5 zmKcGL=PacB>Nus;ihC+SkOVBtl5>tR?)Up?n*I&&o?)KnZ07&2pHwMN73O(3Q3l@MwB?`=jNv7l`uFLr6!i7rYMwWmSiZnd-?{1H}Z)C z6`6awIEHAPPfj?%B=P_M|ML!V3eMWf%*@O&;>yab(}IN!l1hb?m6e4Lcd%$|Y!OI3 eqG=~{h?7BvMS}68f7DH&5e%NLelF{r5}E)Xc`ktf literal 0 HcmV?d00001 diff --git a/test_map.txt b/test_map.txt new file mode 100644 index 0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