Created
July 26, 2015 18:53
-
-
Save ssophwang/fa7003e00ab881b2b1ec to your computer and use it in GitHub Desktop.
crazy_bubbles_homework_2.py
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
from scene import * | |
import math | |
from random import random | |
class MyScene (Scene): | |
circles = [] | |
max_score = 0 | |
game_began = False | |
speed = 5 | |
initial_radius = 10 | |
def setup(self): | |
# This will be called before the first frame is drawn. | |
pass | |
def in_circle(self, p, c): | |
dist = p.distance(c['center']) | |
if dist > c['radius'] + self.initial_radius + 1: | |
return False | |
else: | |
return True | |
def circles_touch(self, c1, c2): | |
d = c1['center'].distance(c2['center']) | |
limit = (d-c1['radius']-c2['radius'])/2 | |
if limit < c1['grow_limit']: | |
c1['grow_limit'] = limit | |
if limit < c2['grow_limit']: | |
c2['grow_limit'] = limit | |
if d > c1['radius'] + c2['radius'] + 1: | |
return False | |
else: | |
return True | |
def boundary_touch(self, c): | |
c['grow_limit'] = min(c['center'].x - c['radius'], | |
self.bounds.w - c['center'].x - c['radius'], | |
c['center'].y - c['radius'], | |
self.bounds.h - c['center'].y - c['radius']) | |
if c['center'].x - c['radius'] <= 0 or c['center'].x + c['radius'] >= self.bounds.w or c['center'].y - c['radius'] <= 0 or c['center'].y + c['radius']>= self.bounds.h: | |
return True | |
else: | |
return False | |
def draw(self): | |
# This will be called for every frame (typically 60 times per second). | |
background(1, 1, 1) | |
for c1 in self.circles: | |
if self.boundary_touch(c1): | |
c1['stuck'] = True | |
for c2 in self.circles: | |
if c1 != c2 and self.circles_touch(c1, c2): | |
c1['stuck'] = True | |
c2['stuck'] = True | |
for c in self.circles: | |
fill(c['color'][0], c['color'][1], c['color'][2], c['color'][3]) | |
ellipse(c['center'].x - c['radius'], c['center'].y - c['radius'], | |
2*c['radius'], 2*c['radius']) | |
if c['stuck'] == False: | |
c['radius'] += min(self.speed, c['grow_limit']) | |
if len(self.circles) > self.max_score: | |
self.max_score = len(self.circles) | |
tint(0, 0, 0) | |
text('Score: ' + str(len(self.circles)), font_size = 30, x = 500, y= 20, alignment = 5) | |
text('Highest Score: ' + str(self.max_score), font_size = 50, x = 500, y= 80, alignment = 5) | |
if not self.game_began: | |
tint(0,0,1) | |
text('Crazy Bubbles 2', font_name = 'Noteworthy-Bold', font_size = 100, x = 500, y= 400, alignment = 5) | |
def touch_began(self, touch): | |
self.game_began = True | |
center = Point(touch.location.x, touch.location.y) | |
color = (random(), random(), random(), 0.5) | |
not_in_any_circles = True | |
for c in self.circles: | |
if self.in_circle(touch.location, c): | |
not_in_any_circles = False | |
if not_in_any_circles == True: | |
circle = {'center': center, | |
'color' : color, | |
'radius': self.initial_radius, | |
'stuck' : False, | |
'grow_limit': self.speed} | |
self.circles.append(circle) | |
def touch_moved(self, touch): | |
pass | |
def touch_ended(self, touch): | |
pass | |
run(MyScene()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment