Template Function for Static Animations

CoolieCoolster

  • Full Member
  • ***
    • Posts: 30
    • View Profile
As I was adjusting the function that animates the "Select Speed" text in NanoMem, I thought that creating a template for it might be helpful should others want to replicate the functionality. While I currently only use it for upwards, vertical animation, the template below has aspect ratio and directional input variables that can be set to choose which of the four animation types is being used. Perhaps at some point a page could be created to compile templates for various functions as to make the game creation process simpler for those doing so for the first time.

While I tried to name the input variables in a way that makes sense, I'll give a brief description of each below:

- line_count: The number of lines, vertical or horizontal, in the frame of animation.
- line_length: The number of pixels long that each line is.
- total_line_count: The total number of lines that the animation cycles through.
- x & y: Coordinate that the highest or leftmost line of the animation would be "blitted" at.
- aspect_ratio: If the animation is vertical, this should be set to 0, while if it's horizontal, this should be set to 1.
- direction: If the animation goes rightwards or downwards, this should be set to 0, while if it does leftwards or upwards, this should be set to 1.
- speed: The time in milliseconds between each frame of animation.

Code: [Select]
def animate(line_count,line_length,total_line_count,x,y,aspect_ratio,direction,speed):
    current_line=0
    line_limit=total_line_count-1
    while aspect_ratio==0:
        if direction==0:
            frame_edge=y+line_count
            line_switch=lambda a,b: a-b
        elif direction==1:
            frame_edge==y
            line_switch=lambda a,b: a+b
        while(1):
            if current_line>line_limit:
                current_line-=current_line
            for i in range(line_count):
                if current_line+i>line_limit:
                    prevent_overflow=line_limit
                else:
                    prevent_overflow=0
                thumby.display.blit(line_set[current_line+i-prevent_overflow],x,line_switch(frame_edge,i),line_length,1)
            thumby.display.update()
            wait(speed)
            current_line+=1
       
    while aspect_ratio==1:
        if direction==0:
            frame_edge=x+line_count
            line_switch=lambda a,b: a-b
        elif direction==1:
            frame_edge==x
            line_switch=lambda a,b: a+b
        while(1):
            if current_line>line_limit:
                current_line-=current_line
            for i in range(line_count):
                if current_line+i>line_limit:
                    prevent_overflow=line_limit
                else:
                    prevent_overflow=0
                thumby.display.blit(line_set[current_line+i-prevent_overflow],line_switch(frame_edge,i),y,1,line_length)
            thumby.display.update()
            wait(speed)
            current_line+=1

The main purpose for animating with lines rather than whole frames is not only because the lines can be reused each time the frame is redrawn, but also because the lines themselves are often the same, especially when the the line length is short. For instance, while my "Select Speed" animation in NanoMem cycles through 90 lines, as each line is only three pixels long, the data for the lines can be referenced from an array of only seven 1x3 pixel bitmaps, avoiding the excessive use of storage space. If anyone interested in using the template has any questions, I'll be glad to answer them below.
« Last Edit: October 23, 2021, 12:57:31 PM by CoolieCoolster »


 

SMF spam blocked by CleanTalk