×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Belgium (Europe)
    Posts
    1,352

    A nice psychedelic show app

    Hello ,

    A nice psychedelic show for Python 1.2

    Code:
    #
    # psychedelic.py
    #
    # author : Found on Lorecraft blog !
    #
    import appuifw
    import e32
    from graphics import *
    import random
    	
    class PsychedelicShow:
        def __init__(self):
            self.shadowsize = 50
            self.increment=[5, 5, 5, 5]
            self.shadowincrement=[5, 5, 5, 5]
            self.exitflag=0
            self.color=(132,43,243)
            self.old_body=appuifw.app.body
            self.canvas=appuifw.Canvas(redraw_callback=self.redraw)
            self.draw=Draw(self.canvas)
            appuifw.app.body=self.canvas
            self.limitX = self.canvas.size[0]
            self.limitY = self.canvas.size[1]
            self.loc=[random.randint(0,self.limitX-1),
                random.randint(0,self.limitY-1),
                random.randint(0,self.limitX-1),
                random.randint(0,self.limitY-1)]
            self.shadowloc=[self.loc[0],self.loc[1],self.loc[2],self.loc[3]]
            self.draw.clear((0,0,0))
            self.redraw(())
        def close_canvas(self): # break reference cycles
            self.draw=None
            appuifw.app.body=self.old_body
            self.canvas=None
            appuifw.app.exit_key_handler=None
        def redraw(self,rect):
            pass
        def set_exit(self):
            self.exitflag=1
        def draw_lines(self, loc, color):
            self.draw.line(loc, outline=color)
            self.draw.line((self.limitX - loc[0], loc[1], self.limitX - loc[2], loc[3]), outline=color)
            self.draw.line((loc[0], self.limitY - loc[1], loc[2], self.limitY - loc[3]), outline=color)
            self.draw.line((self.limitX - loc[0], self.limitY - loc[1], self.limitX - loc[2], self.limitY - loc[3]), outline=color)
        def draw_shadow_lines(self):
            self.draw_lines(self.shadowloc, (0,0,0))
        def check(self, location, index, increment, criteria, initvalue):
            collision = False
            if criteria():
                increment[index] = increment[index] * -1
                location[index] = initvalue
                collision = True
            return collision
    	
        def update(self, location, increment, updatecolor=True):
            location[0] = location[0] + increment[0]
            collision = self.check(location, 0, increment, lambda: location[0] >= self.limitX, self.limitX-1)
            collision = collision or self.check(location, 0, increment, lambda: location[0] <= 0, 0)
    	
            location[1] = location[1] + increment[1]
    	
            collision = collision or self.check(location, 1, increment, lambda: location[1] >= self.limitY, self.limitY-1)
            collision = collision or self.check(location, 1, increment, lambda: location[1] <= 0, 0)
    	
            location[2] = location[2] + increment[2]
    	
            collision = collision or self.check(location, 2, increment, lambda: location[2] >= self.limitX, self.limitX-1)
            collision = collision or self.check(location, 2, increment, lambda: location[2] <= 0, 0)
    	
            location[3] = location[3] + increment[3]
    	
            collision = collision or self.check(location, 3, increment, lambda: location[3] >= self.limitY, self.limitY-1)
            collision = collision or self.check(location, 3, increment, lambda: location[3] <= 0, 0)
    	
            if collision and updatecolor:
                self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        def run(self):
            appuifw.app.exit_key_handler=self.set_exit
            while not self.exitflag:
                self.draw_lines(self.loc, self.color)
                self.update(self.loc, self.increment)
                e32.ao_sleep(0.08)
                if self.shadowsize > 0:
                    self.shadowsize = self.shadowsize - 1
                else:
                    self.draw_shadow_lines()
                    self.update(self.shadowloc, self.shadowincrement, False)
            self.close_canvas()
    appuifw.app.screen='full'
    game=PsychedelicShow()
    game.run()

  2. #2
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    158

    Re: A nice psychedelic show app

    HHH! Very nice and retro screensaver!

  3. #3
    Regular Contributor
    Join Date
    Jan 2005
    Location
    Bangkok, Thailand
    Posts
    148

    Re: A nice psychedelic show app


  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    Re: A nice psychedelic show app

    Glad you like it... I have since added the ability to take a screenshot and upload it to an online gallery just by clicking one button. Some samples here http://demo.etnoteam.fi/~zsolt/uploader/

    The app listens to the hash and star keys and takes a screenshot or a photo respectively and uploads it to a php script.

    One thing I couldn't figure out is how to add the binary contents of the png file to the multipart message because the body = CRLF.join(L) line didn't like binary data. So I bas64 encoded the file, and then it works. The php script needs to decode it and save the contents as a file.

    [code]
    #
    # psychedelic.py
    #
    # by Zsolt Szasz http://blog.lorecraft.com
    #
    import appuifw
    import e32
    from graphics import *
    import random
    from key_codes import *
    import httplib
    import base64
    import camera

    class Keyboard(object):
    def __init__(self,onevent=lambda:None):
    self._keyboard_state={}
    self._downs={}
    self._onevent=onevent
    def handle_event(self,event):
    if event['type'] == appuifw.EEventKeyDown:
    code=event['scancode']
    if not self.is_down(code):
    self._downs[code]=self._downs.get(code,0)+1
    self._keyboard_state
    Code:
    =1
            elif event['type'] == appuifw.EEventKeyUp:
                self._keyboard_state[event['scancode']]=0
            self._onevent()
        def is_down(self,scancode):
            return self._keyboard_state.get(scancode,0)
        def pressed(self,scancode):
            if self._downs.get(scancode,0):
                self._downs[scancode]-=1
                return True
            return False
    keyboard=Keyboard()
    
    class PsychedelicShow:
        def __init__(self):
            self.uploader = Uploader()
            self.shadowsize = 50
            self.increment=[2, 2, 3, 3]
            self.shadowincrement=[2, 2, 3, 3]
            self.exitflag=0
            self.color=(132,43,243)
            self.old_body=appuifw.app.body
            self.canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=self.redraw)
            self.draw=Draw(self.canvas)
            appuifw.app.body=self.canvas
            self.limitX = self.canvas.size[0]
            self.limitY = self.canvas.size[1]
            self.loc=[random.randint(0,self.limitX-1),
                random.randint(0,self.limitY-1),
                random.randint(0,self.limitX-1),
                random.randint(0,self.limitY-1)]
            self.shadowloc=[self.loc[0],self.loc[1],self.loc[2],self.loc[3]]
            self.draw.clear((0,0,0))
            self.redraw(())
        def close_canvas(self): # break reference cycles
            self.draw=None
            appuifw.app.body=self.old_body
            self.canvas=None
            appuifw.app.exit_key_handler=None
        def redraw(self,rect):
            pass
        def set_exit(self):
            self.exitflag=1
        def draw_lines(self, loc, color):
            self.draw.line(loc, outline=color)
            self.draw.line((self.limitX - loc[0], loc[1], self.limitX - loc[2], loc[3]), outline=color)
            self.draw.line((loc[0], self.limitY - loc[1], loc[2], self.limitY - loc[3]), outline=color)
            self.draw.line((self.limitX - loc[0], self.limitY - loc[1], self.limitX - loc[2], self.limitY - loc[3]), outline=color)
        def draw_shadow_lines(self):
            self.draw_lines(self.shadowloc, (0,0,0))
        def check(self, location, index, increment, criteria, initvalue):
            collision = False
            if criteria():
                increment[index] = increment[index] * -1
                location[index] = initvalue
                collision = True
            return collision
        
        def update(self, location, increment, updatecolor=True):
            location[0] = location[0] + increment[0]
            collision = self.check(location, 0, increment, lambda: location[0] >= self.limitX, self.limitX-1)                            
            collision = collision or self.check(location, 0, increment, lambda: location[0] <= 0, 0)
    
            location[1] = location[1] + increment[1]
    
            collision = collision or self.check(location, 1, increment, lambda: location[1] >= self.limitY, self.limitY-1)                    
            collision = collision or self.check(location, 1, increment, lambda: location[1] <= 0, 0)
    
            location[2] = location[2] + increment[2]
    
            collision = collision or self.check(location, 2, increment, lambda: location[2] >= self.limitX, self.limitX-1)                            
            collision = collision or self.check(location, 2, increment, lambda: location[2] <= 0, 0)
    
            location[3] = location[3] + increment[3]
            
            collision = collision or self.check(location, 3, increment, lambda: location[3] >= self.limitY, self.limitY-1)                    
            collision = collision or self.check(location, 3, increment, lambda: location[3] <= 0, 0)
    
            if collision and updatecolor:
                self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        def run(self):
            appuifw.app.exit_key_handler=self.set_exit
            while not self.exitflag:
                self.draw_lines(self.loc, self.color)
                self.update(self.loc, self.increment)
                e32.ao_sleep(0.08)
                if self.shadowsize > 0:
                    self.shadowsize = self.shadowsize - 1
                else:
                    self.draw_shadow_lines()
                    self.update(self.shadowloc, self.shadowincrement, False)
                if keyboard.pressed(EScancodeStar):
                    img = screenshot()
                    self.canvas.text((0,32),u'Saving photo...',fill=0xffff00)
                    photo = camera.take_photo()                
                    self.uploader.upload(photo.resize((self.limitX, self.limitY), callback=None, keepaspect=1))
                    self.canvas.blit(img)
                if keyboard.pressed(EScancodeHash):
                    img = screenshot()
                    self.canvas.text((0,32),u'Saving screenshot...',fill=0xffff00)
                    self.uploader.upload(img)
                    self.canvas.blit(img)
            self.close_canvas()
    class Uploader:
        def upload(self, img):
            filename=u'e:\\screenshot.png'
            img.save(filename)
            contents = open(filename, 'r').read()
            img_content = [(u"userfile", u"screenshot.png", base64.encodestring(contents))]
            host = u"demo.etnoteam.fi"
            path = u"/~zsolt/uploader/index.php"
            result = unicode(self.post_multipart(host, path, img_content))
            #open(u'e:\\result.txt', 'w').write(result)
        def post_multipart(self, host, selector, files):
            """
            Post fields and files to an http host as multipart/form-data.
            fields is a sequence of (name, value) elements for regular form fields.
            files is a sequence of (name, filename, value) elements for data to be uploaded as files
            Return the server's response page.
            """
            content_type, body = self.encode_multipart_formdata(files)
            h = httplib.HTTP(host)
            h.putrequest('POST', selector)
            h.putheader('content-type', content_type)
            h.putheader('content-length', str(len(body)))
            h.endheaders()
            #open(u'e:\\request.txt', 'w').write(body)
            h.send(body)
            errcode, errmsg, headers = h.getreply()
            return h.file.read()
    
        def encode_multipart_formdata(self, files):
            """
            fields is a sequence of (name, value) elements for regular form fields.
            files is a sequence of (name, filename, value) elements for data to be uploaded as files
            Return (content_type, body) ready for httplib.HTTP instance
            """
            BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
            CRLF = '\r\n'
            L = []
            for (key, filename, value) in files:
                L.append('--' + BOUNDARY)
                L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
                L.append('Content-Type: %s' % self.get_content_type(filename))
                L.append('Content-Transfer-Encoding: base64')
                L.append('')
                L.append(value)
            L.append('--' + BOUNDARY + '--')
            L.append('')
            body = CRLF.join(L)
            content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
            return content_type, body
        
        def get_content_type(self, filename):
            return 'image/png'
    
    
    appuifw.app.screen='full'
    game=PsychedelicShow()
    game.run()
    Last edited by zsolt77; 2005-11-12 at 18:25.

  5. #5
    Regular Contributor
    Join Date
    Jul 2004
    Posts
    76

    Re: A nice psychedelic show app

    Very nice!!

Similar Threads

  1. Dialog's clipping area (again). Nokia experts, please help
    By synov in forum Symbian User Interface
    Replies: 0
    Last Post: 2004-06-26, 14:35
  2. app icons do not show on 6600
    By palau in forum Mobile Java General
    Replies: 0
    Last Post: 2003-12-11, 10:16
  3. RecordStore reads don't show up until app exit (7210)
    By dwneonstar in forum Mobile Java General
    Replies: 5
    Last Post: 2003-11-11, 13:31
  4. App doesnt show
    By sbc123 in forum Symbian
    Replies: 3
    Last Post: 2003-07-12, 18:35
  5. Launching an app from pJava..
    By krajag in forum PersonalJava
    Replies: 0
    Last Post: 2002-10-10, 19:57

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×