Source

sketchbook / 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()