Skip to content

Commit

Permalink
Initial implementation of background gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
DiddiLeija committed Jun 17, 2024
1 parent f13d72e commit fd64cf6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import pyxel

from .tools import gradient, draw_gradient

# === Tool functions (physics, data, etc) ===

__all__ = (
Expand Down Expand Up @@ -519,7 +521,11 @@ class BaseLevel(ABC):
button_location = "" # a string representing the coordinates of the "ending button" location
ending_button = None # the ending button object
finished_next = "" # next sequence in case a level ends succesfully
slimehorn_variant = False #
slimehorn_variant = False # Are we using slimehorn variants?
use_gradient = False # Determine if a gradient color will be used on background
gradient_color = 0 # Gradient color (if enabled))
gradient_skips = list() # advanced option to pass a 'skips' arg to 'tools.gradient()'
gradient_height = 10 # advanced option to pass a 'height' arg to 'tools.gradient()'

def __init__(self, player_choice):
pyxel.camera(0, self.draw_v)
Expand Down Expand Up @@ -666,6 +672,13 @@ def update_template(self):
def draw_template(self):
"Some drawing actions that should happen in (almost) every instance."
pyxel.cls(self.bgcolor)
if self.use_gradient:
draw_gradient(
gradient(self.gradient_height, self.gradient_skips),
scroll_x,
self.draw_v,
self.gradient_color
)
if self.check_anyone_alive():
pyxel.camera()
for i in self.clouds:
Expand Down
12 changes: 12 additions & 0 deletions src/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class One(BaseLevel):
# NOTE: The same workaround is present in all the levels stored here...
# TODO: Safely remove this workaround at some point?
nextlevel = "two"
use_gradient = True
gradient_height = 16
gradient_color = 6


class Two(BaseLevel):
Expand Down Expand Up @@ -163,6 +166,9 @@ class Two(BaseLevel):
ending_button = Button(1192, 192)
finished_next = "three"
nextlevel = "three"
use_gradient = True
gradient_color = 1
gradient_height = 129


class Three(BaseLevel):
Expand Down Expand Up @@ -255,6 +261,9 @@ class Three(BaseLevel):
finished_next = "four"
nextlevel = "four"
slimehorn_variant = True
use_gradient = True
gradient_color = 9
gradient_height = 60


class Four(BaseLevel):
Expand Down Expand Up @@ -296,3 +305,6 @@ class Four(BaseLevel):
ending_button = Button(1512, 472)
finished_next = "five"
nextlevel = "five"
use_gradient = True
gradient_color = 6
gradient_height = 129
27 changes: 26 additions & 1 deletion src/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

POSSIBLE_LEVELS = (
# a list of allowed level names.
# NOTE: apparently "menu" or "death" should not be allowed as a saved level.
# NOTE: "menu" or "death" should not be allowed as a saved level.
"intro",
"one",
"two",
Expand All @@ -30,6 +30,7 @@ def draw_text(text, x, y):

def init_class(obj, popt):
"Initialize a class and return the object."
# TODO: Find a better way to do this?
return obj(popt)

def get_savedata():
Expand All @@ -55,3 +56,27 @@ def report_crash(opname, original):
f"Error: Internal operation '{opname}' showed unexpected behavior. "
f"If you are not testing this operation, please report this error. ('{original}')"
)

def gradient(height, skips):
"Generate a list-of-lists needed to draw a gradient on the background."
final = dict()
for i in range(height):
if i in skips:
# this row should be skipped
continue
if i % 2 == 0:
# variant 1
final[128-i] = [0 + (2*op) for op in range(0, 65)]
else:
# variant 2
final[128-i] = [1 + (2*op) for op in range(0, 65)]
return final

def draw_gradient(grad, ini_x, ini_y, col):
# draw a given gradient data.
for k, v in grad.items():
for vv in v:
try:
pyxel.pset((ini_x-1) + vv, ini_y + k, col)
except Exception:
pass # this shouldn't happen anyway

0 comments on commit fd64cf6

Please sign in to comment.