Created
November 12, 2013 22:42
-
-
Save jamesporter/7440159 to your computer and use it in GitHub Desktop.
A Light to Guide You
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 * | |
from math import * | |
from random import random | |
speed = 5.0 | |
d_size = 20.0 | |
demons = 4 | |
class Demon: | |
def __init__(self, id, scn): | |
self.id = id | |
self.x, self.y = scn.size.w * (id + 1.0)/(demons + 1.0), scn.size.h / 1.25 | |
def distance(self, other): | |
return sqrt((other.x - self.x)*(other.x - self.x) + | |
(other.y - self.y)*(other.y - self.y)) | |
def draw(self, scn): | |
h = self.distance(scn) | |
if h > 300.0: | |
alpha = 0 | |
else: | |
alpha = (300.0 - h)/300 | |
fill(1.00, 0.00, 0.00, alpha) | |
ellipse(self.x, self.y, d_size, d_size) | |
if h > 25.0: | |
dx = (scn.x - self.x)/h | |
dy = (scn.y - self.y)/h | |
self.x += (dx * speed/2) | |
self.y += (dy * speed/2) | |
else: | |
scn.in_game = False | |
for d in scn.demons: | |
if not d == self: | |
d_dist = self.distance(d) | |
if d_dist < 50.0: | |
dx = -(d.x - self.x)/h | |
dy = -(d.y - self.y)/h | |
self.x += (dx * speed/2) | |
self.y += (dy * speed/2) | |
class GuidingLight (Scene): | |
def setup(self): | |
# This will be called before the first frame is drawn. | |
self.x,self.y = self.size.w/2.0, self.size.h/4.0 | |
self.touching = 0 | |
self.in_game = True | |
self.setup_demons() | |
self.score = 0 | |
def draw(self): | |
if self.in_game: | |
self.draw_game() | |
self.draw_score() | |
self.score+= 1 | |
if self.score % 100 == 0: | |
self.add_demon() | |
else: | |
self.draw_gameover() | |
def draw_gameover(self): | |
background(0.50, 0.00, 0.00) | |
text('Game Over', font_name='Optima-ExtraBlack', font_size=128.0, x=self.size.w/2, y=self.size.h*0.6, alignment=5) | |
text('Score: %i' % self.score, font_name='Optima-ExtraBlack', font_size=64.0, x=self.size.w/2, y=self.size.h * 0.4, alignment=5) | |
def draw_score(self): | |
text('Score: %i' % self.score, font_name='Optima-ExtraBlack', font_size=64.0, x=self.size.w/2, y=self.size.h * 0.9, alignment=5) | |
def draw_game(self): | |
# This will be called for every frame (typically 60 times per second). | |
if self.touching > 0: | |
background(0.10, 0.10, 0.10) | |
else: | |
background(0.00, 0.00, 0.00) | |
for touch in self.touches.values(): | |
self.draw_light(touch, 10) | |
fill(1.00, 1.00, 1.00) | |
ellipse(self.x - 25,self.y - 25, 50,50) | |
self.update_position() | |
self.update_demons() | |
def update_position(self): | |
#update position of follower | |
if self.touches.values(): | |
t = self.touches.values()[0] | |
tx, ty = t.location.x, t.location.y | |
h = sqrt((tx -self.x)*(tx -self.x) +(ty-self.y)*(ty-self.y)) | |
if h > 25.0: | |
dx = (tx - self.x)/h | |
dy = (ty - self.y)/h | |
self.x += dx * speed | |
self.y += dy * speed | |
def draw_light(self, touch, detail): | |
for i in xrange(detail): | |
size = 120.0 * (detail - i) / detail | |
fill(1.00, 0.80, 0.40, 0.4 * random() + 0.4 * i / detail) | |
ellipse(touch.location.x - size/2, touch.location.y - size/2, size, size) | |
def setup_demons(self): | |
self.demons = [] | |
for i in xrange(4): | |
self.demons.append(Demon(i, self)) | |
def add_demon(self): | |
d = Demon(len(self.demons), self) | |
d.x = self.size.w / 2.0 | |
if random() > 0.5: | |
d.y = -50 | |
else: | |
d.y = self.size.h + 50 | |
self.demons.append(d) | |
def update_demons(self): | |
for d in self.demons: | |
d.draw(self) | |
def touch_began(self, touch): | |
self.touching += 1 | |
def touch_moved(self, touch): | |
pass | |
def touch_ended(self, touch): | |
self.touching -= 1 | |
run(GuidingLight()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little Game for Pythonista (Python on iOS).