From 3943c2610dfa351998a1740c21db77e577278b47 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Wed, 13 Jan 2021 11:20:05 -0600 Subject: [PATCH] added levels --- src/main.py | 22 ++++++++++++++++++- src/testlevel.json | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/testlevel.json diff --git a/src/main.py b/src/main.py index 7f50a71..1977028 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ __docformat__ = "reStructuredText" -import pymunk, pymunk.pygame_util, pygame, random +import pymunk, pymunk.pygame_util, pygame, random, json class Camera(object): """ @@ -138,6 +138,23 @@ class Rain(object): else: raindrop.draw() +class Level(object): + """ + Level class + Creates and destroys a level from a JSON file. + """ + + def __init__(self, levelFilePath: str, game): + self._levelFilePath = levelFilePath + self._levelFile = open(self._levelFilePath, 'r') + self.level = json.load(self._levelFile) + self._game = game + + def start(self): + for platform in self.level['platforms']: + self._game._shapes.append(Platform(self._game,platform['pos1'],platform['pos2'],platform['width'] or 4,platform['color'] or (0,0,0,1))) + + class RiseToFall(object): """ riseToFall game class @@ -168,6 +185,9 @@ class RiseToFall(object): self.camera = Camera() + self.level = Level('testlevel.json',self) + self.level.start() + # set draw options self._print_options = pymunk.pygame_util.DrawOptions(self._screen) diff --git a/src/testlevel.json b/src/testlevel.json new file mode 100644 index 0000000..48b604d --- /dev/null +++ b/src/testlevel.json @@ -0,0 +1,55 @@ +{ + "platforms": [ + { + "pos1": [ + 200, + 200 + ], + "pos2": [ + 200, + 350 + ], + "width": 4, + "color": "" + }, + { + "pos1": [ + 200, + 350 + ], + "pos2": [ + 300, + 350 + ], + "width": 4, + "color": "" + } + ], + "obstacles": [ + { + "pos": [ + 300,300 + ] + } + ], + "playerSpawn": [200,400], + "checkpoints": [ + { + "pos": [300,300], + "size":[20,20] + } + ], + "switches": [ + { + "pos": [300,300], + "rotation": 0 + } + ], + "text": [ + { + "text": "a and d to move", + "pos": [100,100], + "size": 14 + } + ] +}