Skip to content

Commit

Permalink
Set recursion limit, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlasdairWallaceMackie committed Nov 15, 2023
1 parent 40af381 commit b0855ab
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### Written in Python using the [Pyxel](https://github.com/kitao/pyxel) library

### [Click here to view the live animation in your browser](https://kitao.github.io/pyxel/wasm/launcher/?play=AlasdairWallaceMackie.bubble-screensaver.screensaver)

<br>

<img src="./example.gif" width=500>

## To Run
Expand Down
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os, sys
import pyxel
from constants import WINDOW_WIDTH, WINDOW_HEIGHT
from constants import WINDOW_WIDTH, WINDOW_HEIGHT, RECURSION_LIMIT
from screensaver import Screensaver

class App:
Expand All @@ -23,4 +23,5 @@ def draw(self):
pyxel.cls(pyxel.COLOR_BLACK)
self.screensaver.draw()

sys.setrecursionlimit(RECURSION_LIMIT)
App()
13 changes: 6 additions & 7 deletions bubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def init_color(self):
return color

def increment_radius(self, amount: int):
""" Use negative integers to shrink radius """
""" Can use a negative value for the `amount` parameter to shrink radius """
if self.radius + amount in range(BUBBLE_RADIUS_MIN, BUBBLE_RADIUS_MAX + 1):
self.radius += amount

Expand Down Expand Up @@ -101,13 +101,12 @@ def draw_dithering(self):
return

try:
# We are offsetting the start point of the dithering algorithm. If it's initiated in the bubble's center, there will be an issue if the center is inside a neighbor bubble.
# We are offsetting the start point of the dithering algorithm. If it's initiated in the bubble's center, the algorithm could be initialized inside an overlapping neighbor bubble instead
self.recursive_dither(
self.x + ((self.radius / 2) + 1),
self.y - ((self.radius / 2) + 1),
)
except:
# There is still a rare chance of a RecursionError, so we catch it here
except RecursionError: # There is still a rare chance of a RecursionError, so we catch it here
return

def recursive_dither(self, x, y):
Expand All @@ -119,7 +118,7 @@ def recursive_dither(self, x, y):
if location_color == self.current_color and not self.found_next_color_in_neighbor(x, y):
pyxel.pset(x, y, self.next_color)

self.color_neighbors(x, y)
self.set_neighbors_colors(x, y)

def found_next_color_in_neighbor(self, x, y) -> bool:
for new_x, new_y in self.neighbor_coordinates(x, y):
Expand All @@ -128,12 +127,12 @@ def found_next_color_in_neighbor(self, x, y) -> bool:

return False

def color_neighbors(self, x, y):
def set_neighbors_colors(self, x, y):
for new_x, new_y in self.neighbor_coordinates(x, y):
if pyxel.pget(new_x, new_y) != self.next_color:
self.recursive_dither(new_x, new_y)

def neighbor_coordinates(self, x, y) -> [int, int]:
def neighbor_coordinates(self, x, y) -> [(int, int)]:
return [
(x + 1, y),
(x - 1, y),
Expand Down
2 changes: 2 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pyxel
from itertools import cycle

RECURSION_LIMIT = 512

WINDOW_HEIGHT = 128
WINDOW_WIDTH = WINDOW_HEIGHT * 2

Expand Down

0 comments on commit b0855ab

Please sign in to comment.