From 8a628b15e90c9746869c37fe2de0b2bf3f65d2d0 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 17:09:27 +0200 Subject: [PATCH 01/28] Removed some unneeded selfs, added a todo --- scrap_engine.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 8b8f3c0..0482af4 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -46,7 +46,7 @@ class CoordinateError(Exception): An Error that is thrown, when an object is added to a non-existing part of a map. """ - def __init__(self, ob, map, x, y): + def __init__(self, ob, map, x, y): # TODO: rename map self.ob = ob self.x = x self.y = y @@ -69,9 +69,7 @@ def __init__(self, height=height - 1, width=width, background="#", for _ in range(height)] self.obmap = [[[] for _ in range(width)] for _ in range(height)] self.obs = [] - self.out = "\r\u001b[" + str(self.height) + "A" self.out_old = "" - self.out_line = "" def blur_in(self, blurmap, esccode="\033[37m"): """ @@ -92,15 +90,15 @@ def show(self, init=False): """ Prints the maps content. """ - self.out = "\r\u001b[" + str(self.height) + "A" + out = f"\r\u001b[{self.height}A" for arr in self.map: - self.out_line = "" + out_line = "" for i in arr: - self.out_line += i - self.out += self.out_line - if self.out_old != self.out or self.dynfps is False or init: - print(self.out + "\n\u001b[1000D", end="") - self.out_old = self.out + out_line += i + out += out_line + if self.out_old != out or not self.dynfps or init: + print(out + "\n\u001b[1000D", end="") + self.out_old = out def resize(self, height, width, background="#"): """ From de38d11345a45f24ce28b386c55d9f231d760cf4 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 17:25:17 +0200 Subject: [PATCH 02/28] Renamed some interators --- scrap_engine.py | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 0482af4..6450462 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -75,14 +75,14 @@ def blur_in(self, blurmap, esccode="\033[37m"): """ Sets another maps content as its background. """ - for l in range(self.height): - for i in range(self.width): - if blurmap.map[l][i] != " ": - self.map[l][i] = (esccode + - blurmap.map[l][i].replace("\033[0m", "")[-1] + + for h in range(self.height): + for w in range(self.width): + if blurmap.map[h][w] != " ": + self.map[h][w] = (esccode + + blurmap.map[h][w].replace("\033[0m", "")[-1] + "\033[0m") else: - self.map[l][i] = " " + self.map[h][w] = " " for ob in self.obs: ob.redraw() @@ -93,8 +93,8 @@ def show(self, init=False): out = f"\r\u001b[{self.height}A" for arr in self.map: out_line = "" - for i in arr: - out_line += i + for char in arr: + out_line += char out += out_line if self.out_old != out or not self.dynfps or init: print(out + "\n\u001b[1000D", end="") @@ -180,13 +180,11 @@ def __init__(self, char, state="solid", arg_proto=None): self.state = state self.added = False self.arg_proto = arg_proto # This was added to enable more than the - self.x = None + self.x = None # default args for custom objects in Text and Square self.y = None self.backup = None self.map = None - # default args for custom objects in Text and Square - def add(self, map, x, y): """ Adds the object to a certain coordinate on a certain map. @@ -401,8 +399,8 @@ def set_state(self, state): Sets all objects states to a certain state. """ self.state = state - for i in self.obs: - i.set_state(state) + for ob in self.obs: + ob.set_state(state) class Text(ObjectGroup): @@ -501,23 +499,23 @@ def __init__(self, char, width, height, state="solid", ob_class=Object, self.__create() def __create(self): - for l in range(self.height): + for i in range(self.height): if self.threads: threading.Thread(target=self.__one_line_create, - args=(l,), daemon=True).start() + args=(i,), daemon=True).start() else: - self.__one_line_create(l) + self.__one_line_create(i) - def __one_line_create(self, l): + def __one_line_create(self, j): for i in range(self.width): - exec(f"self.ob_{i}_{l} = self.ob_class(self.char, self.state,\ + exec(f"self.ob_{i}_{j} = self.ob_class(self.char, self.state,\ arg_proto=self.ob_args)") - exec(f"self.obs.append(self.ob_{i}_{l})") + exec(f"self.obs.append(self.ob_{i}_{j})") - def __one_line_add(self, l): + def __one_line_add(self, j): for i in range(self.width): - exec(f"self.exits.append(self.ob_{i}_{l}.add(self.map, self.x+i,\ -self.y+l))") + exec(f"self.exits.append(self.ob_{i}_{j}.add(self.map, self.x+i,\ +self.y+j))") def add(self, map, x, y): """ @@ -526,12 +524,12 @@ def add(self, map, x, y): self.x = x self.y = y self.map = map - for l in range(self.height): + for i in range(self.height): if self.threads: - threading.Thread(target=self.__one_line_add, args=(l,), + threading.Thread(target=self.__one_line_add, args=(i,), daemon=True).start() else: - self.__one_line_add(l) + self.__one_line_add(i) self.added = True if 1 in self.exits: return 1 From 2875ca120b70f5e6502355a727d4c94a0fb90865 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 17:31:21 +0200 Subject: [PATCH 03/28] Renamed map to map_ --- scrap_engine.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 6450462..92d3d3a 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -46,11 +46,11 @@ class CoordinateError(Exception): An Error that is thrown, when an object is added to a non-existing part of a map. """ - def __init__(self, ob, map, x, y): # TODO: rename map + def __init__(self, ob, map_, x, y): # TODO: rename map self.ob = ob self.x = x self.y = y - self.map = map + self.map = map_ super().__init__(f"The {ob}s coordinate ({x}|{y}) is \ not in {map.width - 1}x{map.height - 1}") @@ -185,21 +185,21 @@ def __init__(self, char, state="solid", arg_proto=None): self.backup = None self.map = None - def add(self, map, x, y): + def add(self, map_, x, y): """ Adds the object to a certain coordinate on a certain map. """ - if not (0 <= x < map.width) or not (0 <= y < map.height): - raise CoordinateError(self, map, x, y) - if "solid" in [ob.state for ob in map.obmap[y][x]]: + if not (0 <= x < map_.width) or not (0 <= y < map_.height): + raise CoordinateError(self, map_, x, y) + if "solid" in [ob.state for ob in map_.obmap[y][x]]: return 1 - self.backup = map.map[y][x] + self.backup = map_.map[y][x] self.x = x self.y = y - map.map[y][x] = self.char - map.obmap[y][x].append(self) - map.obs.append(self) - self.map = map + map_.map[y][x] = self.char + map_.obmap[y][x].append(self) + map_.obs.append(self) + self.map = map_ self.added = True return 0 @@ -440,19 +440,19 @@ def __texter(self, text): for ob in self.obs: ob.group = self - def add(self, map, x, y): + def add(self, map_, x, y): """ Adds the text to a certain coordinate on a certain map. """ self.added = True - self.map = map + self.map = map_ self.x = x self.y = y count = 0 for l, text in enumerate(self.text.split("\n")): for i, ob in enumerate(self.obs[count:count + len(text)]): if ob.char != self.ignore: - ob.add(map, x + i, y + l) + ob.add(self.map, x + i, y + l) count += len(text) def remove(self): @@ -517,13 +517,13 @@ def __one_line_add(self, j): exec(f"self.exits.append(self.ob_{i}_{j}.add(self.map, self.x+i,\ self.y+j))") - def add(self, map, x, y): + def add(self, map_, x, y): """ Adds the square to a certain coordinate on a certain map. """ self.x = x self.y = y - self.map = map + self.map = map_ for i in range(self.height): if self.threads: threading.Thread(target=self.__one_line_add, args=(i,), @@ -617,13 +617,13 @@ def __add_obs(self): for ob, rx, ry in zip(self.verticals, [0, self.width - 1], [1, 1]): ob.add(self.map, self.x + rx, self.y + ry) - def add(self, map, x, y): + def add(self, map_, x, y): """ Adds the frame to a certain coordinate on a certain map. """ self.x = x self.y = y - self.map = map + self.map = map_ self.__add_obs() self.added = True @@ -685,13 +685,13 @@ def __init__(self, height, width): self.width = width self.added = False - def add(self, map, x, y): + def add(self, map_, x, y): """ Adds the box to a certain coordinate on a certain map. """ self.x = x self.y = y - self.map = map + self.map = map_ for ob in self.obs: ob.add(self.map, ob.rx + self.x, ob.ry + self.y) self.added = True From 316dd41ad7edd66d7300107e2edf775f10ea3911 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 17:39:12 +0200 Subject: [PATCH 04/28] Renamed type to l_type --- scrap_engine.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 92d3d3a..0a2f31f 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -6,8 +6,8 @@ Maps are objects, Object objects can be added to and then can be shown on the screen. -ObjectGroup and their daughters can be used to automate generetaing, adding, -removing etc. for a list of objects in their defined manner. +ObjectGroup and their daughters can be used to automate generetaing, adding, +removing etc. for a list of objects in their defined manner. States: Possible states an object can have are 'solid' and 'float'. @@ -17,10 +17,10 @@ so their .set() methods will return 0. arg_proto: - arg_proto is an dictionary that is given to an object by - the programmer or an object_group(circle, frame, etc.) via the ob_args + arg_proto is an dictionary that is given to an object by + the programmer or an object_group(circle, frame, etc.) via the ob_args argument. - This can be used to store various extra values and is especially usefull + This can be used to store various extra values and is especially usefull when using daughter classes of Object that needs extra values. This software is licensed under the GPL3 @@ -43,7 +43,7 @@ class CoordinateError(Exception): """ - An Error that is thrown, when an object is added to a non-existing + An Error that is thrown, when an object is added to a non-existing part of a map. """ def __init__(self, ob, map_, x, y): # TODO: rename map @@ -52,7 +52,7 @@ def __init__(self, ob, map_, x, y): # TODO: rename map self.y = y self.map = map_ super().__init__(f"The {ob}s coordinate ({x}|{y}) is \ -not in {map.width - 1}x{map.height - 1}") +not in {self.map.width - 1}x{self.map.height - 1}") class Map: @@ -189,7 +189,7 @@ def add(self, map_, x, y): """ Adds the object to a certain coordinate on a certain map. """ - if not (0 <= x < map_.width) or not (0 <= y < map_.height): + if not 0 <= x < map_.width or not 0 <= y < map_.height: raise CoordinateError(self, map_, x, y) if "solid" in [ob.state for ob in map_.obmap[y][x]]: return 1 @@ -296,9 +296,9 @@ def bump_bottom(self): def pull_ob(self): """ - This is triggered, when trying to set an object from a non existing + This is triggered, when trying to set an object from a non existing spot on the map to an existing one. - This is just usefull when resizing maps with objects out of the + This is just usefull when resizing maps with objects out of the new size. """ return @@ -676,7 +676,7 @@ def resize(self, height, width): class Box(ObjectGroup): """ - A datastucture used to group objects(groups) relative to a certain + A datastucture used to group objects(groups) relative to a certain coordinate, that can be added to a map. """ def __init__(self, height, width): @@ -780,7 +780,7 @@ class Line(Box): """ A line described by a vector, that cam be added to map. """ - def __init__(self, char, cx, cy, type="straight", state="solid", + def __init__(self, char, cx, cy, l_type="straight", state="solid", ob_class=Object, ob_args=None): super().__init__(0, 0) if ob_args is None: @@ -789,7 +789,7 @@ def __init__(self, char, cx, cy, type="straight", state="solid", self.ob_class = ob_class self.ob_args = ob_args self.state = state - self.type = type + self.type = l_type self.__gen(cx, cy) def __gen(self, cx, cy): From ba5b83fca0c2d662d8a5a58b9bdf355ee49bb465 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 17:42:21 +0200 Subject: [PATCH 05/28] Specified Exceptions --- scrap_engine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 0a2f31f..4825965 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -117,7 +117,7 @@ def resize(self, height, width, background="#"): try: self.obmap[ob.y][ob.x].append(ob) ob.redraw() - except: # TODO: Check possible exceptions and specify + except IndexError: pass @@ -145,7 +145,7 @@ def remap(self): range(self.x, self.x + self.width)): try: self.map[sy][sx] = self.bmap.map[y][x] - except: # TODO: Check possible exceptions and specify + except IndexError: continue for ob in self.obs: ob.redraw() From 86ea9f885f787d3c60e63c454ecc7bd1fec85704 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 18:23:51 +0200 Subject: [PATCH 06/28] Rewrote ObjectGroup.rem_ob --- scrap_bench.py | 39 +++++++++++++++++++++++++++++++++++++++ scrap_engine.py | 23 ++++++++++++----------- 2 files changed, 51 insertions(+), 11 deletions(-) create mode 100755 scrap_bench.py diff --git a/scrap_bench.py b/scrap_bench.py new file mode 100755 index 0000000..2824bf7 --- /dev/null +++ b/scrap_bench.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import scrap_engine as se +import time, random, os + +def main(): + os.system("") + map = se.Map(background=" ") + text = se.Text("0") + avr = se.Text("0") + text.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)-1) + avr.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)) + tcount = 0 + times = 0 + obs = [] + for i in range(50): + for j in range(10): + exec("ob_%s_%s = se.Object('\033[34ma\033[0m')"%(str(i), str(j))) + exec("ob_%s_%s.add(map, i, j)"%(str(i), str(j))) + exec("obs.append(ob_%s_%s)"%(str(i), str(j))) + + map.show(init=True) + time2 = 0 + while True: + time0 = time.time() + times += time2 + for ob in obs: + ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1])) + text.rechar(str(time2)) + avr.rechar(str(times/tcount if tcount != 0 else 1)) + map.show() + tcount += 1 + time2 = time.time()-time0 + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + print("KeyboardInterrupt") diff --git a/scrap_engine.py b/scrap_engine.py index 4825965..d9ac015 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -32,13 +32,12 @@ __author__ = "lxgr " __version__ = "0.3.3" -# TODO: add comments or use more verbose var names (im looking at you "l") import math import os import threading -width, height = os.get_terminal_size() +screen_width, screen_height = os.get_terminal_size() class CoordinateError(Exception): @@ -46,7 +45,7 @@ class CoordinateError(Exception): An Error that is thrown, when an object is added to a non-existing part of a map. """ - def __init__(self, ob, map_, x, y): # TODO: rename map + def __init__(self, ob, map_, x, y): self.ob = ob self.x = x self.y = y @@ -59,7 +58,7 @@ class Map: """ The map, objects can be added to. """ - def __init__(self, height=height - 1, width=width, background="#", + def __init__(self, height=screen_height - 1, width=screen_width, background="#", dynfps=True): self.height = height self.width = width @@ -125,7 +124,8 @@ class Submap(Map): """ Behaves just like a map, but it self contains a part of another map. """ - def __init__(self, bmap, x, y, height=height - 1, width=width, dynfps=True): + def __init__(self, bmap, x, y, height=screen_height - 1, + width=screen_width, dynfps=True): super().__init__(height, width, dynfps=dynfps) del self.background self.y = y @@ -312,6 +312,7 @@ def rechar(self, char): return 1 self.map.map[self.y][self.x] = self.backup self.redraw() + return 0 def remove(self): """ @@ -322,6 +323,7 @@ def remove(self): self.added = False self.__backup_setter() del self.map.obs[self.map.obs.index(self)] + return 0 def set_state(self, state): """ @@ -362,11 +364,10 @@ def rem_ob(self, ob): """ Removes an object from the group. """ - for i in range(len(self.obs)): - if ob == self.obs[i]: - self.obs[i].group = "" - del self.obs[i] - return 0 + if ob in self.obs: + ob.group = "" + self.obs.pop(self.obs.index(ob)) + return 0 return 1 def move(self, x=0, y=0): @@ -432,7 +433,7 @@ def __add__(self, other): def __texter(self, text): for text in text.split("\n"): - for i, char in enumerate(text): + for char in text: if self.esccode != "": char = self.esccode + char + "\033[0m" self.obs.append(self.ob_class(char, self.state, From 3b6e141e8a9855fbfeebd28848bf2a99d82a1d31 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 18:37:04 +0200 Subject: [PATCH 07/28] Renamed some iterators --- scrap_engine.py | 102 ++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index d9ac015..6e98e5e 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -184,6 +184,7 @@ def __init__(self, char, state="solid", arg_proto=None): self.y = None self.backup = None self.map = None + self.group = None def add(self, map_, x, y): """ @@ -343,8 +344,8 @@ def __init__(self, obs): self.state = None self.obs = obs self.map = None - for ob in obs: - ob.group = self + for obj in obs: + obj.group = self def add_ob(self, ob): """ @@ -365,7 +366,7 @@ def rem_ob(self, ob): Removes an object from the group. """ if ob in self.obs: - ob.group = "" + ob.group = None self.obs.pop(self.obs.index(ob)) return 0 return 1 @@ -374,17 +375,17 @@ def move(self, x=0, y=0): """ Moves all objects in the group by a certain vector. """ - for ob in self.obs: - ob.remove() - for ob in self.obs: - ob.add(self.map, ob.x + x, ob.y + y) + for obj in self.obs: + obj.remove() + for obj in self.obs: + obj.add(self.map, obj.x + x, obj.y + y) def remove(self): """ Removes all objects from their maps. """ - for ob in self.obs: - ob.remove() + for obj in self.obs: + obj.remove() def set(self, x, y): """ @@ -400,8 +401,8 @@ def set_state(self, state): Sets all objects states to a certain state. """ self.state = state - for ob in self.obs: - ob.set_state(state) + for obj in self.obs: + obj.set_state(state) class Text(ObjectGroup): @@ -432,14 +433,14 @@ def __add__(self, other): return self def __texter(self, text): - for text in text.split("\n"): - for char in text: + for txt in text.split("\n"): + for char in txt: if self.esccode != "": char = self.esccode + char + "\033[0m" self.obs.append(self.ob_class(char, self.state, arg_proto=self.ob_args)) - for ob in self.obs: - ob.group = self + for obj in self.obs: + obj.group = self def add(self, map_, x, y): """ @@ -451,9 +452,9 @@ def add(self, map_, x, y): self.y = y count = 0 for l, text in enumerate(self.text.split("\n")): - for i, ob in enumerate(self.obs[count:count + len(text)]): - if ob.char != self.ignore: - ob.add(self.map, x + i, y + l) + for i, obj in enumerate(self.obs[count:count + len(text)]): + if obj.char != self.ignore: + obj.add(self.map, x + i, y + l) count += len(text) def remove(self): @@ -461,8 +462,8 @@ def remove(self): Removes the text from the map. """ self.added = False - for ob in self.obs: - ob.remove() + for obj in self.obs: + obj.remove() def rechar(self, text, esccode=""): """ @@ -470,8 +471,8 @@ def rechar(self, text, esccode=""): """ self.esccode = esccode if self.added: - for ob in self.obs: - ob.remove() + for obj in self.obs: + obj.remove() self.obs = [] self.__texter(text) self.text = text @@ -541,15 +542,15 @@ def remove(self): Removes the square from the map. """ self.added = False - for ob in self.obs: - ob.remove() + for obj in self.obs: + obj.remove() def rechar(self, char): """ Changes the chars the Square is filled with. """ - for ob in self.obs: - ob.rechar(char) + for obj in self.obs: + obj.rechar(char) def resize(self, width, height): """ @@ -581,6 +582,7 @@ class Frame(ObjectGroup): def __init__(self, height, width, corner_chars=None, horizontal_chars=None, vertical_chars=None, state="solid", ob_class=Object, ob_args=None): + super().__init__([]) if ob_args is None: ob_args = {} if vertical_chars is None: @@ -610,13 +612,13 @@ def __init__(self, height, width, corner_chars=None, self.map = None def __add_obs(self): - for ob, rx, ry in zip(self.corners, [0, self.width - 1, 0, self.width - 1], + for obj, rx, ry in zip(self.corners, [0, self.width - 1, 0, self.width - 1], [0, 0, self.height - 1, self.height - 1]): - ob.add(self.map, self.x + rx, self.y + ry) - for ob, rx, ry in zip(self.horizontals, [1, 1], [0, self.height - 1]): - ob.add(self.map, self.x + rx, self.y + ry) - for ob, rx, ry in zip(self.verticals, [0, self.width - 1], [1, 1]): - ob.add(self.map, self.x + rx, self.y + ry) + obj.add(self.map, self.x + rx, self.y + ry) + for obj, rx, ry in zip(self.horizontals, [1, 1], [0, self.height - 1]): + obj.add(self.map, self.x + rx, self.y + ry) + for obj, rx, ry in zip(self.verticals, [0, self.width - 1], [1, 1]): + obj.add(self.map, self.x + rx, self.y + ry) def add(self, map_, x, y): """ @@ -634,8 +636,8 @@ def set(self, x, y): """ self.x = x self.y = y - for ob in self.corners + self.horizontals + self.verticals: - ob.remove() + for obj in self.corners + self.horizontals + self.verticals: + obj.remove() self.__add_obs() def rechar(self, corner_chars=None, horizontal_char="-", @@ -645,19 +647,19 @@ def rechar(self, corner_chars=None, horizontal_char="-", """ if corner_chars is None: corner_chars = ["+", "+", "+", "+"] - for ob, c in zip(self.corners, corner_chars): - ob.rechar(c) - for ob in self.horizontals: - ob.rechar(horizontal_char) - for ob in self.verticals: - ob.rechar(vertical_char) + for obj, c in zip(self.corners, corner_chars): + obj.rechar(c) + for obj in self.horizontals: + obj.rechar(horizontal_char) + for obj in self.verticals: + obj.rechar(vertical_char) def remove(self): """ Removes the frame from the map. """ - for ob in self.corners + self.horizontals + self.verticals: - ob.remove() + for obj in self.corners + self.horizontals + self.verticals: + obj.remove() self.added = False def resize(self, height, width): @@ -693,8 +695,8 @@ def add(self, map_, x, y): self.x = x self.y = y self.map = map_ - for ob in self.obs: - ob.add(self.map, ob.rx + self.x, ob.ry + self.y) + for obj in self.obs: + obj.add(self.map, obj.rx + self.x, obj.ry + self.y) self.added = True def add_ob(self, ob, x, y): @@ -720,8 +722,8 @@ def remove(self): """ Removes the box from the map. """ - for ob in self.obs: - ob.remove() + for obj in self.obs: + obj.remove() self.added = False def resize(self, height, width): @@ -760,8 +762,8 @@ def rechar(self, char): Changes the chars the circle is filled with. """ self.char = char - for ob in self.obs: - ob.rechar(char) + for obj in self.obs: + obj.rechar(char) def resize(self, radius): """ @@ -816,8 +818,8 @@ def rechar(self, char): Changes the chars the line is made from. """ self.char = char - for ob in self.obs: - ob.rechar(char) + for obj in self.obs: + obj.rechar(char) def resize(self, cx, cy): """ From 9651064e41976f44bda56ad619d2ac7fcc7c4cd1 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 18:48:06 +0200 Subject: [PATCH 08/28] ... --- scrap_engine.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 6e98e5e..0b106ab 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -179,8 +179,8 @@ def __init__(self, char, state="solid", arg_proto=None): self.char = char self.state = state self.added = False - self.arg_proto = arg_proto # This was added to enable more than the - self.x = None # default args for custom objects in Text and Square + self.arg_proto = arg_proto + self.x = None self.y = None self.backup = None self.map = None @@ -225,9 +225,9 @@ def set(self, x, y): elif self.x > self.map.width - 1 or self.y > self.map.height - 1: self.pull_ob() return 1 - for ob in self.map.obmap[y][x]: - if ob.state == "solid": - self.bump(ob, self.x - x, self.y - y) + for obj in self.map.obmap[y][x]: + if obj.state == "solid": + self.bump(obj, self.x - x, self.y - y) return 1 self.__backup_setter() self.map.obmap[y][x].append(self) @@ -666,8 +666,7 @@ def resize(self, height, width): """ Changes the frames size. """ - added = self.added - if added: + if added := self.added: self.remove() self.__init__(height, width, corner_chars=self.corner_chars, horizontal_chars=self.horizontal_chars, From 0e1e8af4cea9ed7ec686207ee268f87b0f5f5a41 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 18:51:37 +0200 Subject: [PATCH 09/28] removed scrap_bench --- scrap_bench.py | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100755 scrap_bench.py diff --git a/scrap_bench.py b/scrap_bench.py deleted file mode 100755 index 2824bf7..0000000 --- a/scrap_bench.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 - -import scrap_engine as se -import time, random, os - -def main(): - os.system("") - map = se.Map(background=" ") - text = se.Text("0") - avr = se.Text("0") - text.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)-1) - avr.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)) - tcount = 0 - times = 0 - obs = [] - for i in range(50): - for j in range(10): - exec("ob_%s_%s = se.Object('\033[34ma\033[0m')"%(str(i), str(j))) - exec("ob_%s_%s.add(map, i, j)"%(str(i), str(j))) - exec("obs.append(ob_%s_%s)"%(str(i), str(j))) - - map.show(init=True) - time2 = 0 - while True: - time0 = time.time() - times += time2 - for ob in obs: - ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1])) - text.rechar(str(time2)) - avr.rechar(str(times/tcount if tcount != 0 else 1)) - map.show() - tcount += 1 - time2 = time.time()-time0 - -if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - print("KeyboardInterrupt") From 9ea5d0025480e72f7929542f20e91cb6b1d18712 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Mon, 13 Sep 2021 19:09:34 +0200 Subject: [PATCH 10/28] rewrote resize methods --- scrap_engine.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 0b106ab..ff0ef6f 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -558,14 +558,12 @@ def resize(self, width, height): """ self.width = width self.height = height - if self.added: + if added := self.added: self.remove() - self.obs = [] - self.__create() + self.obs = [] + self.__create() + if added: self.add(self.map, self.x, self.y) - else: - self.obs = [] - self.__create() class Frame(ObjectGroup): @@ -768,14 +766,12 @@ def resize(self, radius): """ Resizes the circle. """ - if self.added: + if added := self.added: self.remove() - self.obs = [] - self.__gen(radius) + self.obs = [] + self.__gen(radius) + if added: self.add(self.map, self.x, self.y) - else: - self.obs = [] - self.__gen(radius) class Line(Box): @@ -824,11 +820,9 @@ def resize(self, cx, cy): """ Resizes the line. """ - if self.added: + if added := self.added: self.remove() - self.obs = [] - self.__gen(cx, cy) + self.obs = [] + self.__gen(cx, cy) + if added: self.add(self.map, self.x, self.y) - else: - self.obs = [] - self.__gen(cx, cy) From 310f76d3f2bf015ec1f9236265d61ed634f584c5 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 12:22:12 +0200 Subject: [PATCH 11/28] Fixed typo --- scrap_engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrap_engine.py b/scrap_engine.py index ff0ef6f..338479c 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -13,7 +13,7 @@ Possible states an object can have are 'solid' and 'float'. If an objects state is 'solid' no other object can be set over it, so the other objects .set() method will return 1. - If an objects state i 'float' other objects can be set over them, + If an objects state is 'float' other objects can be set over them, so their .set() methods will return 0. arg_proto: From 9bc43da64975cb1adea44dc3188c003e4c0508b5 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 12:29:12 +0200 Subject: [PATCH 12/28] Fixed Frame.resize() to not call __init__ again --- scrap_engine.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 338479c..75c53de 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -598,16 +598,21 @@ def __init__(self, height, width, corner_chars=None, self.corner_chars = corner_chars self.horizontal_chars = horizontal_chars self.vertical_chars = vertical_chars + self.__gen_obs() + self.map = None + + def __gen_obs(self): self.corners = [self.ob_class(i, arg_proto=self.ob_args, state=self.state) - for i, j in zip(corner_chars, range(4))] + for i, j in zip(self.corner_chars, range(4))] self.horizontals = [Square(char=i, width=self.width - 2, height=1, - state=self.state, ob_class=Object, ob_args={}) - for i, j in zip(horizontal_chars, range(2))] + state=self.state, ob_class=Object, + ob_args={}) + for i, j in zip(self.horizontal_chars, range(2))] self.verticals = [Square(char=i, width=1, height=self.height - 2, state=self.state, ob_class=Object, ob_args={}) - for i, j in zip(vertical_chars, range(2))] - self.map = None + for i, j in zip(self.vertical_chars, range(2))] + def __add_obs(self): for obj, rx, ry in zip(self.corners, [0, self.width - 1, 0, self.width - 1], @@ -664,12 +669,11 @@ def resize(self, height, width): """ Changes the frames size. """ + self.height = height + self.width = width if added := self.added: self.remove() - self.__init__(height, width, corner_chars=self.corner_chars, - horizontal_chars=self.horizontal_chars, - vertical_chars=self.vertical_chars, state=self.state, - ob_class=self.ob_class, ob_args=self.ob_args) + self.__gen_obs() if added: self.add(self.map, self.x, self.y) From 435e1a0ef5ff18269a92278ae3a0944b20f377f1 Mon Sep 17 00:00:00 2001 From: ttpf-P <65406575+ttpf-P@users.noreply.github.com> Date: Tue, 14 Sep 2021 14:18:39 +0200 Subject: [PATCH 13/28] minor speedup and fix for possible future bug --- scrap_engine.py | 89 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 8b8f3c0..89c245b 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -2,11 +2,11 @@ """ Ascii game engine for the terminal. -The main data scructures are Map and Object. +The main data structures are Map and Object. Maps are objects, Object objects can be added to and then can be shown on the screen. -ObjectGroup and their daughters can be used to automate generetaing, adding, +ObjectGroup and their daughters can be used to automate generating, adding, removing etc. for a list of objects in their defined manner. States: @@ -20,11 +20,11 @@ arg_proto is an dictionary that is given to an object by the programmer or an object_group(circle, frame, etc.) via the ob_args argument. - This can be used to store various extra values and is especially usefull + This can be used to store various extra values and is especially useful when using daughter classes of Object that needs extra values. This software is licensed under the GPL3 -You should have gotten an copy of the GPL3 license anlonside this software +You should have gotten an copy of the GPL3 license alongside this software Feel free to contribute what ever you want to this engine You can contribute here: https://github.com/lxgr-linux/scrap_engine """ @@ -32,11 +32,15 @@ __author__ = "lxgr " __version__ = "0.3.3" -# TODO: add comments or use more verbose var names (im looking at you "l") - import math import os import threading +import functools + +MAXCACHE_LINE = 512 +MAXCACHE_FRAME = 64 + +# TODO: add comments or use more verbose var names (im looking at you "l") width, height = os.get_terminal_size() @@ -46,6 +50,7 @@ class CoordinateError(Exception): An Error that is thrown, when an object is added to a non-existing part of a map. """ + def __init__(self, ob, map, x, y): self.ob = ob self.x = x @@ -59,6 +64,7 @@ class Map: """ The map, objects can be added to. """ + def __init__(self, height=height - 1, width=width, background="#", dynfps=True): self.height = height @@ -69,7 +75,6 @@ def __init__(self, height=height - 1, width=width, background="#", for _ in range(height)] self.obmap = [[[] for _ in range(width)] for _ in range(height)] self.obs = [] - self.out = "\r\u001b[" + str(self.height) + "A" self.out_old = "" self.out_line = "" @@ -92,15 +97,27 @@ def show(self, init=False): """ Prints the maps content. """ - self.out = "\r\u001b[" + str(self.height) + "A" - for arr in self.map: - self.out_line = "" - for i in arr: - self.out_line += i - self.out += self.out_line - if self.out_old != self.out or self.dynfps is False or init: - print(self.out + "\n\u001b[1000D", end="") - self.out_old = self.out + map = tuple([tuple(arr) for arr in self.map]) + out = self.show_map(self.height, self.show_line,map) + if self.out_old != out or self.dynfps is False or init: + print(out + "\n\u001b[1000D", end="") + self.out_old = out + + @staticmethod + @functools.lru_cache(MAXCACHE_FRAME) + def show_map(height, show_line, map): + out = "\r\u001b[" + str(height) + "A" + for arr in map: + out += show_line(arr) + return out + + @staticmethod + @functools.lru_cache(MAXCACHE_LINE) + def show_line(arr): + out_line = "" + for i in arr: + out_line += i + return out_line def resize(self, height, width, background="#"): """ @@ -119,14 +136,16 @@ def resize(self, height, width, background="#"): try: self.obmap[ob.y][ob.x].append(ob) ob.redraw() - except: # TODO: Check possible exceptions and specify - pass + except Exception as err: # TODO: Check possible exceptions and specify + print("please add specific error handling instead of blank except") + raise err class Submap(Map): """ Behaves just like a map, but it self contains a part of another map. """ + def __init__(self, bmap, x, y, height=height - 1, width=width, dynfps=True): super().__init__(height, width, dynfps=dynfps) del self.background @@ -139,19 +158,35 @@ def remap(self): """ Updates the map (rereads the map, the submap contains a part from) """ - self.map = [[self.bmap.background for _ in range(self.width)] - for _ in range(self.height)] - for sy, y in zip(range(0, self.height), + self.map = self.full_bg(self.bmap.background, self.width, self.height) + """for sy, y in zip(range(0, self.height), range(self.y, self.y + self.height)): for sx, x in zip(range(0, self.width), range(self.x, self.x + self.width)): try: self.map[sy][sx] = self.bmap.map[y][x] - except: # TODO: Check possible exceptions and specify - continue + except Exception as err: # TODO: Check possible exceptions and specify + raise err""" for ob in self.obs: ob.redraw() + @staticmethod + def map_to_parent(): + for sy, y in zip(range(0, self.height), + range(self.y, self.y + self.height)): + for sx, x in zip(range(0, self.width), + range(self.x, self.x + self.width)): + try: + self.map[sy][sx] = self.bmap.map[y][x] + except Exception as err: # TODO: Check possible exceptions and specify + raise err + + @staticmethod + @functools.lru_cache(1) + def full_bg(background, width, height): + return [[background for _ in range(width)] + for _ in range(height)] + def set(self, x, y): """ Changes the coordinates on the map, the submap is at. @@ -175,6 +210,7 @@ class Object: """ An object, containing a character, that can be added to a map. """ + def __init__(self, char, state="solid", arg_proto=None): if arg_proto is None: arg_proto = {} @@ -339,6 +375,7 @@ class ObjectGroup: A datatype used to group objects together and do things with them simultaniuously. """ + def __init__(self, obs): self.y = None self.x = None @@ -412,6 +449,7 @@ class Text(ObjectGroup): A datatype containing a string, that can be added to a map. Different Texts can be added together with the '+' operator. """ + def __init__(self, text, state="solid", esccode="", ob_class=Object, ob_args=None, ignore=""): super().__init__([]) @@ -486,6 +524,7 @@ class Square(ObjectGroup): """ A rectangle, that can be added to a map. """ + def __init__(self, char, width, height, state="solid", ob_class=Object, ob_args=None, threads=False): super().__init__([]) @@ -581,6 +620,7 @@ class Frame(ObjectGroup): That can be added to map. """ + def __init__(self, height, width, corner_chars=None, horizontal_chars=None, vertical_chars=None, state="solid", ob_class=Object, ob_args=None): @@ -683,6 +723,7 @@ class Box(ObjectGroup): A datastucture used to group objects(groups) relative to a certain coordinate, that can be added to a map. """ + def __init__(self, height, width): super().__init__([]) self.height = height @@ -739,6 +780,7 @@ class Circle(Box): """ A circle, that can be added to a map. """ + def __init__(self, char, radius, state="solid", ob_class=Object, ob_args=None): super().__init__(0, 0) @@ -784,6 +826,7 @@ class Line(Box): """ A line described by a vector, that cam be added to map. """ + def __init__(self, char, cx, cy, type="straight", state="solid", ob_class=Object, ob_args=None): super().__init__(0, 0) From 8c44df2d27d8d8e8d4b5369e5099178e55d502d7 Mon Sep 17 00:00:00 2001 From: ttpf-P <65406575+ttpf-P@users.noreply.github.com> Date: Tue, 14 Sep 2021 14:26:36 +0200 Subject: [PATCH 14/28] Get everything running for push --- scrap_engine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 880fd72..3e9fa45 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -159,14 +159,14 @@ def remap(self): Updates the map (rereads the map, the submap contains a part from) """ self.map = self.full_bg(self.bmap.background, self.width, self.height) - """for sy, y in zip(range(0, self.height), + for sy, y in zip(range(0, self.height), range(self.y, self.y + self.height)): for sx, x in zip(range(0, self.width), range(self.x, self.x + self.width)): try: self.map[sy][sx] = self.bmap.map[y][x] except Exception as err: # TODO: Check possible exceptions and specify - raise err""" + raise err for ob in self.obs: ob.redraw() From 42678dd1682e82ae14567ffb19e6f9563ab7d735 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 16:35:59 +0200 Subject: [PATCH 15/28] Rewrote Map.resize to not use try excepts --- scrap_engine.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 75c53de..f1548e2 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -112,12 +112,10 @@ def resize(self, height, width, background="#"): if height > self.height else self.height)] self.width = width self.height = height - for ob in self.obs: - try: - self.obmap[ob.y][ob.x].append(ob) - ob.redraw() - except IndexError: - pass + for obj in self.obs: + if obj.y < height and obj.x < width: + self.obmap[obj.y][obj.x].append(obj) + obj.redraw() class Submap(Map): @@ -606,7 +604,7 @@ def __gen_obs(self): state=self.state) for i, j in zip(self.corner_chars, range(4))] self.horizontals = [Square(char=i, width=self.width - 2, height=1, - state=self.state, ob_class=Object, + state=self.state, ob_class=Object, ob_args={}) for i, j in zip(self.horizontal_chars, range(2))] self.verticals = [Square(char=i, width=1, height=self.height - 2, From c59f9b22926c5f04471f618bb6a66d6abc83badc Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 16:41:23 +0200 Subject: [PATCH 16/28] Changed Submap.remap to avoid using try --- scrap_engine.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index f1548e2..79d0d5c 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -141,12 +141,10 @@ def remap(self): range(self.y, self.y + self.height)): for sx, x in zip(range(0, self.width), range(self.x, self.x + self.width)): - try: + if y < self.bmap.height and x < self.bmap.width: self.map[sy][sx] = self.bmap.map[y][x] - except IndexError: - continue - for ob in self.obs: - ob.redraw() + for obj in self.obs: + obj.redraw() def set(self, x, y): """ From 1c55db106898ee591305e4195c8b8dd822ca459f Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 16:56:35 +0200 Subject: [PATCH 17/28] Fixed typos --- scrap_engine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 79d0d5c..2c87505 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -2,11 +2,11 @@ """ Ascii game engine for the terminal. -The main data scructures are Map and Object. +The main data structures are Map and Object. Maps are objects, Object objects can be added to and then can be shown on the screen. -ObjectGroup and their daughters can be used to automate generetaing, adding, +ObjectGroup and their daughters can be used to automate generating, adding, removing etc. for a list of objects in their defined manner. States: @@ -20,11 +20,11 @@ arg_proto is an dictionary that is given to an object by the programmer or an object_group(circle, frame, etc.) via the ob_args argument. - This can be used to store various extra values and is especially usefull + This can be used to store various extra values and is especially useful when using daughter classes of Object that needs extra values. This software is licensed under the GPL3 -You should have gotten an copy of the GPL3 license anlonside this software +You should have gotten an copy of the GPL3 license alongside this software Feel free to contribute what ever you want to this engine You can contribute here: https://github.com/lxgr-linux/scrap_engine """ From 4cf1bb236b7b58d1171032700e4d12f6461df217 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 22:48:24 +0200 Subject: [PATCH 18/28] Made scrap_bench better --- tests/scrap_bench.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/tests/scrap_bench.py b/tests/scrap_bench.py index 2824bf7..e26a771 100755 --- a/tests/scrap_bench.py +++ b/tests/scrap_bench.py @@ -4,31 +4,32 @@ import time, random, os def main(): + global avr, tcount + os.system("") - map = se.Map(background=" ") + b_map = se.Map(background=" ") text = se.Text("0") avr = se.Text("0") - text.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)-1) - avr.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)) + rectangle = se.Square('\033[34ma\033[0m', 50, 10) + text.add(b_map, round((b_map.width-len(text.text))/2-10), + round(b_map.height/2)-1) + avr.add(b_map, round((b_map.width-len(text.text))/2-10), + round(b_map.height/2)) + rectangle.add(b_map, 0, 0) + tcount = 0 times = 0 - obs = [] - for i in range(50): - for j in range(10): - exec("ob_%s_%s = se.Object('\033[34ma\033[0m')"%(str(i), str(j))) - exec("ob_%s_%s.add(map, i, j)"%(str(i), str(j))) - exec("obs.append(ob_%s_%s)"%(str(i), str(j))) - - map.show(init=True) time2 = 0 + + b_map.show() while True: time0 = time.time() times += time2 - for ob in obs: + for ob in rectangle.obs: ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1])) text.rechar(str(time2)) avr.rechar(str(times/tcount if tcount != 0 else 1)) - map.show() + b_map.show() tcount += 1 time2 = time.time()-time0 @@ -36,4 +37,8 @@ def main(): try: main() except KeyboardInterrupt: - print("KeyboardInterrupt") + print(f"""KeyboardInterrupt + +Screen size: {se.screen_width}x{se.screen_height} +Average frame time: {avr.text}s +Frames drawn: {tcount}""") From 14eaaea6cc2d6eb55137022e2ccec7e958343b38 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 22:49:10 +0200 Subject: [PATCH 19/28] Minor changes --- scrap_engine.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 2c87505..34e4da5 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -82,8 +82,8 @@ def blur_in(self, blurmap, esccode="\033[37m"): "\033[0m") else: self.map[h][w] = " " - for ob in self.obs: - ob.redraw() + for obj in self.obs: + obj.redraw() def show(self, init=False): """ @@ -188,7 +188,7 @@ def add(self, map_, x, y): """ if not 0 <= x < map_.width or not 0 <= y < map_.height: raise CoordinateError(self, map_, x, y) - if "solid" in [ob.state for ob in map_.obmap[y][x]]: + if len(lis := map_.obmap[y][x]) != 0 and lis[-1].state == "solid": return 1 self.backup = map_.map[y][x] self.x = x From cf4e832c423b2d6e3f1327a5e22a25d02ddb9761 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 23:23:22 +0200 Subject: [PATCH 20/28] renamed some vars --- scrap_engine.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index 34e4da5..a9eff83 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -231,9 +231,9 @@ def set(self, x, y): self.x = x self.y = y self.map.map[y][x] = self.char - for ob in self.map.obmap[y][x]: - if ob.state == "float": - ob.action(self) + for obj in self.map.obmap[y][x]: + if obj.state == "float": + obj.action(self) return 0 def redraw(self): @@ -354,8 +354,8 @@ def add_obs(self, obs): """ Adds a list of objects to th group. """ - for ob in obs: - self.add_ob(ob) + for obj in obs: + self.add_ob(obj) def rem_ob(self, ob): """ From e73e76a650639b6ca5b47cc267a4e673405baf1b Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Tue, 14 Sep 2021 23:35:15 +0200 Subject: [PATCH 21/28] Removed exec statements --- scrap_engine.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index a9eff83..7563afb 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -505,15 +505,13 @@ def __create(self): self.__one_line_create(i) def __one_line_create(self, j): - for i in range(self.width): - exec(f"self.ob_{i}_{j} = self.ob_class(self.char, self.state,\ -arg_proto=self.ob_args)") - exec(f"self.obs.append(self.ob_{i}_{j})") + for _ in range(self.width): + self.obs.append(self.ob_class(self.char, self.state, + arg_proto=self.ob_args)) def __one_line_add(self, j): - for i in range(self.width): - exec(f"self.exits.append(self.ob_{i}_{j}.add(self.map, self.x+i,\ -self.y+j))") + for i, obj in enumerate(self.obs[j*self.width : (j+1)*self.width]): + self.exits.append(obj.add(self.map, self.x+i, self.y+j)) def add(self, map_, x, y): """ From 3484759091542b9a7dece25e437e2dff75dd52ab Mon Sep 17 00:00:00 2001 From: ttpf-P <65406575+ttpf-P@users.noreply.github.com> Date: Wed, 15 Sep 2021 12:43:33 +0200 Subject: [PATCH 22/28] Update scrap_bench.py --- tests/scrap_bench.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/tests/scrap_bench.py b/tests/scrap_bench.py index 2824bf7..e26a771 100755 --- a/tests/scrap_bench.py +++ b/tests/scrap_bench.py @@ -4,31 +4,32 @@ import time, random, os def main(): + global avr, tcount + os.system("") - map = se.Map(background=" ") + b_map = se.Map(background=" ") text = se.Text("0") avr = se.Text("0") - text.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)-1) - avr.add(map, round((map.width-len(str(text.text)))/2-10), round(map.height/2)) + rectangle = se.Square('\033[34ma\033[0m', 50, 10) + text.add(b_map, round((b_map.width-len(text.text))/2-10), + round(b_map.height/2)-1) + avr.add(b_map, round((b_map.width-len(text.text))/2-10), + round(b_map.height/2)) + rectangle.add(b_map, 0, 0) + tcount = 0 times = 0 - obs = [] - for i in range(50): - for j in range(10): - exec("ob_%s_%s = se.Object('\033[34ma\033[0m')"%(str(i), str(j))) - exec("ob_%s_%s.add(map, i, j)"%(str(i), str(j))) - exec("obs.append(ob_%s_%s)"%(str(i), str(j))) - - map.show(init=True) time2 = 0 + + b_map.show() while True: time0 = time.time() times += time2 - for ob in obs: + for ob in rectangle.obs: ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1])) text.rechar(str(time2)) avr.rechar(str(times/tcount if tcount != 0 else 1)) - map.show() + b_map.show() tcount += 1 time2 = time.time()-time0 @@ -36,4 +37,8 @@ def main(): try: main() except KeyboardInterrupt: - print("KeyboardInterrupt") + print(f"""KeyboardInterrupt + +Screen size: {se.screen_width}x{se.screen_height} +Average frame time: {avr.text}s +Frames drawn: {tcount}""") From 6e9d8703343165d7bdd45f7859eff33b08c22373 Mon Sep 17 00:00:00 2001 From: ttpf-P <65406575+ttpf-P@users.noreply.github.com> Date: Wed, 15 Sep 2021 12:51:32 +0200 Subject: [PATCH 23/28] Get everything running for push --- tests/scrap_bench.py => scrap_bench.py | 20 ++- examples/scrap_test.py => scrap_test.py | 178 +++++++++++++----------- 2 files changed, 113 insertions(+), 85 deletions(-) rename tests/scrap_bench.py => scrap_bench.py (59%) rename examples/scrap_test.py => scrap_test.py (64%) mode change 100755 => 100644 diff --git a/tests/scrap_bench.py b/scrap_bench.py similarity index 59% rename from tests/scrap_bench.py rename to scrap_bench.py index e26a771..16a1822 100755 --- a/tests/scrap_bench.py +++ b/scrap_bench.py @@ -25,13 +25,29 @@ def main(): while True: time0 = time.time() times += time2 - for ob in rectangle.obs: - ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1])) + """for ob in rectangle.obs: + ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1]))""" + r_list = [] + t1 = time.perf_counter_ns() + for x in range(len(rectangle.obs)): + r_list.append((random.randint(-1, 1), random.randint(-1, 1))) + for ob_id in range(len(rectangle.obs)): + rectangle.obs[ob_id].set(rectangle.obs[ob_id].x+r_list[ob_id][0], rectangle.obs[ob_id].y+r_list[ob_id][1]) + t2 = time.perf_counter_ns() text.rechar(str(time2)) + t3 = time.perf_counter_ns() avr.rechar(str(times/tcount if tcount != 0 else 1)) b_map.show() + t4 = time.perf_counter_ns() + b_map.show() + t5 = time.perf_counter_ns() tcount += 1 time2 = time.time()-time0 + """perf_times = [t1,t2,t3,t4,t5] + for x in range(len(perf_times)-1): + print(str(x)+"\t"+str(perf_times[x+1]-perf_times[x])) + time.sleep(2)""" + if __name__ == "__main__": try: diff --git a/examples/scrap_test.py b/scrap_test.py old mode 100755 new mode 100644 similarity index 64% rename from examples/scrap_test.py rename to scrap_test.py index ee44e1f..e48ae8d --- a/examples/scrap_test.py +++ b/scrap_test.py @@ -194,87 +194,99 @@ def recogniser(): smap.remap() smap.show() # showing smap Map -while True: - if ev == "'w'": - player.direction="t" - player.set(player.x, player.y-1) # Doing something on keypress w - ev=0 - elif ev == "'a'": - player.direction="l" - player.set(player.x-1, player.y) # Doing something different on keypress a - ev=0 - elif ev == "'s'": - player.direction="b" - player.set(player.x, player.y+1) # Doing something more different on keypress s - ev=0 - elif ev == "'d'": - player.direction="r" - player.set(player.x+1, player.y) # Doing yet another different thing on keypress d - ev=0 - elif ev2 == "Key.up": - player0.direction="t" - player0.set(player0.x, player0.y-1) # Doing something on keypress w - ev2=0 - elif ev2 == "Key.left": - player0.direction="l" - player0.set(player0.x-1, player0.y) # Doing something different on keypress a - ev2=0 - elif ev2 == "Key.down": - player0.direction="b" - player0.set(player0.x, player0.y+1) # Doing something more different on keypress s - ev2=0 - elif ev2 == "Key.right": - player0.direction="r" - player0.set(player0.x+1, player0.y) # Doing yet another different thing on keypress d - ev2=0 - elif ev == "'e'": - text2.rechar("thus\nus\nmultiline mext!") - square2.rechar("A") # Doing some weird shit on keypress e - ev=0 - elif ev == "'q'": - # Creating and adding some Object on keypress q - exec("ob_"+str(obcount)+"=se.Object('A', state='solid')") - exec("ob_"+str(obcount)+".add(map, player.x+1, player.y)") - obcount+=1 - ev=0 - elif ev == "'m'": - menu() # Running the menu function on keypress q to open the menu - smap.show(init=True) # The init=True option ensures, the map Map is drawn after closing the menu, even if no changes accured in the map + +time_1 = 0 +time_2 = 0 + +try: + while True: + if ev == "'w'": + player.direction="t" + player.set(player.x, player.y-1) # Doing something on keypress w + ev=0 + elif ev == "'a'": + player.direction="l" + player.set(player.x-1, player.y) # Doing something different on keypress a + ev=0 + elif ev == "'s'": + player.direction="b" + player.set(player.x, player.y+1) # Doing something more different on keypress s + ev=0 + elif ev == "'d'": + player.direction="r" + player.set(player.x+1, player.y) # Doing yet another different thing on keypress d + ev=0 + elif ev2 == "Key.up": + player0.direction="t" + player0.set(player0.x, player0.y-1) # Doing something on keypress w + ev2=0 + elif ev2 == "Key.left": + player0.direction="l" + player0.set(player0.x-1, player0.y) # Doing something different on keypress a + ev2=0 + elif ev2 == "Key.down": + player0.direction="b" + player0.set(player0.x, player0.y+1) # Doing something more different on keypress s + ev2=0 + elif ev2 == "Key.right": + player0.direction="r" + player0.set(player0.x+1, player0.y) # Doing yet another different thing on keypress d + ev2=0 + elif ev == "'e'": + text2.rechar("thus\nus\nmultiline mext!") + square2.rechar("A") # Doing some weird shit on keypress e + ev=0 + elif ev == "'q'": + # Creating and adding some Object on keypress q + exec("ob_"+str(obcount)+"=se.Object('A', state='solid')") + exec("ob_"+str(obcount)+".add(map, player.x+1, player.y)") + obcount+=1 + ev=0 + elif ev == "'m'": + menu() # Running the menu function on keypress q to open the menu + smap.show(init=True) # The init=True option ensures, the map Map is drawn after closing the menu, even if no changes accured in the map + smap.remap() + ev=0 + elif ev == "Key.space": + shoot(player) + ev=0 + elif ev2 == "'#'": + shoot(player0) + ev2=0 + else: + time.sleep(0.05) # Else just wait 0.05 seconds + # Let lui run + if luisframe+20 == framenum: + if lui.x == 20 and luisview == "l": + lui.set(19, 10) + luisview="r" + elif lui.x == 20 and luisview == "r": + lui.set(21, 10) + luisview="l" + elif lui.x == 19 or lui.x == 21: + lui.set(20, 10) + luisframe+=20 + for ob in [player, player0]: + if ob.x+5 > smap.x+smap.width: + smap.set(smap.x+1 ,smap.y) + if ob.x < smap.x+5: + smap.set(smap.x-1 ,smap.y) + for ob in bullets: + if ob.direction == "t": + ob.set(ob.x, ob.y-1) + elif ob.direction == "l": + ob.set(ob.x-1, ob.y) + elif ob.direction == "b": + ob.set(ob.x, ob.y+1) + elif ob.direction == "r": + ob.set(ob.x+1, ob.y) + t2 = time.perf_counter_ns() smap.remap() - ev=0 - elif ev == "Key.space": - shoot(player) - ev=0 - elif ev2 == "'#'": - shoot(player0) - ev2=0 - else: - time.sleep(0.05) # Else just wait 0.05 seconds - # Let lui run - if luisframe+20 == framenum: - if lui.x == 20 and luisview == "l": - lui.set(19, 10) - luisview="r" - elif lui.x == 20 and luisview == "r": - lui.set(21, 10) - luisview="l" - elif lui.x == 19 or lui.x == 21: - lui.set(20, 10) - luisframe+=20 - for ob in [player, player0]: - if ob.x+5 > smap.x+smap.width: - smap.set(smap.x+1 ,smap.y) - if ob.x < smap.x+5: - smap.set(smap.x-1 ,smap.y) - for ob in bullets: - if ob.direction == "t": - ob.set(ob.x, ob.y-1) - elif ob.direction == "l": - ob.set(ob.x-1, ob.y) - elif ob.direction == "b": - ob.set(ob.x, ob.y+1) - elif ob.direction == "r": - ob.set(ob.x+1, ob.y) - smap.remap() - smap.show() # Draw the frame - framenum+=1 + t1 = time.perf_counter_ns() + smap.show() # Draw the frame + time_1 += time.perf_counter_ns() - t1 + time_2 += t1 - t2 + framenum+=1 +except KeyboardInterrupt: + print(str((time_1/framenum)/10**9)+"s") + print(str((time_2/framenum)/10**9)+"s") \ No newline at end of file From 66da19dc6bed6375e2bc85e329507fc674a411fa Mon Sep 17 00:00:00 2001 From: ttpf-P <65406575+ttpf-P@users.noreply.github.com> Date: Fri, 17 Sep 2021 12:03:36 +0200 Subject: [PATCH 24/28] more performance improvements --- scrap_engine.py | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index e969ce5..c88df58 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -32,7 +32,6 @@ __author__ = "lxgr " __version__ = "0.3.3" - import math import os import threading @@ -45,6 +44,8 @@ screen_width, screen_height = os.get_terminal_size() +width, height = screen_width, screen_height # backwards compatibility + class CoordinateError(Exception): """ @@ -98,7 +99,7 @@ def show(self, init=False): Prints the maps content. """ map = tuple([tuple(arr) for arr in self.map]) - out = self.show_map(self.height, self.show_line,map) + out = self.show_map(self.height, self.show_line, map) if self.out_old != out or self.dynfps is False or init: print(out + "\n\u001b[1000D", end="") self.out_old = out @@ -157,24 +158,32 @@ def remap(self): Updates the map (rereads the map, the submap contains a part from) """ self.map = self.full_bg(self.bmap.background, self.width, self.height) - for sy, y in zip(range(0, self.height), + """for sy, y in zip(range(0, self.height), range(self.y, self.y + self.height)): for sx, x in zip(range(0, self.width), range(self.x, self.x + self.width)): if y < self.bmap.height and x < self.bmap.width: - self.map[sy][sx] = self.bmap.map[y][x] + self.map[sy][sx] = self.bmap.map[y][x]""" + self.map = self.map_to_parent(self.height, self.width, self.y, self.x, + tuple([tuple(line) for line in self.map]), + tuple([tuple(line) for line in self.bmap.map])) for obj in self.obs: obj.redraw() - def map_to_parent(self): - for sy, y in zip(range(0, self.height), - range(self.y, self.y + self.height)): - for sx, x in zip(range(0, self.width), - range(self.x, self.x + self.width)): - try: - self.map[sy][sx] = self.bmap.map[y][x] - except IndexError: # TODO: Check possible exceptions and specify - continue + @staticmethod + @functools.lru_cache() + def map_to_parent(height, width, y_, x_, parent, child): + parent = [list(line) for line in parent] + child = [list(line) for line in child] + for sy, y in zip(range(0, height), + range(y_, y_ + height)): + for sx, x in zip(range(0, width), + range(x_, x_ + width)): + try: + parent[sy][sx] = child[y][x] + except IndexError: + continue + return parent @staticmethod @functools.lru_cache(1) @@ -547,11 +556,11 @@ def __create(self): def __one_line_create(self, j): for _ in range(self.width): self.obs.append(self.ob_class(self.char, self.state, - arg_proto=self.ob_args)) + arg_proto=self.ob_args)) def __one_line_add(self, j): - for i, obj in enumerate(self.obs[j*self.width : (j+1)*self.width]): - self.exits.append(obj.add(self.map, self.x+i, self.y+j)) + for i, obj in enumerate(self.obs[j * self.width: (j + 1) * self.width]): + self.exits.append(obj.add(self.map, self.x + i, self.y + j)) def add(self, map_, x, y): """ @@ -648,10 +657,9 @@ def __gen_obs(self): state=self.state, ob_class=Object, ob_args={}) for i, j in zip(self.vertical_chars, range(2))] - def __add_obs(self): for obj, rx, ry in zip(self.corners, [0, self.width - 1, 0, self.width - 1], - [0, 0, self.height - 1, self.height - 1]): + [0, 0, self.height - 1, self.height - 1]): obj.add(self.map, self.x + rx, self.y + ry) for obj, rx, ry in zip(self.horizontals, [1, 1], [0, self.height - 1]): obj.add(self.map, self.x + rx, self.y + ry) From 4d509cc4389c86ed5bb881081a261652ec6ebcff Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Sun, 19 Sep 2021 15:05:43 +0200 Subject: [PATCH 25/28] moved some files back to where they belong to --- scrap_test.py => examples/scrap_test.py | 178 +++++++++++------------- scrap_bench.py => tests/scrap_bench.py | 20 +-- 2 files changed, 85 insertions(+), 113 deletions(-) rename scrap_test.py => examples/scrap_test.py (64%) mode change 100644 => 100755 rename scrap_bench.py => tests/scrap_bench.py (59%) diff --git a/scrap_test.py b/examples/scrap_test.py old mode 100644 new mode 100755 similarity index 64% rename from scrap_test.py rename to examples/scrap_test.py index e48ae8d..ee44e1f --- a/scrap_test.py +++ b/examples/scrap_test.py @@ -194,99 +194,87 @@ def recogniser(): smap.remap() smap.show() # showing smap Map - -time_1 = 0 -time_2 = 0 - -try: - while True: - if ev == "'w'": - player.direction="t" - player.set(player.x, player.y-1) # Doing something on keypress w - ev=0 - elif ev == "'a'": - player.direction="l" - player.set(player.x-1, player.y) # Doing something different on keypress a - ev=0 - elif ev == "'s'": - player.direction="b" - player.set(player.x, player.y+1) # Doing something more different on keypress s - ev=0 - elif ev == "'d'": - player.direction="r" - player.set(player.x+1, player.y) # Doing yet another different thing on keypress d - ev=0 - elif ev2 == "Key.up": - player0.direction="t" - player0.set(player0.x, player0.y-1) # Doing something on keypress w - ev2=0 - elif ev2 == "Key.left": - player0.direction="l" - player0.set(player0.x-1, player0.y) # Doing something different on keypress a - ev2=0 - elif ev2 == "Key.down": - player0.direction="b" - player0.set(player0.x, player0.y+1) # Doing something more different on keypress s - ev2=0 - elif ev2 == "Key.right": - player0.direction="r" - player0.set(player0.x+1, player0.y) # Doing yet another different thing on keypress d - ev2=0 - elif ev == "'e'": - text2.rechar("thus\nus\nmultiline mext!") - square2.rechar("A") # Doing some weird shit on keypress e - ev=0 - elif ev == "'q'": - # Creating and adding some Object on keypress q - exec("ob_"+str(obcount)+"=se.Object('A', state='solid')") - exec("ob_"+str(obcount)+".add(map, player.x+1, player.y)") - obcount+=1 - ev=0 - elif ev == "'m'": - menu() # Running the menu function on keypress q to open the menu - smap.show(init=True) # The init=True option ensures, the map Map is drawn after closing the menu, even if no changes accured in the map - smap.remap() - ev=0 - elif ev == "Key.space": - shoot(player) - ev=0 - elif ev2 == "'#'": - shoot(player0) - ev2=0 - else: - time.sleep(0.05) # Else just wait 0.05 seconds - # Let lui run - if luisframe+20 == framenum: - if lui.x == 20 and luisview == "l": - lui.set(19, 10) - luisview="r" - elif lui.x == 20 and luisview == "r": - lui.set(21, 10) - luisview="l" - elif lui.x == 19 or lui.x == 21: - lui.set(20, 10) - luisframe+=20 - for ob in [player, player0]: - if ob.x+5 > smap.x+smap.width: - smap.set(smap.x+1 ,smap.y) - if ob.x < smap.x+5: - smap.set(smap.x-1 ,smap.y) - for ob in bullets: - if ob.direction == "t": - ob.set(ob.x, ob.y-1) - elif ob.direction == "l": - ob.set(ob.x-1, ob.y) - elif ob.direction == "b": - ob.set(ob.x, ob.y+1) - elif ob.direction == "r": - ob.set(ob.x+1, ob.y) - t2 = time.perf_counter_ns() +while True: + if ev == "'w'": + player.direction="t" + player.set(player.x, player.y-1) # Doing something on keypress w + ev=0 + elif ev == "'a'": + player.direction="l" + player.set(player.x-1, player.y) # Doing something different on keypress a + ev=0 + elif ev == "'s'": + player.direction="b" + player.set(player.x, player.y+1) # Doing something more different on keypress s + ev=0 + elif ev == "'d'": + player.direction="r" + player.set(player.x+1, player.y) # Doing yet another different thing on keypress d + ev=0 + elif ev2 == "Key.up": + player0.direction="t" + player0.set(player0.x, player0.y-1) # Doing something on keypress w + ev2=0 + elif ev2 == "Key.left": + player0.direction="l" + player0.set(player0.x-1, player0.y) # Doing something different on keypress a + ev2=0 + elif ev2 == "Key.down": + player0.direction="b" + player0.set(player0.x, player0.y+1) # Doing something more different on keypress s + ev2=0 + elif ev2 == "Key.right": + player0.direction="r" + player0.set(player0.x+1, player0.y) # Doing yet another different thing on keypress d + ev2=0 + elif ev == "'e'": + text2.rechar("thus\nus\nmultiline mext!") + square2.rechar("A") # Doing some weird shit on keypress e + ev=0 + elif ev == "'q'": + # Creating and adding some Object on keypress q + exec("ob_"+str(obcount)+"=se.Object('A', state='solid')") + exec("ob_"+str(obcount)+".add(map, player.x+1, player.y)") + obcount+=1 + ev=0 + elif ev == "'m'": + menu() # Running the menu function on keypress q to open the menu + smap.show(init=True) # The init=True option ensures, the map Map is drawn after closing the menu, even if no changes accured in the map smap.remap() - t1 = time.perf_counter_ns() - smap.show() # Draw the frame - time_1 += time.perf_counter_ns() - t1 - time_2 += t1 - t2 - framenum+=1 -except KeyboardInterrupt: - print(str((time_1/framenum)/10**9)+"s") - print(str((time_2/framenum)/10**9)+"s") \ No newline at end of file + ev=0 + elif ev == "Key.space": + shoot(player) + ev=0 + elif ev2 == "'#'": + shoot(player0) + ev2=0 + else: + time.sleep(0.05) # Else just wait 0.05 seconds + # Let lui run + if luisframe+20 == framenum: + if lui.x == 20 and luisview == "l": + lui.set(19, 10) + luisview="r" + elif lui.x == 20 and luisview == "r": + lui.set(21, 10) + luisview="l" + elif lui.x == 19 or lui.x == 21: + lui.set(20, 10) + luisframe+=20 + for ob in [player, player0]: + if ob.x+5 > smap.x+smap.width: + smap.set(smap.x+1 ,smap.y) + if ob.x < smap.x+5: + smap.set(smap.x-1 ,smap.y) + for ob in bullets: + if ob.direction == "t": + ob.set(ob.x, ob.y-1) + elif ob.direction == "l": + ob.set(ob.x-1, ob.y) + elif ob.direction == "b": + ob.set(ob.x, ob.y+1) + elif ob.direction == "r": + ob.set(ob.x+1, ob.y) + smap.remap() + smap.show() # Draw the frame + framenum+=1 diff --git a/scrap_bench.py b/tests/scrap_bench.py similarity index 59% rename from scrap_bench.py rename to tests/scrap_bench.py index 16a1822..e26a771 100755 --- a/scrap_bench.py +++ b/tests/scrap_bench.py @@ -25,29 +25,13 @@ def main(): while True: time0 = time.time() times += time2 - """for ob in rectangle.obs: - ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1]))""" - r_list = [] - t1 = time.perf_counter_ns() - for x in range(len(rectangle.obs)): - r_list.append((random.randint(-1, 1), random.randint(-1, 1))) - for ob_id in range(len(rectangle.obs)): - rectangle.obs[ob_id].set(rectangle.obs[ob_id].x+r_list[ob_id][0], rectangle.obs[ob_id].y+r_list[ob_id][1]) - t2 = time.perf_counter_ns() + for ob in rectangle.obs: + ob.set(random.choice([ob.x, ob.x+1, ob.x-1]), random.choice([ob.y, ob.y+1, ob.y-1])) text.rechar(str(time2)) - t3 = time.perf_counter_ns() avr.rechar(str(times/tcount if tcount != 0 else 1)) b_map.show() - t4 = time.perf_counter_ns() - b_map.show() - t5 = time.perf_counter_ns() tcount += 1 time2 = time.time()-time0 - """perf_times = [t1,t2,t3,t4,t5] - for x in range(len(perf_times)-1): - print(str(x)+"\t"+str(perf_times[x+1]-perf_times[x])) - time.sleep(2)""" - if __name__ == "__main__": try: From d297d92c7aff99291631a9fb26e58244e461d967 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Sun, 19 Sep 2021 15:20:49 +0200 Subject: [PATCH 26/28] Changed some stuff back --- scrap_engine.py | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index c88df58..d0ab05c 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -40,13 +40,8 @@ MAXCACHE_LINE = 512 MAXCACHE_FRAME = 64 -# TODO: add comments or use more verbose var names (im looking at you "l") - screen_width, screen_height = os.get_terminal_size() -width, height = screen_width, screen_height # backwards compatibility - - class CoordinateError(Exception): """ An Error that is thrown, when an object is added to a non-existing @@ -98,23 +93,23 @@ def show(self, init=False): """ Prints the maps content. """ - map = tuple([tuple(arr) for arr in self.map]) - out = self.show_map(self.height, self.show_line, map) - if self.out_old != out or self.dynfps is False or init: + map = (tuple(arr) for arr in self.map) + out = self.__show_map(self.height, self.__show_line, map) + if self.out_old != out or not self.dynfps or init: print(out + "\n\u001b[1000D", end="") self.out_old = out @staticmethod @functools.lru_cache(MAXCACHE_FRAME) - def show_map(height, show_line, map): - out = "\r\u001b[" + str(height) + "A" + def __show_map(height, show_line, map): + out = f"\r\u001b[{height}A" for arr in map: out += show_line(arr) return out @staticmethod @functools.lru_cache(MAXCACHE_LINE) - def show_line(arr): + def __show_line(arr): out_line = "" for char in arr: out_line += char @@ -157,22 +152,16 @@ def remap(self): """ Updates the map (rereads the map, the submap contains a part from) """ - self.map = self.full_bg(self.bmap.background, self.width, self.height) - """for sy, y in zip(range(0, self.height), - range(self.y, self.y + self.height)): - for sx, x in zip(range(0, self.width), - range(self.x, self.x + self.width)): - if y < self.bmap.height and x < self.bmap.width: - self.map[sy][sx] = self.bmap.map[y][x]""" - self.map = self.map_to_parent(self.height, self.width, self.y, self.x, - tuple([tuple(line) for line in self.map]), - tuple([tuple(line) for line in self.bmap.map])) + self.map = self.__full_bg(self.bmap.background, self.width, self.height) + self.map = self.__map_to_parent(self.height, self.width, self.y, self.x, + (tuple(line) for line in self.map), + (tuple(line) for line in self.bmap.map)) for obj in self.obs: obj.redraw() @staticmethod @functools.lru_cache() - def map_to_parent(height, width, y_, x_, parent, child): + def __map_to_parent(height, width, y_, x_, parent, child): parent = [list(line) for line in parent] child = [list(line) for line in child] for sy, y in zip(range(0, height), @@ -187,7 +176,7 @@ def map_to_parent(height, width, y_, x_, parent, child): @staticmethod @functools.lru_cache(1) - def full_bg(background, width, height): + def __full_bg(background, width, height): return [[background for _ in range(width)] for _ in range(height)] From 2cecdf40f846354c208112c5f8ca7fda5afe5f68 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Sun, 19 Sep 2021 15:26:10 +0200 Subject: [PATCH 27/28] Undid some changes --- scrap_engine.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scrap_engine.py b/scrap_engine.py index d0ab05c..2709b57 100755 --- a/scrap_engine.py +++ b/scrap_engine.py @@ -48,12 +48,12 @@ class CoordinateError(Exception): part of a map. """ - def __init__(self, ob, map_, x, y): - self.ob = ob + def __init__(self, obj, map_, x, y): + self.ob = obj self.x = x self.y = y self.map = map_ - super().__init__(f"The {ob}s coordinate ({x}|{y}) is \ + super().__init__(f"The {obj}s coordinate ({x}|{y}) is \ not in {self.map.width - 1}x{self.map.height - 1}") @@ -62,8 +62,8 @@ class Map: The map, objects can be added to. """ - def __init__(self, height=screen_height - 1, width=screen_width, background="#", - dynfps=True): + def __init__(self, height=screen_height - 1, width=screen_width, + background="#", dynfps=True): self.height = height self.width = width self.dynfps = dynfps @@ -154,8 +154,8 @@ def remap(self): """ self.map = self.__full_bg(self.bmap.background, self.width, self.height) self.map = self.__map_to_parent(self.height, self.width, self.y, self.x, - (tuple(line) for line in self.map), - (tuple(line) for line in self.bmap.map)) + (tuple(line) for line in self.map), + (tuple(line) for line in self.bmap.map)) for obj in self.obs: obj.redraw() From 1ffc91817e19bfe4d678910f2edcb767812453a0 Mon Sep 17 00:00:00 2001 From: lxgr-linux Date: Sun, 19 Sep 2021 15:40:15 +0200 Subject: [PATCH 28/28] Updated docs --- docs/DOCS.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/DOCS.md b/docs/DOCS.md index d004512..67d9d95 100644 --- a/docs/DOCS.md +++ b/docs/DOCS.md @@ -312,12 +312,13 @@ Changes the radius of the circle ### scrap_engine.Line A line that can be drawn on the map, that's described by a vector. This is a daughter class of ```scrap_engine.Box``` and shares all its methods. The unrounded coordinates of the single points Objects of the line are passed in arg_proto to the Objects. -#### Method ```scrap_engine.Line.__init__(self, char, cx, cy, state="straight", state="solid", ob_class=Object, ob_args={})``` +#### Method ```scrap_engine.Line.__init__(self, char, cx, cy, l_type="straight", state="solid", ob_class=Object, ob_args={})``` Constructor. - char:```String``` Character used for the circle - cx: ```float``` X component of the vector - cy: ```float``` Y component of the vector - state:```String``` State ```"solid"``` or ```"float"```, that indices the behaviour of the Object. ```"solid"``` means that not other objects can be put over the object, ```"float"``` means that it is possible. +- l_type:```String``` The type the line should have `straight` or `crippled` - ob_class:```class``` The class of the objects in the label, that should be used - ob_args:```dictionary``` This dictionary is passed as ```arg_proto``` to the objects @@ -360,8 +361,8 @@ A wrapper for ```scrap_engine.Submap.show()``` and ```scrap_engine.Submap.remap( ### CoordinateError The CoordinateError is raised, when an Object is tried to add to an impossible coordinate. Its' attributes are: -- ob:`scrap_engine.Object` The Object that's tried to add -- map:`scrap_engine.Map` The Map the Object is tried to add to +- obj:`scrap_engine.Object` The Object that's tried to add +- map_:`scrap_engine.Map` The Map the Object is tried to add to - x:`int` The x coordinate the Object is tried to add to - y:`int` The y coordinate the Object is tried to add to