SHARE
TWEET

a simple countdown / countup for Python/Pygame

diliupg Apr 17th, 2017 (edited) 760 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. from pygame.locals import KEYDOWN, K_ESCAPE, RLEACCEL
  3.  
  4. class Make_Countdown(pygame.sprite.DirtySprite):
  5.     """
  6.    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.
  7.    Font size need to be supplied along with the colour of text, lownum and hinum.
  8.    Countdown = True by default. hinum -> lownum
  9.    Countdown = False lownum -> hinum
  10.    if a font is not supplied (path), the default pygame font will be used.
  11.    Fading out is set by default.
  12.    Time will hold each number on screen for that duration If fade is set that time will be used to fade out."""
  13.  
  14.     def __init__(self, posx = None, posy = None, colour = (255, 50, 64), number = 5, font = None, fontsize = 200, fade_speed = 20, countdown = True, fadeout = True):
  15.  
  16.         pygame.sprite.DirtySprite.__init__(self)
  17.  
  18.         self.screen = pygame.display.get_surface()
  19.  
  20.         if self.screen == None:
  21.             print"none"
  22.             self.screen =pygame.display.set_mode((300, 300), 1, 32)
  23.  
  24.         self.subsurface = self.screen.subsurface(0, 0, self.screen.get_width(), self.screen.get_height()).convert_alpha()
  25.         self.back = self.screen
  26.         self.background = (0, 0, 0)
  27.         self.alpha = 255
  28.         if posx == None:
  29.             self.posx = self.screen.get_width() / 2
  30.         else:
  31.             self.posx = posx
  32.         if posy == None:
  33.             self.posy = self.screen.get_height() / 2
  34.         else:
  35.             self.posy = posy
  36.         self.fontsize = fontsize
  37.         self.colour = colour
  38.         self.lownum = 1
  39.         self.hinum = number
  40.         self.font = font
  41.         self.count_down = countdown
  42.         self.fade = fadeout
  43.         self.fade_speed = fade_speed
  44.         self.imagelist = []
  45.  
  46.         self.renderedfont = pygame.font.Font(self.font, self.fontsize)
  47.  
  48.         for x in xrange(self.lownum, self.hinum + 1):  # we want to count hinum as well so + 1
  49.  
  50.             matext = self.renderedfont.render(str(x), True, self.colour, (0, 0, 0)).convert()
  51.  
  52.             matext.set_colorkey(0, RLEACCEL)
  53.  
  54.             matext.set_alpha(255)
  55.  
  56.             self.imagelist.append(matext)
  57.  
  58.  
  59.  
  60.     def run(self, variable):
  61.  
  62.         direction = variable
  63.  
  64.         if direction == "down":
  65.  
  66.             self.imagelist = reversed(self.imagelist)
  67.  
  68.         for image in self.imagelist:
  69.  
  70.             self.alpha = 255
  71.             # the calculation below makes the sprite be printed by its center coord
  72.             posx = self.posx - (image.get_width() / 2)
  73.             posy = self.posy - (image.get_height() / 2)
  74.             self.subsurface = self.screen.subsurface(posx, posy, image.get_width(), image.get_height()).convert_alpha()
  75.             for x in xrange(50):
  76.  
  77.                 self.alpha -= 5
  78.                 image.set_alpha(self.alpha)
  79.  
  80.                 self.screen.blit(image, (posx, posy))
  81.  
  82.                 pygame.display.update()
  83.                 pygame.time.wait(self.fade_speed)  # how long to pause
  84.                 self.screen.blit(self.subsurface, (posx, posy))
  85.  
  86.             for e in pygame.event.get():
  87.                 if e.type == KEYDOWN and e.key == K_ESCAPE:
  88.                     raise SystemExit
  89.  
  90.  
  91.  
  92. if __name__ == '__main__':
  93.     pygame.init()
  94.     counter = Make_Countdown()
  95.     #counter.run("up")
  96.     counter.run("down")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top