Excercise: Graphics drawing on full canvas

• Program an application that lets you move a colour point on the full sized canvas,
by using the phones navigation keys: up, down, left, right.

1. Use e.g. the script from the "extended use of keys" exercise and add few things:

from graphics import * since we want to use the graphics functionalities

We need to define the size and starting position of the colour point:

blobsize=30
location_x = 100.
location_y = 100.

img.point((location_x + blobsize/2,location_y + blobsize/2),0xff0000,width=blobsize)

The application needs to constantly redraw the whole canvas in order to update the new position of the colour point. For this we create a functioin call handle_redraw and use canvas.blit(...):

def handle_redraw(rect):
    canvas.blit(img)

In the Canvas function we add the handle_redraw function as the redraw_callback:

canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=handle_redraw)

In the "running" loop we need to add:

img.clear(0x0000ff) so that the entire canvas it redrawn (resets the entire surface of the drawable to a given colour)

 

note: also you need to change keyboard.pressed(...) to keyboard.is_down(...) in the "running" loop, so that not only a single hit of a key gets recognized but the continous pressing of the key.

 

Example file: ex_graphics_drawing.py

Description: ex_graphics_drawing_descr.py

 

For further info what other graphics and text beside a point can be drawn check out the app_body_canvas.py script or the API_Reference_for_Python.pdf

 

 

If you are bored:

Add in the current exercise script some limitations to the movements of the point, so it can not go outside the visible canvas.

You can also check out Nokia's ball.py example and modify it.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Previous | Next | Menu     Copyright (c) 2006 Jurgen Scheible