Created
May 20, 2012 18:09
-
-
Save PiotrZakrzewski/2758988 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on May 20, 2012 | |
@author: Piotr Zakrzewski | |
''' | |
import collections | |
import pygame | |
import threading | |
import random | |
class Circle(object): | |
def __init__(self,pos,radius,color): | |
self.pos = pos | |
self.radius = radius | |
self.color = color | |
self.children = list() | |
def draw(self,surface): | |
pygame.draw.circle(surface,self.color,self.pos,self.radius) | |
class FluctuatingCircle(Circle): | |
signs = [1,-1] | |
def __init__(self,pos,radius,color,fluctuation): | |
Circle.__init__(self, pos, radius, color) | |
self.fluctuation = fluctuation | |
def update(self): | |
self.fluctuate() | |
def fluctuate(self): | |
x,y = self.pos | |
x = FluctuatingCircle.deviate(x,self.fluctuation) | |
y = FluctuatingCircle.deviate(y,self.fluctuation) | |
newPos = x,y | |
# print(self.pos,newPos) | |
self.pos = newPos | |
@classmethod | |
def deviate(cls,number,deviation): | |
random_sign = random.choice(cls.signs) | |
deviated = number + (deviation* random_sign) | |
return int(deviated) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
import blob | |
input_delay = 8000 # time gap between handling user input events (in miliseconds). | |
WINDOW_WIDTH = 512 | |
WINDOW_HEIGHT = 512 | |
def main(): | |
""" | |
This function is being executed when this file is run as a main module | |
""" | |
init_game() | |
game_main_loop() | |
def game_main_loop(): | |
clock = pygame.time.Clock() | |
while True: | |
clock.tick(input_delay) | |
events = pygame.event.get() | |
user_input(events) | |
update_Blobs() | |
pygame.display.update() | |
def update_Blobs(): | |
for blob in blobs: | |
blob.update() | |
blob.draw(window) | |
def init_game(): | |
global window | |
pygame.init() | |
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) | |
def _isLMB(event): | |
if event.button == 1: | |
return True | |
else: | |
return False | |
blobs = list() | |
def handleMouseClick(event): | |
pos = event.pos | |
radius = random.randint(10,100) | |
color = _random_color() | |
b = blob.FluctuatingCircle(pos,radius,color,1) | |
blobs.append(b) | |
def _random_color(): | |
r = random.randint(0,255) | |
g = random.randint(0,255) | |
b = random.randint(0,255) | |
return r,g,b | |
def user_input(events): | |
for event in events: | |
if event.type == pygame.QUIT: | |
raise SystemExit | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
handleMouseClick(event) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment