# MovingBallWithKeysS60.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-11-01 File created. # 2006-11-01 Last modification. import appuifw # importing the Application User Interface Framework import e32 # importing utilities related to Symbian OS import key_codes e32.ao_yield() class MovingBall : def __init__( self ) : self.lock = e32.Ao_lock() self.old_title = appuifw.app.title appuifw.app.title = u"MOVE THE BALL WITH KEYS" self.exit_command_given = False appuifw.app.exit_key_handler = self.handle_exit_key # If self.draw_canvas_contents is set as the redraw_callback function here, # it will be called before this __init__() method reaches its end. # There should be a mechanism for blocking the execution of other methods # before this method is executed. self.application_canvas = appuifw.Canvas( redraw_callback = None ) #self.draw_canvas_contents ) appuifw.app.body = self.application_canvas self.application_canvas.bind( key_codes.EKeyLeftArrow, self.arrow_left_pressed ) self.application_canvas.bind( key_codes.EKeyUpArrow, self.arrow_up_pressed ) self.application_canvas.bind( key_codes.EKeyDownArrow, self.arrow_down_pressed ) self.application_canvas.bind( key_codes.EKeyRightArrow, self.arrow_right_pressed ) self.rgb_color_specification = 0x00FF0000 # red canvas_width, canvas_height = self.application_canvas.size self.ball_position_x = canvas_width / 2 self.ball_position_y = canvas_height / 2 def arrow_left_pressed( self ) : self.ball_position_x = self.ball_position_x - 3 self.lock.signal() def arrow_up_pressed( self ) : self.ball_position_y = self.ball_position_y - 3 self.lock.signal() def arrow_down_pressed( self ) : self.ball_position_y = self.ball_position_y + 3 self.lock.signal() def arrow_right_pressed( self ) : self.ball_position_x = self.ball_position_x + 3 self.lock.signal() def draw_canvas_contents( self, rect ) : # clear screen to black self.application_canvas.clear(0) # Write the current ball coordinates to the upper left corner. self.application_canvas.text(( 10, 10), u"(%d,%d)" % ( self.ball_position_x, self.ball_position_y ), fill=0xffffff) self.application_canvas.point( ( self.ball_position_x, self.ball_position_y ), 0x00FF0000, width=40 ) print self.ball_position_x print self.ball_position_y # self.application_canvas.ellipse( (80, 80, 80, 80) ) def run( self ) : try: self.lock.wait() while not self.exit_command_given : self.draw_canvas_contents( None ) self.lock.wait() finally: pass def close( self ) : # this method is probably not needed. appuifw.app.menu = [] appuifw.app.body = None appuifw.app.exit_key_handler = None appuifw.app.title = self.old_title def handle_exit_key( self ) : self.exit_command_given = True self.lock.signal() if __name__ == "__main__": this_application = MovingBall() this_application.run()