Pong code

From MoDe


# pong game
# 2 paddles
# 1 ball
# 1 winner
# keys 3 and 9 control right paddle
# keys 1 and 7 control left paddle
# keys 1 and 3 move their respective paddles up
# keys 7 and 9 move their respective paddles down

# things to improve
#  - play sound when ball hits paddle
#  - increase ball speed regularly
#  - bluetooth capabilities to allow multi players across phones

import appuifw
from graphics import *
import e32
from key_codes import *
from music import *

class Keyboard(object):
   def __init__(self,onevent=lambda:None):
       self._keyboard_state={}
       self._downs={}
       self._onevent=onevent
   def handle_event(self,event):
       if event['type'] == appuifw.EEventKeyDown:
           code=event['scancode']
           if not self.is_down(code):
               self._downs[code]=self._downs.get(code,0)+1
           self._keyboard_state[code]=1
       elif event['type'] == appuifw.EEventKeyUp:
           self._keyboard_state[event['scancode']]=0
       self._onevent()
   def is_down(self,scancode):
       return self._keyboard_state.get(scancode,0)
   def pressed(self,scancode):
       if self._downs.get(scancode,0):
           self._downs[scancode]-=1
           return True
       return False
keyboard=Keyboard()

appuifw.app.screen='full'
img=None
def handle_redraw(rect):
   if img:
       canvas.blit(img)
appuifw.app.body=canvas=appuifw.Canvas(
   event_callback=keyboard.handle_event,
   redraw_callback=handle_redraw)
img=Image.new(canvas.size)

running=1
def quit():
   global running
   running=0
appuifw.app.exit_key_handler=quit

numsteps = 35
paddlelength=5
ystep=img.size[1]/(numsteps+paddlelength)
xstep=(img.size[0]-2*ystep)/numsteps

location=[img.size[0]/2, img.size[1]/2]
speed=[1,1]
blobsize=10
tone_duration=25
volume = 1
max_volume = loudest()
base_freq = 440
xs,ys=img.size[0]-blobsize,img.size[1]-blobsize
gravity=0.03
acceleration=0.05
import time
start_time=time.clock()
n_frames=0
position = [8,8]

niter=0

while running :

   winner = -1
   location=[img.size[0]/2, img.size[1]/2]

   while running:
       img.clear(0)
       img.text((40,70),u'Use 1,3,7,9 to play',0xffffff)
       img.rectangle((0, ystep*(position[0]), ystep-1, ystep*(position[0]+paddlelength-1)), 0xbbffff)
       img.rectangle((img.size[0]-ystep, ystep*(position[1]), img.size[0]-1, ystep*(position[1]+paddlelength-1)), 0xffbbff)
       img.point((location[0],location[1]),
                 0x00ff00,width=blobsize)
       handle_redraw(())
       e32.ao_yield()

       niter = niter + 1
       if niter%2 == 0 :

           location[0] += speed[0]
           location[1] += speed [1]

           # is the ball on the left side?
           if location [0] == blobsize/2 + xstep :
               if abs (location [1] - ystep*(position[0]+paddlelength/2)) < ystep * (paddlelength/2 + 1):
                   speed [0] = 1

           # is the ball on the right side?
           if location [0] == img.size [0] - blobsize/2 - xstep :
               if abs (location [1] - ystep*(position[1]+paddlelength/2)) < ystep * (paddlelength/2 + 1):
                   speed [0] = -1

           # is the ball on the top?
           if location [1] == blobsize/2 :
               speed [1] = 1

           # is the ball on the bottom?
           if location [1] == img.size[1]-blobsize+1 :
               speed [1] = -1

           if location[0] == 0 :
               running = 0
               winner = 1

           if location[0] == img.size[0] :
               running = 0
               winner = 2


       if niter % 2 == 0 :
           if keyboard.is_down(EKey1): position[0] -= 1
           if keyboard.is_down(EKey7): position[0] += 1
           if keyboard.is_down(EKey3): position[1] -= 1
           if keyboard.is_down(EKey9): position[1] += 1

           if position[0] == -1 : position [0] = 0
           if position[0] == numsteps+1 : position [0] = numsteps

           if position[1] == -1 : position [1] = 0
           if position[1] == numsteps+1 : position [1] = numsteps


       n_frames+=1

   if winner == 1 :
       if appuifw.query(u"Right won! Play again?","query") == True :
           running = 1

   if winner == 2 :
       if appuifw.query(u"Left won! Play again?","query") == True :
           running = 1





end_time=time.clock()
total=end_time-start_time

print "%d frames, %f seconds, %f FPS, %f ms/frame."%(n_frames,total,
                                                    n_frames/total,
                                                    total/n_frames*1000.)