Source code for bruhanimate.bruheffect.twinkle_effect
"""Copyright 2023 Ethan ChristensenLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License."""importrandomfrombruhcolorimportbruhcoloredfrom..bruhutilimportTWINKLE_COLORS,Bufferfrom.base_effectimportBaseEffectfrom.settingsimportTwinkleSettings
[docs]classTWINKLE_SPEC:""" A single twinkle character that cycles through brightness levels. """
[docs]def__init__(self,char:chr,value:int):""" Initializes the twinkle character. Args: char (chr): Character to display. value (int): Initial brightness index (0–23). """self.char=charself.value=valueself.fade=bruhcolored(self.char,TWINKLE_COLORS[self.value])self.mode=random.choice([1,-1])
[docs]defnext(self):""" Advances the brightness by one step and returns self. """ifself.value>=23:self.mode=-1elifself.value<=0:self.mode=1self.value+=self.modeself.fade=bruhcolored(self.char,TWINKLE_COLORS[self.value])returnself
[docs]defcopy(self):""" Returns a copy of this TWINKLE_SPEC. Returns: TWINKLE_SPEC: Copy with the same char, value, and mode. """new=TWINKLE_SPEC(self.char,self.value)new.mode=self.modereturnnew
[docs]classTwinkleEffect(BaseEffect):""" A twinkling star-like effect where characters pulse in brightness. """
[docs]def__init__(self,buffer:Buffer,background:str,settings:TwinkleSettings=None):""" Initializes the TwinkleEffect. Args: buffer (Buffer): Effect buffer to push updates to. background (str): Character or string to use as the background. settings (TwinkleSettings, optional): Configuration for the effect. Defaults to None. """super(TwinkleEffect,self).__init__(buffer,background)s=settingsorTwinkleSettings()self.density=s.densityself.twinkle_chars=s.twinkle_charsself.specs=[]self._set_specs()
[docs]defset_density(self,density:float):""" Sets the density of twinkling characters. Args: density (float): Fraction of cells that twinkle, between 0 and 1. """ifisinstance(density,float)and0<=density<=1:self.density=densityself._set_specs()
[docs]defset_twinkle_chars(self,twinkle_chars:list[str]):""" Sets the characters used for the twinkle effect. Args: twinkle_chars (list[str]): List of characters to randomly assign to twinkle positions. """ifisinstance(twinkle_chars,list):self.twinkle_chars=twinkle_charsself._set_specs()
[docs]defrender_frame(self,frame_number:int):""" Renders the next frame of the twinkle effect. Args: frame_number (int): The current frame number. """forx,yinself.specs:spec=self.buffer.get_char(x,y)self.buffer.put_char(x,y,spec.next().copy())delspec