Skip to content

Commit

Permalink
updated comments + working pagination for show
Browse files Browse the repository at this point in the history
  • Loading branch information
w84death committed Jul 28, 2023
1 parent efe3081 commit 4ff2f36
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 140 deletions.
15 changes: 0 additions & 15 deletions ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
"""

class Ansi:
"""
A class to handle the ANSI escape sequences, text attributes, and colors.
"""
def __init__(self):
"""
Initialize the ANSI object.
"""
self.text_attributes = {
"Bold": "\033[1m",
"Faint": "\033[2m",
Expand Down Expand Up @@ -46,24 +40,15 @@ def __init__(self):
}

def list_sequences(self, title, sequences):
"""
List a set of sequences with a title.
"""
print(f"\033[0;1m{title}\033[0m\n")
for name, code in sequences.items():
print(f"{code}{name}\t\t{code}\033[0m")
self.reset_attributes()

def reset_attributes(self):
"""
Reset all attributes.
"""
print("\033[0m")

def run(self, argument=""):
"""
Shows all available attributes.
"""
print("\n\033[1mANSI Escape Sequences\n")
print("\033[0mReset all attributes\t\\033[0m\n")

Expand Down
20 changes: 1 addition & 19 deletions buzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
]

class Buzz:
"""
A class to handle the Buzz functionalities.
"""
def __init__(self):
"""
Initialize the Buzz object.
"""
self.name = "Buzz"
self.notes = {
'C': 261,
Expand All @@ -46,9 +40,6 @@ def __init__(self):
self.msg("Initialized.")

def play_note(self, note, duration):
"""
Play a specific note for a certain duration.
"""
if note != ' ':
self.buzzer.freq(int(self.notes[note]*0.5))
self.buzzer.duty_u16(BUZZER_DUTY)
Expand All @@ -64,9 +55,6 @@ def play_note(self, note, duration):
self.buzzer.duty_u16(0)

def demo(self, note_duration):
"""
Play a demo of songs in a new thread.
"""
self.msg("Playing demo.\nPress Ctrl+C to stop.\n")

while True:
Expand All @@ -90,16 +78,10 @@ def demo(self, note_duration):
break

def stop(self):
"""
Stop the demo.
"""
time.sleep(0.1)
self.buzzer.duty_u16(0)

def msg(self, message):
"""
Print a message from the program.
"""
def msg(self, message)
print(f"{self.name} : {message}")

def run(self, note_duration=NOTE_DURATION):
Expand Down
18 changes: 0 additions & 18 deletions duck.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
import random

class Duck:
"""
A class to handle the Duck functionalities.
"""
def __init__(self):
"""
Initialize the Duck object.
"""
self.name = "Yellow Duck"
self.pixels = neopixel.NeoPixel(machine.Pin(29),5*5)
self.pixels.fill((0,0,0))
Expand Down Expand Up @@ -100,17 +94,11 @@ def __init__(self):
}

def draw(self,bitmap):
"""
Draw a specific frame.
"""
for i in range(25):
self.pixels[24-i]=self.palette[bitmap[i]]
self.pixels.write()

def hello(self):
"""
Play a duck animation in a new thread.
"""
self.msg("Aproching..")
anim_intro = self.animations["buddy"][:4]
for frame in anim_intro:
Expand All @@ -129,9 +117,6 @@ def hello(self):
break

def bye(self):
"""
Stop the duck animation.
"""
anim_outro = self.animations["buddy"][:4][::-1]
self.msg("Duck swims away...")
for frame in anim_outro:
Expand All @@ -141,9 +126,6 @@ def bye(self):
self.pixels.write()

def msg(self, message):
"""
Print a message from the program.
"""
print(f"{self.name} : {message}")

def run(self, argument=""):
Expand Down
75 changes: 46 additions & 29 deletions life.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def random_seed(self):

def update_world(self):
"""
Update the world with the temporary world.
Update the world with calculated next state of the world
"""
for i in range(self.world_size):
self.world[i] = self.temp[i]
self.world[i] = self.future_world[i]

def get_cell_value(self,i):
"""
Get the cell value.
Returns 0 if asking for a cell outside the world.
"""
if i<0 or i>=len(self.world):
return 0
Expand All @@ -66,44 +66,61 @@ def check_world(self):
Check the world for the next generation.
"""
i=0
off=self.world_width
offset=WORLD_WIDTH
stable = True

for cell in self.world:
# Check eight closest cells
density=0

# left and right
# cell is not on the left edge
if i%self.world_size-1>0:
density += self.get_cell_value(i-1)
if i%self.world_size-1<self.world_width:
# cell is not on the right edge
if i%self.world_size-1<WORLD_WIDTH:
density += self.get_cell_value(i+1)
density += self.get_cell_value(i-off+1)
density += self.get_cell_value(i-off)
density += self.get_cell_value(i-off-1)

density += self.get_cell_value(i+off+1)
density += self.get_cell_value(i+off)
density += self.get_cell_value(i+off-1)

# top row
density += self.get_cell_value(i-offset+1)
density += self.get_cell_value(i-offset)
density += self.get_cell_value(i-offset-1)

# bottom row
density += self.get_cell_value(i+offset+1)
density += self.get_cell_value(i+offset)
density += self.get_cell_value(i+offset-1)

# The rules of life..
if cell == 1: # Cell is alive
if density<2 or density>3: # In overcrouded or to lonely conditions life is no more
self.temp[i] = 0
# Cell is alive
if cell == 1:

# In overcrouded or to lonely = life is no more
if density<2 or density>3:
self.future_world[i] = 0
stable=False
else: # In good conditions life is going forward
self.temp[i] = 1
if cell == 0: # Cell is empty
if density==3: # In good conditions new life is born
self.temp[i] = 1

# In good conditions life is going forward
else:
self.future_world[i] = 1

# Cell is empty
if cell == 0:

# In good conditions new life is born
if density==3:
self.future_world[i] = 1
stable=False
else: # Still empty
self.temp[i]=0

# Still empty
else:
self.future_world[i]=0

i+=1

return not stable

def draw_world(self):
"""
Draw the world.
"""
print("\033[2J")
line = ""
for cell in range(len(self.world)):
Expand All @@ -116,10 +133,7 @@ def draw_world(self):
line=""
print("Period:",self.period)

def run(self, delay=DELAY):
"""
Simulate the Life.
"""
def simulate(self, delay):
self.random_seed()
self.draw_world()
print("Press Ctrl+C to quit.\n")
Expand All @@ -137,6 +151,9 @@ def run(self, delay=DELAY):
except KeyboardInterrupt:
break

def run(self, delay=DELAY):
self.simulate(delay)

if __name__ == '__main__':
life = Life()
life.run()
Expand Down
63 changes: 37 additions & 26 deletions neolife.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@
NEW_COLOR = (12,8,8)

class Neolife:
"""
A class to handle the Life functionalities.
"""
def __init__(self):
"""
Initialize the Life object.
"""
self.world = []
self.temp = []
self.future_world = []
self.world_size = WORLD_WIDTH*WORLD_HEIGHT
self.period = 0
self.pixels = neopixel.NeoPixel(machine.Pin(29), self.world_size)
Expand All @@ -43,7 +37,7 @@ def initialize_world(self):
"""
for _ in range(self.world_size):
self.world.append(0)
self.temp.append(0)
self.future_world.append(0)

def random_seed(self):
"""
Expand All @@ -54,14 +48,14 @@ def random_seed(self):

def update_world(self):
"""
Update the world with the temporary world.
Update the world with calculated next state of the world
"""
for i in range(self.world_size):
self.world[i] = self.temp[i]
self.world[i] = self.future_world[i]

def get_cell_value(self,i):
"""
Get the cell value.
Returns 0 if asking for a cell outside the world.
"""
if i<0 or i>=len(self.world):
return 0
Expand All @@ -78,32 +72,52 @@ def check_world(self):
for cell in self.world:
# Check eight closest cells
density=0

# left and right
# cell is not on the left edge
if i%self.world_size-1>0:
density += self.get_cell_value(i-1)
# cell is not on the right edge
if i%self.world_size-1<WORLD_WIDTH:
density += self.get_cell_value(i+1)

# top row
density += self.get_cell_value(i-offset+1)
density += self.get_cell_value(i-offset)
density += self.get_cell_value(i-offset-1)

# bottom row
density += self.get_cell_value(i+offset+1)
density += self.get_cell_value(i+offset)
density += self.get_cell_value(i+offset-1)

# The rules of life..
if cell == 1: # Cell is alive
if density<2 or density>3: # In overcrouded or to lonely conditions life is no more
self.temp[i] = 0
# Cell is alive
if cell == 1:

# In overcrouded or to lonely = life is no more
if density<2 or density>3:
self.future_world[i] = 0
stable=False
else: # In good conditions life is going forward
self.temp[i] = 1
if cell == 0: # Cell is empty
if density==3: # In good conditions new life is born
self.temp[i] = 1

# In good conditions life is going forward
else:
self.future_world[i] = 1

# Cell is empty
if cell == 0:

# In good conditions new life is born
if density==3:
self.future_world[i] = 1
stable=False
else: # Still empty
self.temp[i]=0

# Still empty
else:
self.future_world[i]=0

i+=1

return not stable

def draw_world(self, color=FORGROUND_COLOR):
Expand All @@ -120,9 +134,6 @@ def draw_world(self, color=FORGROUND_COLOR):
self.pixels.write()

def simulate(self, delay):
"""
Simulate the Game of Life.
"""
self.random_seed()
self.draw_world()
print("Press Ctrl+C to quit.\n")
Expand All @@ -145,8 +156,8 @@ def simulate(self, delay):
self.pixels.write()
break

def run(self, argument=DELAY):
self.simulate(argument)
def run(self, delay=DELAY):
self.simulate(delay)

if __name__ == '__main__':
neolife = Neolife()
Expand Down
Loading

0 comments on commit 4ff2f36

Please sign in to comment.