15 October 2006

flowering.py

I've been playing with a Paint-like program on my 6600 (with Python for Series 60). The cool things is after I paint a picture, I can take a screenshot of it and use that image as the background for my phone. To paint the second pic (the first one is similar), you first gravitate (bounce ball), then pretend you are Bob Ross (hit zero key) (remember Bob Ross's paintings? occasionally, before starting, he would put an oval cut out over the actual canvas he was painting on — after he was done, he would remove the cutout and all of a sudden the painting itself would be oval shaped..), then realize that you really don't need to be him (hit zero key again) and finally start gravitating once again. Here are some flowerings: The program, flowering.py isn't well polished:
# sri, oct 15, 2006
# under mit license
# used Nokia's ball.py example as reference implementation;
# Menu Key: allows you to select type of brush
# Exit: exit app
# Hash Key (#): take a pic
# Star Key (*): for fun!

import appuifw, graphics, e32
from key_codes import *

class Keyboard:
    def __init__(self):
        self.state = {}
        self.downs = {}

    def handle_event(self, event):
        code = event.get("scancode", False)
        if event["type"] == appuifw.EEventKeyDown:
            if not self.is_down(code):
                self.downs[code] = self.downs.get(code, 0) + 1
            self.state[code] = 1
        elif event["type"] == appuifw.EEventKeyUp:
            self.state[code] = 0

    def is_down(self, scancode):
        return self.state.get(scancode, 0)

    def pressed(self, scancode):
        if self.downs.get(scancode, 0):
            self.downs[scancode] -= 1
            return True
        return False

# --------------------------------------------------------------------

gravitate = False  # brush it a  bouncing ball
justpaint = False  # brush is a brush, OK?

def do_gravitate():
    global gravitate, justpaint
    gravitate = True
    justpaint = False
def do_justpaint():
    global justpaint, gravitate
    justpaint = True
    gravitate = False

appuifw.app.menu = [
    (u"Gravitate", do_gravitate),
    (u"Just paint", do_justpaint)
    ]
keyboard = Keyboard()

running = True
def quit():
    global running
    running = False
appuifw.app.exit_key_handler = quit

appuifw.app.screen = "full"
appuifw.app.body = c = appuifw.Canvas(
    event_callback=keyboard.handle_event)

call_me_bob_ross = 0
radius = min(c.size)/2
xcenter, ycenter = c.size[0]/2, c.size[1]/2

topleft = (xcenter-radius,
           ycenter-radius)
bottomright = (xcenter+radius,
               ycenter+radius)

# ball stuff:
position = [0, 0]
velocity = [0, 0]
acceleration = 0.05
gravity = 0.03

c_size = c.size
#oldposition = position

# ball color stuff:
coloridx = 0
allcolors = []
tmp = (0, 32, 64, 128, 255)
for r in tmp:
    for g in tmp:
        for b in tmp:
            allcolors.append((r, g, b))
allcolors_len = len(allcolors)

# --------------------------------------------------------------------

while running:
    coloridx += 1
    if coloridx >= allcolors_len:
        coloridx = 0

    c.point(position,
            allcolors[coloridx],
            width=15)

    if call_me_bob_ross%2 != 0:
        c.ellipse(((0,0), c.size),
                  outline=(255, 0, 0), # red,
                  width=1)

    e32.ao_yield()

    if gravitate:
        # -----------------------------------------------------
        velocity[0] *= 0.999
        velocity[1] *= 0.999
        velocity[1] += gravity
        oldposition = position
        position[0] += velocity[0]
        position[1] += velocity[1]

        if position[0] > c_size[0]:
            position[0] = c_size[0] - (position[0] - c_size[0])
            velocity[0] = -1.00 * velocity[0]
            velocity[1] =  1.00 * velocity[1]
        if position[0] < 0:
            position[0] = -position[0]
            velocity[0] = -1.00 * velocity[0]
            velocity[1] =  1.00 * velocity[1]
        if position[1] > c_size[1]:
            position[1] = c_size[1] - (position[1] - c_size[1])
            velocity[0] =  1.00 * velocity[0]
            velocity[1] = -1.00 * velocity[1]
        if position[1] < 0:
            position[1] = -position[1]
            velocity[0] =  1.00 * velocity[0]
            velocity[1] = -1.00 * velocity[1]

        if keyboard.is_down(EScancodeLeftArrow):
            velocity[0] -= acceleration
        if keyboard.is_down(EScancodeRightArrow):
            velocity[0] += acceleration
        if keyboard.is_down(EScancodeDownArrow):
            velocity[1] += acceleration
        if keyboard.is_down(EScancodeUpArrow):
            velocity[1] -= acceleration
        # -----------------------------------------------------
    else:
        # just paint
        if keyboard.is_down(EScancodeLeftArrow):
            position[0] -= 1
        if keyboard.is_down(EScancodeRightArrow):
            position[0] += 1
        if keyboard.is_down(EScancodeDownArrow):
            position[1] += 1
        if keyboard.is_down(EScancodeUpArrow):
            position[1] -= 1

    # common actions:
    if keyboard.pressed(EScancodeHash):
        if call_me_bob_ross%2 != 0:
            c.ellipse(((0,0), c.size),
                      outline=(255, 255, 255),
                      width=100)
        img = graphics.screenshot()
        img = img.resize((174, 143), keepaspect=1)
        img.save(u"E:\\screenshot.jpg")
    elif keyboard.pressed(EScancodeStar):
        c.clear()
    elif keyboard.pressed(EScancode0):
        call_me_bob_ross += 1