×
Namespaces

Variants
Actions
Revision as of 08:24, 17 July 2012 by hamishwillee (Talk | contribs)

Archived:Getting started with game programming on PySymbian

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: anees042 (21 Oct 2008)
Last edited: hamishwillee (17 Jul 2012)

This example shows how to do the basic key handling, how to draw graphics to screen,, how to move a graphic with the help of keypad, and how to clear the screen.


Code

# __author__='Fayyaz Ali'
# version = '1.0 beta'
# filename='GameSample.py'
# license='GNU GPL'
 
# import the required modules
import e32, appuifw, key_codes, graphics
 
# define the exit function
def quit():app_lock.signal()
appuifw.app.exit_key_handler=quit
 
# make the screen large
appuifw.app.screen='large'
 
global x,y
x = 110
y = 160
 
# this method is for handling the redraw event of canvas
def handle_redraw(rect):
canvas.blit(img)
 
# this method is called when the ok button is pressed
def press_select():
appuifw.note(u'hit','info')
 
# this method is called when the right button is pressed
def press_right():
global x
x+=5
update()
 
# this method is called when the left button is pressed
def press_left():
global x
x-=5
update()
 
# this method is called when the up button is pressed
def press_up():
global y
y-=5
update()
 
# this method is called when the down button is pressed
def press_down():
global y
y+=5
update()
 
def update():
# clear img
img.clear()
# open an image
img1=graphics.Image.open("e:\\Images\\area.png")
# resize the image
img2 = img1.resize((20,20))
# copy the img1 to img
img.blit(img2, (0,0), (x, y))
handle_redraw(None)
 
# create an empty image according to the resolution of the phone
img=graphics.Image.new((240,320))
 
# create an object of the Canvas
canvas=appuifw.Canvas(redraw_callback=handle_redraw)
 
# bind the canvas with keys, so that it can listen to
# key presses of those keys and do some actions on each key
 
# we are passing two parameters to the bind method of canvas class.
# The first is the key code to which we want our application to
# listen to events. And the second is handler for the event.
canvas.bind(key_codes.EKeySelect, press_select)
canvas.bind(key_codes.EKeyDownArrow, press_down)
canvas.bind(key_codes.EKeyUpArrow, press_up)
canvas.bind(key_codes.EKeyRightArrow, press_right)
canvas.bind(key_codes.EKeyLeftArrow, press_left)
 
# set the application body to canvas
appuifw.app.body=canvas
 
update()
 
app_lock=e32.Ao_lock()
app_lock.wait()
27 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×