
Originally Posted by
Rafael T.
I was reading a PDF about Python and it talked about event based code. It explains what it is but I couldn't understand.
It says:
Code:
Event Loop
● e32.Ao_lock waits for events, but stops execution
● Game:
App. has to be active all the time
But still needs respond to events (keys, ...)
Initialize event call-backs
while <end condition>:
Update game state
Redraw screen
Pause / yield
Sleep(+ execute waiting active objects):
e32.ao_sleep(interval)
Yield(just execute ready active objects with
higher priority)
e32.ao_yield()
Could someone give me a clear explanation about this? I was also talking to JOM via email and he told me event based applications could be better, faster and more improved on system response. Unfortunaly, he is now travelling and I couldn't ask him about this. Can somebody explain me? What's the difference between a event based and a non-event bases application?
Sorry for asking too much

Hello Rafael T.!
Look at the link over here.
Getting Started with Game Programming
The above example is based on the event based code.
In the above code/example which i mentioned, i have used these two lines;
Code:
app_lock=e32.Ao_lock()
app_lock.wait()
These two lines makes the application listen for events. we can also call it the main method of the game or application. if you test this example and remove these two lines, the application will appear and then disappear.
so the point is these two lines makes the application run forever now how to stop it.
then when we want to stop it, we send it a signal, you can see it in my code in quit() method;
this was all about event based code
now if you wants to see a non event based code, here it is;
Note: i have removed the above mentioned lines.
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():running=0
appuifw.app.exit_key_handler=quit
# make the screen large
appuifw.app.screen='large'
global x,y, running
x = 110
y = 160
running = 1
# 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=x+5
update()
# this method is called when the left button is pressed
def press_left():
global x
x=x-5
update()
# this method is called when the up button is pressed
def press_up():
global y
y=y-5
update()
# this method is called when the down button is pressed
def press_down():
global y
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
while running:
update()
#app_lock=e32.Ao_lock()
#app_lock.wait()
i have just added a new global variable "running", you can see it here;
Code:
global x,y, running
changed the quit method to;
Code:
def quit():running=0
and changed the last three lines to;
Code:
while running:
update()
#app_lock=e32.Ao_lock()
#app_lock.wait()