Source code for bruhanimate.bruheffect.noise_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..bruhutilimportBufferfrom.base_effectimportBaseEffectfrom.settingsimportNoiseSettings
[docs]classNoiseEffect(BaseEffect):""" A noise effect that adds random pixels to the screen with a specified intensity. """
[docs]def__init__(self,buffer:Buffer,background:str,settings:NoiseSettings=None):""" Initializes the NoiseEffect class. Args: buffer (Buffer): Effect buffer to push updates to. background (str): Character or string for background. settings (NoiseSettings, optional): Configuration for the noise effect. Defaults to None. """super(NoiseEffect,self).__init__(buffer,background)s=settingsorNoiseSettings()self.intensity=s.intensity/1000if1<=s.intensity<=999else200/1000self.color=s.colorself.characters=Trueself.noise=" !@#$%^&*()_+1234567890-=~`qazwsxedcrfvtgbyhnujmik,ol.p;/[']\\QAZXSWEDCVFRTGBNHYUJM<KIOL>?:P{\"}|"self.noise_length=len(self.noise)
[docs]defset_intensity(self,intensity:int):""" Updates the intensity of the effect. Args: intensity (int): New intensity value between 1 and 999. """self.intensity=(intensity/1000ifintensityand1<=intensity<=999else200/1000)
[docs]defset_color(self,color:bool,characters:bool=True):""" Updates the color and character rendering mode. Args: color (bool): Whether to render with color. characters (bool, optional): Whether to render noise characters or background blocks. Defaults to True. """self.color=colorself.characters=characters
[docs]defrender_frame(self,frame_number:int):""" Renders a frame of the noise effect. Args: frame_number (int): The current frame number. """ifself.color:foryinrange(self.buffer.height()):forxinrange(self.buffer.width()):ifrandom.random()<self.intensity:ifself.characters:self.buffer.put_char(x,y,bruhcolored(self.noise[random.randint(0,self.noise_length-1)],on_color=random.randint(0,255),).colored,)else:self.buffer.put_char(x,y,bruhcolored(" ",on_color=random.randint(0,255)).colored,)else:foryinrange(self.buffer.height()):forxinrange(self.buffer.width()):ifrandom.random()<self.intensity:self.buffer.put_char(x,y,self.noise[random.randint(0,self.noise_length-1)])