Advertisement
Windspar

Pygame Display CountDown

Jan 12th, 2020
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import os
  2. import pygame
  3. from pygame.sprite import Sprite, Group
  4.  
  5. class Text(Sprite):
  6.     def __init__(self, text, font, color, position, anchor="topleft"):
  7.         Sprite.__init__(self)
  8.         self._text = text
  9.         self._font = font
  10.         self._color = color
  11.         self._anchor = anchor
  12.         self._position = position
  13.         self.render()
  14.  
  15.     def render(self):
  16.         self.image = self._font.render(self._text, 1, self._color)
  17.         self.rect = self.image.get_rect(**{self._anchor: self._position})
  18.  
  19.     def set_text(self, text):
  20.         self._text = text
  21.         self.render()
  22.  
  23. class Timer:
  24.     def __init__(self, start, interval, callback):
  25.         self.tick = start
  26.         self.interval = interval
  27.         self.callback = callback
  28.  
  29.     def update(self, ticks):
  30.         while ticks > self.tick:
  31.             self.tick += self.interval
  32.             self.callback(self)
  33.  
  34. class CountDownTimer:
  35.     def __init__(self, count, callback, interval=1000):
  36.         self.count = count
  37.         self.callback = callback
  38.         self.timer = Timer(pygame.time.get_ticks(), interval, self.countdown)
  39.  
  40.     def countdown(self, timer):
  41.         self.count -= 1
  42.         self.callback(self)
  43.  
  44.     def update(self, ticks):
  45.         if self.count >= 0:
  46.             self.timer.update(ticks)
  47.  
  48. class DisplayCountDown:
  49.     def __init__(self, count, font, color, position, anchor="topleft", interval=1000):
  50.         self.countdown = CountDownTimer(count, self.update_text, interval)
  51.         self.display = "{:02d}"
  52.         self.text = Text(self.display.format(self.countdown.count), font, color, position, anchor)
  53.  
  54.     def update_text(self, countdown):
  55.         if countdown.count >= 0:
  56.             self.text.set_text(self.display.format(countdown.count))
  57.         else:
  58.             self.text.kill()
  59.  
  60.     def update(self, ticks):
  61.         self.countdown.update(ticks)
  62.  
  63. def main():
  64.     pygame.init()
  65.     # center pygame screen
  66.     os.environ['SDL_VIDEO_CENTERED'] = '1'
  67.     # Basic pygame setup
  68.     pygame.display.set_caption("CountDown Timer Example")
  69.     surface = pygame.display.set_mode((450, 600))
  70.     clock = pygame.time.Clock()
  71.     rect = surface.get_rect()
  72.     delta = 0
  73.     fps = 60
  74.  
  75.     # Setup variables
  76.     timer_font = pygame.font.Font(None, 38)
  77.     position = rect.centerx, 20
  78.  
  79.     # Game Variables
  80.     timer = DisplayCountDown(10, timer_font, pygame.Color("white"), position, "midtop")
  81.     timer_group = Group(timer.text)
  82.  
  83.     # Main loop
  84.     running = True
  85.     while running:
  86.         # Event loop
  87.         for event in pygame.event.get():
  88.             if event.type == pygame.QUIT:
  89.                 running = False
  90.  
  91.         # Update
  92.         ticks = pygame.time.get_ticks()
  93.         timer.update(ticks)
  94.  
  95.         # Draw
  96.         surface.fill(pygame.Color("black"))
  97.         timer_group.draw(surface)
  98.  
  99.         # Render to screen
  100.         pygame.display.flip()
  101.  
  102.         # Sleep, Idle, and Delta
  103.         delta = clock.tick(fps)
  104.  
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement