daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import sin, cos
  2.  
  3. import pygame
  4. pygame.init()
  5.  
  6. WHITE = (250, 250, 250)
  7. BLACK = (5, 5, 5)
  8.  
  9. HUE_MAX = 360
  10.  
  11. class NormalText:
  12.     def __init__(self, text: str, pos=(0, 0),
  13.                  font=pygame.font.Font(pygame.font.get_default_font(), 40)):
  14.         self.text = text
  15.         self.font = font
  16.         self.surface = self.font.render(text, True, WHITE)
  17.         self.pos = pos
  18.  
  19.     def update(self):
  20.         pass
  21.  
  22.     def draw(self, screen):
  23.         screen.blit(self.surface, self.pos)
  24.  
  25.  
  26. class WaveText:
  27.  
  28.     def __init__(self, text: str, pos=(0, 0), frequency=5, speed=-0.3, radius=4, spread_x=1.5,
  29.                  font=pygame.font.Font(pygame.font.get_default_font(), 40), huestep=3):
  30.         """
  31.        A basic wavy text widget.
  32.        :param text: The string to render
  33.        :param pos: Top left corner of the text. Note that because of the circles it will move outside of it
  34.        :param frequency: How often a new wave is stared
  35.        :param speed: the speed of each wave (~difference of position with the next letter)
  36.        :param radius: radius on which letters move
  37.        :param spread_x: to have an ellipse stretched in the x axis set this to > 1 and < 1 for the y axis.
  38.        :param font:
  39.        """
  40.         self.speed = speed
  41.         self.frequency = frequency
  42.         self.radius = radius
  43.         self.pos = pos
  44.         self.spread_x = spread_x
  45.  
  46.         self.t = 0
  47.  
  48.         self.text = text
  49.         self.font = font
  50.         self.char_to_surf = {}
  51.  
  52.  
  53.         self.hue = 0
  54.         self.huestep = huestep
  55.  
  56.         col = pygame.Color(*WHITE)
  57.         self.char_surfs = []
  58.         for hue in range(0, HUE_MAX, huestep):
  59.             col.hsva = (hue, 100, 100, 100)
  60.             self.char_surfs.extend([self.get_surf(c, col) for c in self.text])
  61.  
  62.  
  63.     def get_surf(self, c, col):
  64.         key = c + str(col)
  65.         surf = self.char_to_surf.get(key)
  66.         if not surf:
  67.             surf = self.font.render(c, True, col)
  68.             self.char_to_surf[key] = surf
  69.         return surf
  70.  
  71.     def update(self):
  72.         self.t += 1
  73.  
  74.         self.hue = (self.hue + 1) % (HUE_MAX // self.huestep)
  75.  
  76.     def draw(self, screen):
  77.         x0, y0 = self.pos
  78.  
  79.         xr = self.radius / self.spread_x
  80.         yr = self.radius
  81.         offset = self.t / self.frequency
  82.         speed = self.speed
  83.  
  84.         screen_blit = screen.blit
  85.  
  86.         numchars = len(self.text)
  87.         charoffset = self.hue * numchars
  88.  
  89.         for char in self.char_surfs[charoffset:charoffset+numchars]:
  90.             dx = xr * cos(offset)
  91.             dy = yr * sin(offset)
  92.             screen_blit(char, (x0 + dx, y0 + dy))
  93.             x0 += char.get_width()
  94.             offset += speed
  95.  
  96.  
  97.  
  98. def main():
  99.     SCREEN_SIZE = 1920, 1080
  100.     FPS = 60
  101.  
  102.     screen = pygame.display.set_mode(SCREEN_SIZE)
  103.     clock = pygame.time.Clock()
  104.  
  105.     opt_sel = 0
  106.     options = ("Of course not!", "Guess again, nerd!")
  107.     options_objects = [
  108.         (NormalText(t, (800, 500 + 50*i)), WaveText(t, (800, 500 + 50*i)))
  109.         for i, t in enumerate(options)]
  110.  
  111.     while "I want to read the same text":
  112.         for e in pygame.event.get():
  113.             if e.type == pygame.QUIT:
  114.                 return 0
  115.             if e.type == pygame.KEYDOWN:
  116.                 if e.key == pygame.K_q:
  117.                     return 0
  118.                 elif e.key == pygame.K_UP:
  119.                     if opt_sel > 0:
  120.                         opt_sel -= 1
  121.                 elif e.key == pygame.K_DOWN:
  122.                     if opt_sel + 1 < len(options):
  123.                         opt_sel += 1
  124.  
  125.         screen.fill(BLACK)
  126.  
  127.         for i, option in enumerate(options_objects):
  128.             option[1].update()
  129.             option[1 if opt_sel == i else 0].draw(screen)
  130.  
  131.         pygame.display.update()
  132.  
  133.         clock.tick(FPS)
  134.  
  135.  
  136. if __name__ == '__main__':
  137.     quit(main())
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
 
Top