Skip to content

Commit

Permalink
Define levels with JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Naunet committed Sep 2, 2021
1 parent 9396d6c commit d00a91a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
23 changes: 3 additions & 20 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pygame
from pygame.constants import *
import time
import json
from enum import Enum
# local
import view
Expand Down Expand Up @@ -102,7 +103,7 @@ def level_select(self, event):
self.screen = Screen.GAME

def setup_level(self, level):
params = self.levels.get(level)
params = self.levels.get(str(level))
if not params:
self.initialize(level=level)
else:
Expand All @@ -114,26 +115,8 @@ def setup_level(self, level):
def read_levels(self):
# read levels
levelfile = open('levels.txt', 'r+')
lines = levelfile.readlines()
self.levels = json.load(levelfile) # dictionary
levelfile.close()
# creat dictionary
self.levels = dict()
while lines:
while lines[0] == '\n':
lines.pop(0)
number = int(lines.pop(0))
timer = int(lines.pop(0))
width = int(lines.pop(0))
height = int(lines.pop(0))
level = dict(timer=timer, width=width, height=height)
tmp = lines.pop(0)
if tmp != '\n':
level["wallrate"] = float(tmp)
tmp = lines.pop(0)
if tmp != '\n':
exits = eval(tmp)
level["exits"] = exits
self.levels[number] = level

def play(self, event):
back = self.draw.header(self.current_level, self.paused,
Expand Down
39 changes: 20 additions & 19 deletions levels.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

1
1000
20
22
0.45


2
800
18
18
0.7
[(0,4),(0,8),(0,12),(0,16)]

3
400
10
22
{
"1": {
"timer" : 1000,
"width" : 20,
"height" : 22,
"wallrate" : 0.45
},
"2": {
"timer" : 800,
"width" : 18,
"height" : 18,
"wallrate" : 0.6,
"exits" : [[0,4],[0,8],[0,12],[0,16]]
},
"3": {
"timer" : 400,
"width" : 10,
"height" : 22
}
}

15 changes: 8 additions & 7 deletions wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def __init__(self, view, width, height, exits=None, wallrate=0.45):
self.HEIGHT = height
self.EXITS = exits
if not self.EXITS:
self.EXITS = [(self.WIDTH//2, 0), (self.WIDTH//2 + 1, 0),
(self.WIDTH - 1, self.HEIGHT//2),
(self.WIDTH - 1, self.HEIGHT//2 + 1),
(0, self.HEIGHT//2), (0, self.HEIGHT//2 + 1),
(self.WIDTH//2, self.HEIGHT - 1),
(self.WIDTH//2 + 1, self.HEIGHT - 1)]
self.EXITS = [[self.WIDTH//2, 0], [self.WIDTH//2 + 1, 0],
[self.WIDTH - 1, self.HEIGHT//2],
[self.WIDTH - 1, self.HEIGHT//2 + 1],
[0, self.HEIGHT//2], [0, self.HEIGHT//2 + 1],
[self.WIDTH//2, self.HEIGHT - 1],
[self.WIDTH//2 + 1, self.HEIGHT - 1]]
self.START_X = self.WIDTH//2 # centre cleared by def
self.START_Y = self.HEIGHT//2 # centre cleared by def
self.pos_x = self.START_X
Expand Down Expand Up @@ -107,7 +107,8 @@ def move(self, anim, paused):
return (self.pos_x, self.pos_y)

def is_end_game(self):
if (self.pos_x, self.pos_y) in self.EXITS:
# print((self.pos_x, self.pos_y), self.EXITS)
if [self.pos_x, self.pos_y] in self.EXITS:
return True
return False

Expand Down

0 comments on commit d00a91a

Please sign in to comment.