Created
January 26, 2020 16:23
-
-
Save kadir014/d26e5bbdce98a75197d3b968ef28ba82 to your computer and use it in GitHub Desktop.
Fade effect for pygame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
display = None | |
fade_surface = None | |
tick = 0 | |
speed = 10 | |
anim_state = None | |
anim_func = None | |
def init(): | |
global display, fade_surface | |
display = pygame.display.get_surface() | |
fade_surface = pygame.Surface(display.get_size()).convert() | |
fade_surface.fill((0, 0, 0)) | |
def update(): | |
global tick, speed, anim_state, anim_func, display, fade_surface, fade_out | |
if anim_state == "fade_in" and tick > 0: | |
fade_surface.set_alpha(256 - tick) | |
display.blit(fade_surface, (0, 0)) | |
elif anim_state == "fade_out" and tick > 0: | |
fade_surface.set_alpha(tick) | |
display.blit(fade_surface, (0, 0)) | |
elif anim_state == "fade_in2": | |
fade_surface.set_alpha(256 - tick) | |
display.blit(fade_surface, (0, 0)) | |
tick -= speed | |
if tick < 0: | |
tick = 0 | |
if anim_state: | |
if anim_state == "fade_in2": | |
anim_func() | |
fade_out(None) | |
elif anim_state == "fade_in": | |
anim_func() | |
anim_state = None | |
anim_func = None | |
elif anim_state == "fade_out" and anim_func: | |
anim_func() | |
anim_state = None | |
anim_func = None | |
else: | |
anim_state = None | |
anim_func = None | |
def fade_in(func): | |
global anim_state, anim_func, tick | |
tick = 256 | |
anim_state = "fade_in" | |
anim_func = func | |
def fade_out(func): | |
global anim_state, anim_func, tick | |
tick = 256 | |
anim_state = "fade_out" | |
anim_func = func | |
def fade(func): | |
global anim_state, anim_func, tick | |
tick = 256 | |
anim_state = "fade_in2" | |
anim_func = func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment