Browse Source

play a little sound

Brennen Bearnes 4 years ago
parent
commit
bb5ce7f40f
1 changed files with 53 additions and 22 deletions
  1. 53
    22
      raspipe.py

+ 53
- 22
raspipe.py View File

@@ -1,15 +1,19 @@
1 1
 #!/usr/bin/env python3
2 2
 # encoding: utf-8
3 3
 
4
-import sys
4
+import math
5
+import os
5 6
 import pygame
7
+import random
8
+import re
9
+import sys
6 10
 
7 11
 class RasPipe:
8 12
     size = width, height = 320, 240
9
-    font_size = 30
13
+    # size = width, height = 480, 320
14
+
15
+    font_size = 48
10 16
     delay = 70
11
-    black = (0, 0, 0)
12
-    white = (255,255,255)
13 17
     input_buffer_size = 100
14 18
     input_lines = []
15 19
     display_lines = 7
@@ -17,30 +21,57 @@ class RasPipe:
17 21
     def __init__(self, infile):
18 22
         self.infile = infile
19 23
         pygame.init()
24
+        exit
20 25
         self.screen = pygame.display.set_mode(self.size)
21 26
         self.font = pygame.font.Font(None, self.font_size) 
22 27
 
28
+        self.bgcolor = pygame.Color(0, 0, 0)
29
+        self.fgcolor = pygame.Color(255, 255, 255)
30
+
31
+        # A little bit of sound.
32
+        pygame.mixer.init()
33
+        self.click = pygame.mixer.Sound('./tick.wav')
34
+
23 35
     def run(self):
36
+        tick = 0
37
+
38
+        self.click.play()
39
+
24 40
         line = self.infile.readline()
25 41
         while line:
26 42
             for event in pygame.event.get():
27 43
               if event.type == pygame.QUIT:
28 44
                 sys.exit()
29 45
 
46
+            tick = tick + 1
47
+
30 48
             self.scale_display()
31 49
 
32
-            self.screen.fill(self.black)
50
+            self.screen.fill(self.bgcolor)
33 51
 
34
-            # Get last 7 lines of input...
52
+            # Get last display_lines of input...
35 53
             to_render = self.input_lines[-self.display_lines:]
36 54
 
37
-            # ...and scroll them up the display.
55
+            # ...and scroll them up the display:
38 56
             y = 0
39 57
             for render_line in to_render:
40 58
                 y += self.font_size
41
-                text_surface = self.font.render(render_line.rstrip(), True, self.white)
42
-                rect = text_surface.get_rect(center=(160, y))
59
+                text_surface = self.font.render(render_line.rstrip(), True, self.fgcolor)
60
+                # TODO: allow centering text
61
+                # rect = text_surface.get_rect(center=((self.width / 2), y))
62
+                rect = text_surface.get_rect(left=2, top=y)
43 63
                 self.screen.blit(text_surface, rect)
64
+            
65
+            # A progress bar of sorts
66
+            progress = (self.width / 100) * (len(self.input_lines) / 10)
67
+            self.screen.fill(self.fgcolor, [0, 0, progress, self.font_size])
68
+
69
+            if tick % self.display_lines == 0:
70
+                self.click.play()
71
+                # self.bgcolor.r = random.randrange(0, 255)
72
+                # self.bgcolor.g = random.randrange(0, 255)
73
+                # self.bgcolor.b = random.randrange(0, 255)
74
+                # self.bgcolor.a = random.randrange(0, 255)
44 75
 
45 76
             pygame.display.flip()
46 77
 
@@ -50,26 +81,26 @@ class RasPipe:
50 81
             line = self.infile.readline()
51 82
 
52 83
     def scale_display(self):
84
+       """Set the current font size and delay based on amount of input"""
53 85
        original_font_size = self.font_size
54 86
 
55
-       # How big should our font be?
56
-       if len(self.input_lines) > 20:
57
-           self.delay = 30
87
+       # How big should our font be, and how fast should text scroll?
88
+       if len(self.input_lines) > 150:
89
+           self.font_size = 18
90
+           self.delay = 5
91
+       elif len(self.input_lines) > 60:
58 92
            self.font_size = 20
59
-
60
-       if len(self.input_lines) > 50:
93
+           self.delay = 10
94
+       elif len(self.input_lines) > 30:
95
+           self.font_size = 24
61 96
            self.delay = 20
62
-           self.font_size = 15
63
-
64
-       if len(self.input_lines) > 100:
65
-           self.delay = 5
66
-           self.font_size = 10
67 97
 
68 98
        if self.font_size != original_font_size:
69 99
            self.font = pygame.font.Font(None, self.font_size) 
70
-
100
+   
71 101
        # How many lines of text to display?
72 102
        self.display_lines = int(self.size[1] / self.font_size) - 1
73 103
 
74
-rp = RasPipe(sys.stdin)
75
-rp.run()
104
+if __name__ == '__main__':
105
+    rp = RasPipe(sys.stdin)
106
+    rp.run()

Loading…
Cancel
Save