44# Feel free to contribute what ever you want to this engine
55# You can contribute here: https://github.com/lxgr-linux/scrap_engine
66
7- import time , os , threading , math
7+ import os , threading , math
88
99width , height = os .get_terminal_size ()
1010
@@ -14,16 +14,19 @@ def __init__(self, ob, map, x, y):
1414 self .x = x
1515 self .y = y
1616 self .map = map
17- super ().__init__ (f"The { ob } s coordinate ({ x } |{ y } ) is not in { map .width - 1 } x{ map .height - 1 } " )
17+ super ().__init__ (f"The { ob } s coordinate ({ x } |{ y } ) is \
18+ not in { map .width - 1 } x{ map .height - 1 } " )
1819
1920
2021class Map ():
21- def __init__ (self , height = height - 1 , width = width , background = "#" , dynfps = True ):
22+ def __init__ (self , height = height - 1 , width = width , background = "#" ,
23+ dynfps = True ):
2224 self .height = height
2325 self .width = width
2426 self .dynfps = dynfps
2527 self .background = background
26- self .map = [[self .background for j in range (width )] for i in range (height )]
28+ self .map = [[self .background for j in range (width )]
29+ for i in range (height )]
2730 self .obmap = [[[] for j in range (width )] for i in range (height )]
2831 self .obs = []
2932 self .out_old = ""
@@ -32,7 +35,9 @@ def blur_in(self, blurmap, esccode="\033[37m"):
3235 for l in range (self .height ):
3336 for i in range (self .width ):
3437 if blurmap .map [l ][i ] != " " :
35- self .map [l ][i ] = esccode + blurmap .map [l ][i ].replace ("\033 [0m" , "" )[- 1 ]+ "\033 [0m"
38+ self .map [l ][i ] = (esccode +
39+ blurmap .map [l ][i ].replace ("\033 [0m" , "" )[- 1 ] +
40+ "\033 [0m" )
3641 else :
3742 self .map [l ][i ] = " "
3843 for ob in self .obs :
@@ -51,8 +56,12 @@ def show(self, init=False):
5156
5257 def resize (self , height , width , background = "#" ):
5358 self .background = background
54- self .map = [[self .background for j in range (width )] for i in range (height )]
55- self .obmap = [[[] for j in range (width if width > self .width else self .width )] for i in range (height if height > self .height else self .height )]
59+ self .map = [[self .background for j in range (width )]
60+ for i in range (height )]
61+ self .obmap = [[[] for j in range (width
62+ if width > self .width else self .width )]
63+ for i in range (height
64+ if height > self .height else self .height )]
5665 self .width = width
5766 self .height = height
5867 for ob in self .obs :
@@ -71,16 +80,20 @@ def __init__(self, bmap, x, y, height=height-1, width=width, dynfps=True):
7180 self .x = x
7281 self .dynfps = dynfps
7382 self .bmap = bmap
74- self .map = [[self .bmap .background for j in range (width )] for i in range (height )]
83+ self .map = [[self .bmap .background for j in range (width )]
84+ for i in range (height )]
7585 self .obmap = [[[] for j in range (width )] for i in range (height )]
7686 self .obs = []
7787 self .out_old = ""
7888 self .remap ()
7989
8090 def remap (self ):
81- self .map = [[self .bmap .background for j in range (self .width )] for i in range (self .height )]
82- for sy , y in zip (range (0 , self .height ), range (self .y , self .y + self .height )):
83- for sx , x in zip (range (0 , self .width ), range (self .x , self .x + self .width )):
91+ self .map = [[self .bmap .background for j in range (self .width )]
92+ for i in range (self .height )]
93+ for sy , y in zip (range (0 , self .height ),
94+ range (self .y , self .y + self .height )):
95+ for sx , x in zip (range (0 , self .width ),
96+ range (self .x , self .x + self .width )):
8497 try :
8598 self .map [sy ][sx ] = self .bmap .map [y ][x ]
8699 except :
@@ -89,7 +102,7 @@ def remap(self):
89102 ob .redraw ()
90103
91104 def set (self , x , y ):
92- if x < 0 or y < 0 : #or x+self.width>self.bmap.width or y+self.height>self.bmap.height:
105+ if x < 0 or y < 0 :
93106 return 1
94107 self .x = x
95108 self .y = y
@@ -106,7 +119,8 @@ def __init__(self, char, state="solid", arg_proto={}):
106119 self .char = char
107120 self .state = state
108121 self .added = False
109- self .arg_proto = arg_proto # This was added to enable more than the default args for custom objects in Text and Square
122+ self .arg_proto = arg_proto # This was added to enable more than the
123+ # default args for custom objects in Text and Square
110124
111125 def add (self , map , x , y ):
112126 if not (0 <= x < map .width ) or not (0 <= y < map .height ):
@@ -164,7 +178,8 @@ def redraw(self):
164178 return 0
165179
166180 def __backup_setter (self ):
167- if len (self .map .obmap [self .y ][self .x ]) > self .map .obmap [self .y ][self .x ].index (self )+ 1 :
181+ if (len (self .map .obmap [self .y ][self .x ])
182+ > self .map .obmap [self .y ][self .x ].index (self )+ 1 ):
168183 self .map .obmap [self .y ][self .x ][self .map .obmap [self .y ][self .x ].index (self )+ 1 ].backup = self .backup
169184 else :
170185 self .map .map [self .y ][self .x ] = self .backup
@@ -253,7 +268,8 @@ def set_state(self, state):
253268
254269
255270class Text (ObjectGroup ):
256- def __init__ (self , text , state = "solid" , esccode = "" , ob_class = Object , ob_args = {}, ignore = "" ):
271+ def __init__ (self , text , state = "solid" , esccode = "" , ob_class = Object ,
272+ ob_args = {}, ignore = "" ):
257273 self .obs = []
258274 self .ob_class = ob_class
259275 self .added = False
@@ -280,7 +296,8 @@ def __texter(self, text):
280296 for i , char in enumerate (text ):
281297 if self .esccode != "" :
282298 char = self .esccode + char + "\033 [0m"
283- self .obs .append (self .ob_class (char , self .state , arg_proto = self .ob_args ))
299+ self .obs .append (self .ob_class (char , self .state ,
300+ arg_proto = self .ob_args ))
284301 for ob in self .obs :
285302 ob .group = self
286303
@@ -314,7 +331,8 @@ def rechar(self, text, esccode=""):
314331
315332
316333class Square (ObjectGroup ):
317- def __init__ (self , char , width , height , state = "solid" , ob_class = Object , ob_args = {}, threads = False ):
334+ def __init__ (self , char , width , height , state = "solid" , ob_class = Object ,
335+ ob_args = {}, threads = False ):
318336 self .obs = []
319337 self .ob_class = ob_class
320338 self .width = width
@@ -332,26 +350,30 @@ def __init__(self, char, width, height, state="solid", ob_class=Object, ob_args=
332350 def __create (self ):
333351 for l in range (self .height ):
334352 if self .threads :
335- threading .Thread (target = self .__one_line_create , args = (l ,), daemon = True ).start ()
353+ threading .Thread (target = self .__one_line_create ,
354+ args = (l ,), daemon = True ).start ()
336355 else :
337356 self .__one_line_create (l )
338357
339358 def __one_line_create (self , l ):
340359 for i in range (self .width ):
341- exec ("self.ob_" + str (i )+ "_" + str (l )+ " = self.ob_class(self.char, self.state, arg_proto=self.ob_args)" )
342- exec ("self.obs.append(self.ob_" + str (i )+ "_" + str (l )+ ")" )
360+ exec (f"self.ob_{ i } _{ l } = self.ob_class(self.char, self.state,\
361+ arg_proto=self.ob_args)" )
362+ exec (f"self.obs.append(self.ob_{ i } _{ l } )" )
343363
344364 def __one_line_add (self , l ):
345365 for i in range (self .width ):
346- exec ("self.exits.append(self.ob_" + str (i )+ "_" + str (l )+ ".add(self.map, self.x+i, self.y+l))" )
366+ exec (f"self.exits.append(self.ob_{ i } _{ l } .add(self.map, self.x+i,\
367+ self.y+l))" )
347368
348369 def add (self , map , x , y ):
349370 self .x = x
350371 self .y = y
351372 self .map = map
352373 for l in range (self .height ):
353374 if self .threads :
354- threading .Thread (target = self .__one_line_add , args = (l ,), daemon = True ).start ()
375+ threading .Thread (target = self .__one_line_add , args = (l ,),
376+ daemon = True ).start ()
355377 else :
356378 self .__one_line_add (l )
357379 self .added = True
@@ -382,19 +404,31 @@ def resize(self, width, height):
382404
383405
384406class Frame (ObjectGroup ):
385- def __init__ (self , height , width , corner_chars = ["+" , "+" , "+" , "+" ], horizontal_chars = ["-" , "-" ], vertical_chars = ["|" , "|" ], state = "solid" , ob_class = Object , ob_args = {}):
407+ def __init__ (self , height , width , corner_chars = ["+" , "+" , "+" , "+" ],
408+ horizontal_chars = ["-" , "-" ], vertical_chars = ["|" , "|" ],
409+ state = "solid" , ob_class = Object , ob_args = {}):
386410 self .height = height
387411 self .width = width
388412 self .ob_class = ob_class
389413 self .ob_args = ob_args
390414 self .added = False
391415 self .state = state
392- self .corners = [self .ob_class (i , arg_proto = self .ob_args , state = self .state ) for i , j in zip (corner_chars , range (4 ))]
393- 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 ))]
394- 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 ))]
416+ self .corner_chars = corner_chars
417+ self .horizontal_chars = horizontal_chars
418+ self .vertical_chars = vertical_chars
419+ self .corners = [self .ob_class (i , arg_proto = self .ob_args ,
420+ state = self .state )
421+ for i , j in zip (corner_chars , range (4 ))]
422+ self .horizontals = [Square (char = i , width = self .width - 2 , height = 1 ,
423+ state = self .state , ob_class = Object , ob_args = {})
424+ for i , j in zip (horizontal_chars , range (2 ))]
425+ self .verticals = [Square (char = i , width = 1 , height = self .height - 2 ,
426+ state = self .state , ob_class = Object , ob_args = {})
427+ for i , j in zip (vertical_chars , range (2 ))]
395428
396429 def __add_obs (self ):
397- for ob , rx , ry in zip (self .corners , [0 , self .width - 1 , 0 , self .width - 1 ], [0 , 0 , self .height - 1 , self .height - 1 ]):
430+ for ob , rx , ry in zip (self .corners , [0 , self .width - 1 , 0 , self .width - 1 ],
431+ [0 , 0 , self .height - 1 , self .height - 1 ]):
398432 ob .add (self .map , self .x + rx , self .y + ry )
399433 for ob , rx , ry in zip (self .horizontals , [1 , 1 ], [0 , self .height - 1 ]):
400434 ob .add (self .map , self .x + rx , self .y + ry )
@@ -415,7 +449,8 @@ def set(self, x, y):
415449 ob .remove ()
416450 self .__add_obs ()
417451
418- def rechar (self , corner_chars = ["+" , "+" , "+" , "+" ], horizontal_char = "-" , vertical_char = "|" ):
452+ def rechar (self , corner_chars = ["+" , "+" , "+" , "+" ], horizontal_char = "-" ,
453+ vertical_char = "|" ):
419454 for ob , c in zip (self .corners , corner_chars ):
420455 ob .rechar (c )
421456 for ob in self .horizontals :
@@ -428,6 +463,17 @@ def remove(self):
428463 ob .remove ()
429464 self .added = False
430465
466+ def resize (self , height , width ):
467+ added = self .added
468+ if added :
469+ self .remove ()
470+ self .__init__ (height , width , corner_chars = self .corner_chars ,
471+ horizontal_chars = self .horizontal_chars ,
472+ vertical_chars = self .vertical_chars , state = self .state ,
473+ ob_class = self .ob_class , ob_args = self .ob_args )
474+ if added :
475+ self .add (self .map , self .x , self .y )
476+
431477
432478class Box (ObjectGroup ):
433479 def __init__ (self , height , width ):
@@ -462,9 +508,14 @@ def remove(self):
462508 ob .remove ()
463509 self .added = False
464510
511+ def resize (self , height , width ):
512+ self .heigth = height
513+ self .width = width
514+
465515
466516class Circle (Box ):
467- def __init__ (self , char , radius , state = "solid" , ob_class = Object , ob_args = {}):
517+ def __init__ (self , char , radius , state = "solid" , ob_class = Object ,
518+ ob_args = {}):
468519 super ().__init__ (0 , 0 )
469520 self .char = char
470521 self .ob_class = ob_class
@@ -477,7 +528,8 @@ def __gen(self, radius):
477528 for i in range (- (int (radius )+ 1 ), int (radius + 1 )+ 1 ):
478529 for j in range (- (int (radius )+ 1 ), int (radius + 1 )+ 1 ):
479530 if math .sqrt ((i )** 2 + (j )** 2 ) <= radius :
480- self .add_ob (self .ob_class (self .char , state = self .state , arg_proto = self .ob_args ), i , j )
531+ self .add_ob (self .ob_class (self .char , state = self .state ,
532+ arg_proto = self .ob_args ), i , j )
481533
482534 def rechar (self , char ):
483535 self .char = char
@@ -496,7 +548,8 @@ def resize(self, radius):
496548
497549
498550class Line (Box ):
499- def __init__ (self , char , cx , cy , type = "straight" , state = "solid" , ob_class = Object , ob_args = {}):
551+ def __init__ (self , char , cx , cy , type = "straight" , state = "solid" ,
552+ ob_class = Object , ob_args = {}):
500553 super ().__init__ (0 , 0 )
501554 self .char = char
502555 self .ob_class = ob_class
@@ -512,12 +565,16 @@ def __gen(self, cx, cy):
512565 for i in range (int (math .sqrt (cx ** 2 ))):
513566 i = int (cx / math .sqrt (cx ** 2 )* i )
514567 j = {"straight" : int , "crippled" : round }[self .type ](cy * i / cx )
515- self .add_ob (self .ob_class (self .char , state = self .state , arg_proto = {** self .ob_args , ** {"x" : i , "y" : cy * i / cx }}), i , j )
568+ self .add_ob (self .ob_class (self .char , state = self .state ,
569+ arg_proto = {** self .ob_args , ** {"x" : i , "y" : cy * i / cx }}),
570+ i , j )
516571 else :
517572 for j in range (int (math .sqrt (cy ** 2 ))):
518573 j = int (cy / math .sqrt (cy ** 2 )* j )
519574 i = {"straight" : int , "crippled" : round }[self .type ](cx * j / cy )
520- self .add_ob (self .ob_class (self .char , state = self .state , arg_proto = {** self .ob_args , ** {"x" : cx * j / cy , "y" : j }}), i , j )
575+ self .add_ob (self .ob_class (self .char , state = self .state ,
576+ arg_proto = {** self .ob_args , ** {"x" : cx * j / cy , "y" : j }}),
577+ i , j )
521578
522579 def rechar (self , char ):
523580 self .char = char
0 commit comments