Merxlc

UI.py

May 3rd, 2021
624
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import time
  3. import os
  4. from IPython.display import clear_output
  5.  
  6. class Canvas:
  7.  
  8.     def __init__(self, width=60, height=30):
  9.         self.height = height
  10.         self.width = width
  11.         self.objects = []
  12.  
  13.     def draw(self):
  14.         lines = []
  15.  
  16.         specials = {}
  17.         for obj in self.objects:
  18.             if obj.__class__ == Square:
  19.  
  20.                 tl = (obj.x, obj.y)
  21.                 specials[tl] = '#'
  22.                 tr = (obj.x+obj.width+1, obj.y)
  23.                 specials[tr] = '#'
  24.                 bl = (obj.x, obj.y+obj.height+1)
  25.                 specials[bl] = '#'
  26.                 br = (obj.x+obj.width+1, obj.y+obj.height+1)
  27.                 specials[br] = '#'
  28.  
  29.                 for x in range(obj.width):
  30.                     coords = (obj.x+x+1, obj.y)
  31.                     specials[coords] = '-'
  32.  
  33.                 for x in range(obj.width):
  34.                     coords = (obj.x+x+1, obj.y+obj.height+1)
  35.                     specials[coords] = '-'
  36.  
  37.                 for y in range(obj.height):
  38.                     coords = (obj.x, obj.y+y+1)
  39.                     specials[coords] = '|'
  40.  
  41.                 for y in range(obj.height):
  42.                     coords = (obj.x+obj.width+1, obj.y+y+1)
  43.                     specials[coords] = '|'
  44.  
  45.                 specials.update(obj.textcoords)
  46.  
  47.         lines.append('#'+('-'*self.width)+'#')
  48.         for y in range(self.height):
  49.             thisline=''
  50.             thisline += '|'
  51.             for x in range(self.width):
  52.                 if (x, y) in specials:
  53.                     thisline += specials[(x, y)]
  54.                 else:
  55.                     thisline += ' '
  56.             thisline += '|'
  57.             lines.append(thisline)
  58.         lines.append('#'+('-'*self.width)+'#')
  59.         clear_output()
  60.         print('\n'.join(lines))
  61.  
  62. def wrap_text(text, max_width):
  63.     words = text.split(' ')
  64.     final = []
  65.     currentstr = []
  66.     lengthcounter = 0
  67.     for word in words:
  68.         lengthcounter += 1
  69.         lengthcounter += len(word)
  70.         if lengthcounter > max_width:
  71.             final.append(' '.join(currentstr))
  72.             currentstr = [word]
  73.             lengthcounter = len(word)
  74.         else:
  75.             currentstr.append(word)
  76.     final.append(' '.join(currentstr))
  77.     return final
  78.  
  79. def coords_from_text(lines):
  80.     final = {}
  81.     for y, text in enumerate(lines):
  82.         for x, char in enumerate(text):
  83.             final[(x, y)] = char
  84.     return final
  85.  
  86. def tot(t1, t2):
  87.     return (t1[0] + t2[0], t1[1] + t2[1])
  88.  
  89. class Square:
  90.     def __init__(self, x, y, width, height, text=''):
  91.         self.x = x
  92.         self.y = y
  93.         self.width = width
  94.         self.height = height
  95.         self.text = text
  96.         self.textcoords = coords_from_text(wrap_text(text, width))
  97.         self.textcoords = {tot((x+1, y+1), coord): self.textcoords[coord] for coord in self.textcoords}
  98.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×