A Class to make a count down or count up counter for Pygame

import pygame
from pygame.locals import KEYDOWN, K_ESCAPE, RLEACCEL

class Make_Countdown(pygame.sprite.Sprite):
    """
    xpos and ypos are the coords where the text will be centeres and printed. By defaault they are screen_centerx and screen_centery. If you leave posx = None and posy = None these values will be used. If you enter any values, those will be used.
    Font size need to be supplied along with the colour of text, lownum and hinum.
    Countdown = True by default. hinum -> lownum
    Countdown = False lownum -> hinum
    if a font is not supplied (path), the default pygame font will be used.
    Fading out is set by default.
    Time will hold each number on screen for that duration If fade is set that time will be used to fade out."""
    def __init__(self, posx = None, posy = None, colour = (255, 50, 64), lownum = 1, hinum = 5, font = None, fontsize = 120, fade_speed = 25, countdown = True, fadeout = True):

        pygame.sprite.Sprite.__init__(self)

        self.screen = pygame.display.get_surface()
        if self.screen == None:
            self.screen =pygame.display.set_mode((200, 200), 1, 32)
        self.subsurface = self.screen.subsurface(0, 0, self.screen.get_width(), self.screen.get_height()).convert_alpha()
        self.background = (0, 0, 0)
        self.alpha = 255
        if posx == None:
            self.posx = self.screen.get_width() / 2
        else:
            self.posx = posx
        if posy == None:
            self.posy = self.screen.get_height() / 2
        else:
            self.posy = posy
        self.fontsize = fontsize
        self.colour = colour
        self.lownum = lownum
        self.hinum = hinum
        self.font = font
        self.count_down = countdown
        self.fade = fadeout
        self.fade_speed = fade_speed
        self.imagelist = []
        self.print_font = pygame.font.Font(font, fontsize)
        self.renderedfont = pygame.font.Font(self.font, self.fontsize)

        for x in xrange(lownum, hinum + 1):  # we want to count hinum as well so + 1

            matext = self.renderedfont.render(str(x), True, self.colour, (0, 0, 0)).convert()

            matext.set_colorkey(0, RLEACCEL)

            matext.set_alpha(255)

            self.imagelist.append(matext)

    def down(self):
        self.subsurface = self.screen.subsurface(0, 0, self.screen.get_width(), self.screen.get_height()).convert_alpha()

        for image in reversed(self.imagelist):

            self.alpha = 255
            # the calculation below makes the sprite be printed by its center coord
            posx = self.posx - (image.get_width() / 2)
            posy = self.posy - (image.get_height() / 2)
            for x in xrange(50):

                self.alpha -= 5
                image.set_alpha(self.alpha)
                self.screen.blit(image, (posx, posy))

                pygame.display.flip()
                pygame.time.wait(self.fade_speed)
                self.screen.blit((self.subsurface), (0, 0))

            for e in pygame.event.get():
                if e.type == KEYDOWN and e.key == K_ESCAPE:
                    raise SystemExit

        return False

    def up(self):
        self.subsurface = self.screen.subsurface(0, 0, self.screen.get_width(), self.screen.get_height()).convert_alpha()

        for image in (self.imagelist):

            self.alpha = 255
            posx = self.posx - (image.get_width() / 2)
            posy = self.posy - (image.get_height() / 2)
            for x in xrange(50):

                self.alpha -= 5
                image.set_alpha(self.alpha)
                self.screen.blit(image, (posx, posy))

                pygame.display.flip()
                pygame.time.wait(self.fade_speed)
                self.screen.blit((self.subsurface), (0, 0))


            for e in pygame.event.get():
                if e.type == KEYDOWN and e.key == K_ESCAPE:
                    raise SystemExit

        return False


if __name__ == '__main__':
    pygame.init()
    counter = Make_Countdown()
    counter.down()
    counter.up()
Pygame Count down ( or Count up) maker

Leave a Reply