Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # To the extent possible under law, the Kyle Start has waived all
  3. # copyright and related or neighboring rights for this script.  This work is
  4. # published from: United States.
  5. # https://creativecommons.org/publicdomain/zero/1.0/
  6. import random
  7. import math
  8.  
  9. import tcod
  10. import tcod.event
  11. import tcod.tileset
  12.  
  13. import numpy as np
  14.  
  15. WIDTH, HEIGHT = 96, 54
  16.  
  17. BLOCKS = {
  18.     # 1: upper-left, 2: upper-right, 4: lower-left, 8: lower-right
  19.     0x2588: 1 | 2 | 4 | 8,
  20.     0x2596: 4,
  21.     0x2597: 8,
  22.     0x2598: 1,
  23.     0x2599: 1 | 4 | 8,
  24.     0x259A: 1 | 8,
  25.     0x259B: 1 | 2 | 4,
  26.     0x259C: 1 | 2 | 8,
  27.     0x259D: 2,
  28.     0x259E: 2 | 4,
  29.     0x259F: 2 | 4 | 8,
  30. }
  31.  
  32.  
  33. def generate_quadrants():
  34.     """Generate quadrant block elements, overwriting the current font.
  35.  
  36.    Call this after your font is loaded.
  37.    """
  38.     ts = tcod.tileset.get_default()
  39.     half_w = ts.tile_width // 2
  40.     half_h = ts.tile_height // 2
  41.     for codepoint, quads in BLOCKS.items():
  42.         tile = np.zeros(ts.tile_shape, np.uint8)
  43.         tile[:half_h, :half_w] = 255 if quads & 1 else 0
  44.         tile[:half_h, half_w:] = 255 if quads & 2 else 0
  45.         tile[half_h:, :half_w] = 255 if quads & 4 else 0
  46.         tile[half_h:, half_w:] = 255 if quads & 8 else 0
  47.         ts.set_tile(codepoint, tile)
  48.  
  49.  
  50. class Dot:
  51.     def __init__(self):
  52.         self.reset()
  53.         for _ in range(random.randint(0, 150)):
  54.             self.step()
  55.  
  56.     def reset(self):
  57.         self.pos = (54, 96)
  58.         direction = random.uniform(0, math.tau)
  59.         speed = random.uniform(.0001, 0.001)
  60.         self.speed = math.sin(direction) * speed, math.cos(direction) * speed
  61.         post = random.uniform(0, 50)
  62.         self.pos = self.pos[0] + math.sin(direction) * post, self.pos[1] + math.cos(direction) * post
  63.  
  64.     def step(self):
  65.         self.pos = self.pos[0] + self.speed[0], self.pos[1] + self.speed[1]
  66.         self.speed = self.speed[0] * 1.5, self.speed[1] * 1.5
  67.         if not (0 <= self.pos[0] < HEIGHT * 2 and 0 <= self.pos[1] < WIDTH * 2):
  68.             self.reset()
  69.  
  70.     def draw(self, pad):
  71.         line = np.array(tcod.line_where(
  72.             round(self.pos[0]), round(self.pos[1]), round(self.pos[0] + self.speed[0]), round(self.pos[1] + self.speed[1]))).T[:-1].T
  73.         line = line.T[line[0] < HEIGHT * 2].T
  74.         line = line.T[line[1] < WIDTH * 2].T
  75.         line = line.T[line[0] >= 0].T
  76.         line = line.T[line[1] >= 0].T
  77.         if not line.size:
  78.             return
  79.         vis = 255
  80.         pad[tuple(line)] |= np.linspace(vis // 2, vis, line.shape[1], dtype=np.uint8)[:, np.newaxis]
  81.  
  82.  
  83. def main():
  84.     """Example program for tcod.event"""
  85.     TITLE = None
  86.  
  87.     tcod.console_set_custom_font(
  88.         "potash_10x10.png", tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_ASCII_INROW
  89.     )
  90.  
  91.     generate_quadrants()
  92.  
  93.     with tcod.console_init_root(
  94.         WIDTH,
  95.         HEIGHT,
  96.         TITLE,
  97.         order="F",
  98.         renderer=tcod.RENDERER_SDL2,
  99.         vsync=True,
  100.     ) as console:
  101.         tcod.sys_set_fps(30)
  102.         pad = np.zeros((HEIGHT * 2, WIDTH * 2, 3), np.uint8)
  103.         dots = [Dot() for _ in range(100)]
  104.         while True:
  105.             pad[...] //= 2
  106.             for dot in dots:
  107.                 dot.draw(pad)
  108.                 dot.step()
  109.  
  110.             console.draw_semigraphics(pad)
  111.             tcod.console_flush()
  112.             for event in tcod.event.get():
  113.                 if event.type == "QUIT" or (event.type == "KEYDOWN" and event.sym == tcod.event.K_ESCAPE):
  114.                     raise SystemExit()
  115.  
  116.  
  117. if __name__ == "__main__":
  118.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement