Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import os
- from IPython.display import clear_output
- class Canvas:
- def __init__(self, width=60, height=30):
- self.height = height
- self.width = width
- self.objects = []
- def draw(self):
- lines = []
- specials = {}
- for obj in self.objects:
- if obj.__class__ == Square:
- tl = (obj.x, obj.y)
- specials[tl] = '#'
- tr = (obj.x+obj.width+1, obj.y)
- specials[tr] = '#'
- bl = (obj.x, obj.y+obj.height+1)
- specials[bl] = '#'
- br = (obj.x+obj.width+1, obj.y+obj.height+1)
- specials[br] = '#'
- for x in range(obj.width):
- coords = (obj.x+x+1, obj.y)
- specials[coords] = '-'
- for x in range(obj.width):
- coords = (obj.x+x+1, obj.y+obj.height+1)
- specials[coords] = '-'
- for y in range(obj.height):
- coords = (obj.x, obj.y+y+1)
- specials[coords] = '|'
- for y in range(obj.height):
- coords = (obj.x+obj.width+1, obj.y+y+1)
- specials[coords] = '|'
- specials.update(obj.textcoords)
- lines.append('#'+('-'*self.width)+'#')
- for y in range(self.height):
- thisline=''
- thisline += '|'
- for x in range(self.width):
- if (x, y) in specials:
- thisline += specials[(x, y)]
- else:
- thisline += ' '
- thisline += '|'
- lines.append(thisline)
- lines.append('#'+('-'*self.width)+'#')
- clear_output()
- print('\n'.join(lines))
- def wrap_text(text, max_width):
- words = text.split(' ')
- final = []
- currentstr = []
- lengthcounter = 0
- for word in words:
- lengthcounter += 1
- lengthcounter += len(word)
- if lengthcounter > max_width:
- final.append(' '.join(currentstr))
- currentstr = [word]
- lengthcounter = len(word)
- else:
- currentstr.append(word)
- final.append(' '.join(currentstr))
- return final
- def coords_from_text(lines):
- final = {}
- for y, text in enumerate(lines):
- for x, char in enumerate(text):
- final[(x, y)] = char
- return final
- def tot(t1, t2):
- return (t1[0] + t2[0], t1[1] + t2[1])
- class Square:
- def __init__(self, x, y, width, height, text=''):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.text = text
- self.textcoords = coords_from_text(wrap_text(text, width))
- self.textcoords = {tot((x+1, y+1), coord): self.textcoords[coord] for coord in self.textcoords}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement