Skip to content

Instantly share code, notes, and snippets.

@SebastianJarsve
Last active December 15, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianJarsve/5297728 to your computer and use it in GitHub Desktop.
Save SebastianJarsve/5297728 to your computer and use it in GitHub Desktop.
from scene import *
from sound import play_effect
from PIL import Image
import urllib, cStringIO
class StartPhoto(object):
def __init__(self):
self.title = 'Python'
self.link = 'http://imgs.xkcd.com/comics/python.png'
self.file = Image.open(cStringIO.StringIO(urllib.urlopen(self.link).read())).convert('RGBA')
self.img = load_pil_image(self.file)
self.bbox = Rect(*self.file.getbbox())
self.bbox.center(Point(WIDTH/2, HEIGHT/2))
self.link_layer = TextLayer(self.link, 'Futura', 20)
self.link_layer.frame.center(Point(WIDTH/2, HEIGHT-50))
class Photo(object):
def __init__(self):
self.picture_html = urllib.urlopen("http://dynamic.xkcd.com/random/comic/")
self.link = ''
for lines in self.picture_html.readlines():
self.link += lines
self.title = self.link.split('<title>xkcd: ')[1].split('</title>')[0]
self.link=self.link.split("Image URL (for hotlinking/embedding): ")[1].split('<')[0]
self.file = cStringIO.StringIO(urllib.urlopen(self.link).read())
self.file = Image.open(self.file).convert('RGBA')
self.img = load_pil_image(self.file)
self.bbox = Rect(*self.file.getbbox())
self.bbox.center(Point(WIDTH/2, HEIGHT/2))
self.link_layer = TextLayer(self.link, 'Futura', 20)
self.link_layer.frame.center(Point(WIDTH/2, HEIGHT-50))
class NextButton(object):
def __init__(self):
self.wh = Size(50, 50)
self.x = WIDTH-self.wh.w*2
self.y = HEIGHT-self.wh.h*2
def draw(self):
image('Typicons192_Right', self.x, self.y, *self.wh)
def bbox(self):
return Rect(self.x, self.y, *self.wh)
class PrevButton(object):
def __init__(self):
self.wh = Size(50, 50)
self.x = self.wh.w
self.y = HEIGHT-self.wh.h*2
def draw(self):
image('Typicons192_Left', self.x, self.y, *self.wh)
def bbox(self):
return Rect(self.x, self.y, *self.wh)
class SaveButton(object):
def __init__(self):
self.wh = Size(50, 50)
self.x = WIDTH-self.wh.w*2
self.y = 50
self.bbox = Rect(self.x, self.y, *self.wh)
def draw(self):
image('Typicons192_Archive', self.x, self.y, *self.wh)
def touch_began(self, t, img):
if t.location in self.bbox:
img.file.save("./XKCD/%s.png" % img.title)
play_effect('Coin_1')
class ZoomButtons(object):
def __init__(self):
self.tp = False
self.tm = False
self.percent = 0
self.pluss = Layer(Rect(100, 50, 50, 50))
self.pluss.image = 'Typicons48_Zoom_In'
self.minus = Layer(Rect(50, 50, 50, 50))
self.minus.image = 'Typicons48_Zoom_Out'
def draw(self, obj):
self.pluss.draw()
self.minus.draw()
obj.bbox.w += (obj.bbox.w*self.percent)/100
obj.bbox.h += (obj.bbox.h*self.percent)/100
if self.tp:
self.percent+=0.01
if self.tm:
self.percent-=0.01
def touch_began(self, touch):
if touch.location in self.pluss.frame:
self.tp = True
if touch.location in self.minus.frame:
self.tm = True
def touch_ended(self, touch):
self.tp = False
self.tm = False
self.percent = 0
class MyScene(Scene):
def setup(self):
global WIDTH, HEIGHT
WIDTH = self.size.w
HEIGHT = self.size.h
self.seen_images = []
self.zoom = ZoomButtons()
self.next = NextButton()
self.prev = PrevButton()
self.save = SaveButton()
self.img = StartPhoto()
self.seen_images.append(self.img)
def draw(self):
background(0,0,0)
text(str(self.seen_images.index(self.img)+1), font_size=24, x=self.size.w/2,
y=self.size.h-100)
self.img.link_layer.draw()
image(self.img.img, *self.img.bbox)
self.next.draw()
self.prev.draw()
self.save.draw()
self.zoom.draw(self.img)
if len(self.seen_images) > 10:
del self.seen_images[0]
def touch_moved(self, t):
if t.location in self.img.bbox:
self.img.bbox.x += t.location.x-t.prev_location.x
self.img.bbox.y += t.location.y-t.prev_location.y
def touch_began(self, t):
self.zoom.touch_began(t)
self.save.touch_began(t, self.img)
if t.location in self.next.bbox():
if len(self.seen_images) > self.seen_images.index(self.img)+1:
self.img = self.seen_images[self.seen_images.index(self.img)+1]
else:
self.img = Photo()
self.seen_images.append(self.img)
if t.location in self.prev.bbox():
if self.seen_images.index(self.img) > 0:
self.img = self.seen_images[self.seen_images.index(self.img)-1]
else:
return
def touch_ended(self, t):
self.zoom.touch_ended(t)
run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment