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()
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.