Ian George avatar Ian George committed 3bc3434

Renamed drawing app and made brownian.py

Comments (0)

Files changed (4)

drawer/canvas.py

- #! /usr/bin/env python
-#display
-
-import pygame
-from pygame.locals import *
-
-pygame.init()
-
-SCREEN_UPDATE_ADD = 1
-SCREEN_UPDATE_BLANK = 2
-
-class AnimatedCanvas(object):
-    def __init__(self, win_width, win_height, refresh_rate=100):
-        """
-        win_width - pixels
-        win_height - pixels
-        refresh_rate - timeout between screen updates (ms)
-        """
-        self.dimensions = (win_width, win_height)
-        self.refresh_rate = refresh_rate
-
-        self.screen_update = SCREEN_UPDATE_BLANK
-
-        self.clock = pygame.time.Clock()
-        self.surface = pygame.display.set_mode(self.dimensions)
-
-    def update(self):
-        raise NotImplementedError
-
-    def _draw(self):
-        if self.screen_update == SCREEN_UPDATE_BLANK:
-            self.surface.fill((0,0,0))
-        self.update()
-        pygame.display.flip()
-
-    def run(self):
-        self.running = True
-        pygame.time.set_timer(USEREVENT+1, self.refresh_rate)
-
-        while self.running:
-            for event in pygame.event.get():
-                if event.type == USEREVENT+1:
-                    self._draw()
-                if event.type == QUIT:
-                    self.running = False
-                    break
-
-        pygame.quit()

drawer/test1.py

-from __future__ import division
-import math
-import random
-
-import pygame
-from pygame.locals import *
-
-import canvas
-
-class TestCanvas(canvas.AnimatedCanvas):
-    rgb = [10, 10, 10]
-    drawat = [10, 10]
-    radius = 10
-
-    def _get_movement(self, mv, b):
-        direction = random.randint(0,2)-1
-        move = random.randint(*b) * direction
-        return mv + move
-
-    def _bounds_check(self, bv, ub, lb = 0):
-        if bv > ub:
-            return bv - ub
-        if bv < lb:
-            return bv + ub
-        return bv
-
-
-    def affect(self, values, bounds, upper_bound):
-        if (not hasattr(values, "strip") and hasattr(values, "__getitem__") or hasattr(values, "__iter__")):
-            new_values = []
-            for val in values:
-                nv = self._get_movement(val, bounds)
-                new_values.append(self._bounds_check(nv, upper_bound))
-            return new_values
-        else:
-            nv = self._get_movement(values, bounds)
-            return self._bounds_check(nv, upper_bound)
-
-    def update(self):
-        self.drawat = self.affect(self.drawat, (5, 50), 500)
-        self.radius = self.affect(self.radius, (1, 10), 100)
-        self.rgb = self.affect(self.rgb, (1,20), 254)
-        pygame.draw.circle(self.surface, self.rgb, self.drawat, self.radius)
-
-if __name__ == "__main__":
-    tc = TestCanvas(500,500,50)
-    tc.screen_update = canvas.SCREEN_UPDATE_ADD
-    tc.run()
-

drawing/brownian.py

+from __future__ import division
+import math
+import random
+
+import pygame
+from pygame.locals import *
+
+import canvas
+
+class circle(object):
+    def __init__(self, canvas_size, start_pos):
+        self.bounds = canvas_size
+        self.position = self.gen_position(start_pos, 10)
+        self.radius = self.gen_radius(15)
+        self.colour = self.gen_colour(5)
+
+    def gen_position(self, start_pos, max_distance):
+        x = start_pos[0]
+        y = start_pos[1]
+        while 1:
+            x += (random.randint(1, max_distance*2) - max_distance)
+            y += (random.randint(1, max_distance*2) - max_distance)
+            if x > self.bounds[0]: x = 1
+            if x < 1: x = self.bounds[0]
+            if y > self.bounds[1]: y = 1
+            if y < 1: y = self.bounds[1]
+            yield (x,y)
+
+    def gen_radius(self, max_radius):
+        while 1:
+            yield random.randint(1, max_radius)
+
+    def gen_colour(self, max_diff):
+        r = 0
+        g = 0
+        b = [100, random.randint(1, max_diff)]
+        
+        while 1:
+            b[0] += b[1]
+
+            if b[0] > 254:
+                b[0] = 255
+                b[1] *= -1
+            if b[0] < 0: 
+                b[0] = 0
+                b[1] *= -1
+            yield (r,g,b[0])
+
+class TestCanvas(canvas.AnimatedCanvas):
+    iteration = 0
+    circles_now = 1
+    circles_bounds = (5, 100)
+
+    def __init__(self, *args, **kwargs):
+        super(TestCanvas, self).__init__(*args, **kwargs)
+        self.circles = []
+        for i in range(self.circles_now):
+            self.add_circle()
+
+    def add_circle(self):
+        self.circles.append(circle(
+                self.dimensions, (
+                    random.randint(1, self.dimensions[0]),
+                    random.randint(1, self.dimensions[1]))))
+
+
+    def update(self):
+        self.iteration += 1
+
+        #fade current surface back by 10/255
+        self.surface.fill((0,0,0,10))
+
+        if self.iteration % 25 == 0:
+            value = random.randint(1,10)
+            if self.circles_now > self.circles_bounds[1]:
+                direction = -1
+            elif self.circles_now < self.circles_bounds[0]:
+                direction = 1
+            else:
+                direction = random.randint(0,2)-1
+
+            self.circles_now += (value * direction)
+
+        circles_diff = self.circles_now - len(self.circles)
+
+        if circles_diff > 0:
+            for i in range(circles_diff):
+                self.add_circle()
+        elif circles_diff < 0:
+            self.circles = self.circles[:self.circles_now]
+            
+        
+        for c in self.circles:
+            pygame.draw.circle(
+                self.surface, 
+                c.colour.next(), 
+                c.position.next(), 
+                c.radius.next())
+
+
+if __name__ == "__main__":
+    tc = TestCanvas(1000,200,50)
+    tc.screen_update = canvas.SCREEN_UPDATE_ADD
+    tc.run()

drawing/canvas.py

+ #! /usr/bin/env python
+#display
+
+import pygame
+from pygame.locals import *
+
+pygame.init()
+
+SCREEN_UPDATE_ADD = 1
+SCREEN_UPDATE_BLANK = 2
+
+class AnimatedCanvas(object):
+    def __init__(self, win_width, win_height, refresh_rate=100):
+        """
+        win_width - pixels
+        win_height - pixels
+        refresh_rate - timeout between screen updates (ms)
+        """
+        self.dimensions = (win_width, win_height)
+        self.refresh_rate = refresh_rate
+
+        self.screen_update = SCREEN_UPDATE_BLANK
+
+        self.clock = pygame.time.Clock()
+        self.screen = pygame.display.set_mode(self.dimensions, DOUBLEBUF)
+        self.surface = self.screen.copy().convert_alpha()
+
+    def update(self):
+        raise NotImplementedError
+
+    def _draw(self):
+        if self.screen_update == SCREEN_UPDATE_BLANK:
+            self.surface.fill((0,0,0))
+        self.update()
+        self.screen.blit(self.surface, (0,0))
+        pygame.display.flip()
+
+    def run(self):
+        self.running = True
+        pygame.time.set_timer(USEREVENT+1, self.refresh_rate)
+
+        while self.running:
+            for event in pygame.event.get():
+                if event.type == USEREVENT+1:
+                    self._draw()
+                if event.type == QUIT:
+                    self.running = False
+                    break
+
+        pygame.quit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.