Created
July 24, 2023 21:47
-
-
Save Gertkeno/4c3608d86c64347eee9b66a39a8bfe92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Screen: | |
def __init__(self, size): | |
self.size = size | |
# create an array with `size` lines of `size` dots | |
# multiplying a string like `"hello world! " * 20` | |
# then multiplying the array | |
self.screen = ['.' * size] * size | |
def __str__(self): | |
return str.join("\n", self.screen) | |
def present(self): | |
for line in self.screen: | |
for char in line: | |
# normally `print` ends with a new line, we | |
# set it to empty so we can print many dots | |
# on the same line. | |
print(char, end="") | |
print() | |
def clear(self): | |
# same as __init__ | |
self.screen = ['.' * self.size] * self.size | |
def draw(self, position, char): | |
# python does not allow modifying strings, but we can | |
# convert them into a list/array and then combine | |
# the list back into a string. Ugly, i know. | |
ct = list(self.screen[position.y]) | |
ct[position.x] = char | |
self.screen[position.y] = str.join("", ct) | |
class Point: | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
screen = Screen(20) | |
screen.draw(Point(19, 15), "@") | |
screen.present() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment