# Code is based on the implementation of ball.py from Nokia Corporation # Below you can find the Licence aggrement of the original License. # The author does not want to retain any rights on this software and # the program is provided AS-IS, for modifications, distributions and any # other activities. # Author: Christos Scordellis # Date: 17 July 2008 # Version: 0.0.1 (Beta) # Copyright (c) 2005 Nokia Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import appuifw from graphics import * import e32 from key_codes 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 e32.Ao_lock().signal() appuifw.app.exit_key_handler=quit location=[img.size[0]/2,img.size[1]/2] player1_location=[img.size[0]/2,img.size[1]/2] player2_location=[img.size[0]/2,img.size[1]/2] original_speed=1.6 speed=[original_speed,original_speed] blobsize=12 xs,ys=img.size[0]-blobsize,img.size[1]-blobsize gravity=0.00 acceleration=0.05 friction=1 racket_speed=2.5 green_colour=0x00ff00 red_colour= 0xff0000 ball_colour=green_colour racket_width=4 racket_height=60 player1=0 player2=0 player1_racket =0 vertical_speed=0 ball_counter=0 # 0.999 def change_colour(): global ball_colour ball_counter=0 ball_colour=red_colour return if ball_colour == 0x00ff00: ball_colour=0xffffff else: ball_colour=0x00ff00 import time start_time=time.clock() n_frames=0 # To speed things up, we prerender the text. labeltext=u'Use arrows to move ball' textrect=img.measure_text(labeltext, font='normal')[0] text_img=Image.new((textrect[2]-textrect[0],textrect[3]-textrect[1])) text_img.clear(0) text_img.text((-textrect[0],-textrect[1]),labeltext,fill=0xffffff,font='normal') scorechange = True fluffy = False while running: img.clear(0) # if (n_frames % 30) == 0: if scorechange: if speed[0]-abs(speed[0]) == 0: speed[0]=original_speed else: speed[0]=-original_speed if speed[1]-abs(speed[1]) == 0: speed[1]=original_speed else: speed[1]=-original_speed scorechange = False labeltext=u'Score: %d - %d' % (player1,player2) if ((player1 + player2) % 5) == 0: original_speed+=0.1 textrect=img.measure_text(labeltext, font='normal')[0] text_img=Image.new((textrect[2]-textrect[0],textrect[3]-textrect[1])) text_img.clear(0) text_img.text((-textrect[0],-textrect[1]),labeltext,fill=0xffffff,font='normal') img.blit(text_img, (0,0)) # player1 racket img.rectangle((2,player1_location[1]-racket_height/2,racket_width+2,player1_location[1]+racket_height/2),0xffffff,fill=(255,255,255)) img.point((2,player1_location[1]),0xff0000,width=6) # player2 racker img.rectangle((img.size[0]-racket_width,player2_location[1]-(racket_height/2),img.size[0]-1,player2_location[1]+racket_height/2),0xffffff,fill=(255,255,255)) img.point((img.size[0]-racket_width,player2_location[1]),0x0000ff,width=6) # court lines img.rectangle((xs/2,ys/2-30,xs/2+4,ys/2+30),0xffffff,fill=(255,255,255)) img.rectangle((xs/2,10,xs/2+4,30),0xffffff,fill=(255,255,255)) img.rectangle((xs/2,ys-30,xs/2+4,ys-10),0xffffff,fill=(255,255,255)) # ball if fluffy: img.point((location[0]+blobsize,location[1]+blobsize),red_colour,width=blobsize*3) img.point((location[0]+blobsize,location[1]+blobsize),ball_colour,width=blobsize) handle_redraw(()) if ball_counter > 10: ball_colour=green_colour e32.ao_yield() vertical_speed=0 location[0]+=speed[0] location[1]+=speed[1] # player 1 hit if (location[0] < racket_width) and (speed[0]<=0) and (abs((player1_location[1]-(blobsize) - location[1])) <= (racket_height/2)): ball_colour=red_colour ball_counter=0 speed[0]=-speed[0] vertical_speed=location[1] - player1_location[1] location[0]+=speed[0] # Corner shot if (vertical_speed > racket_height/4) and (speed[1] < 0): # and (vertical_speed < racket_height/2): speed[1]=-speed[1] # speed[1]*=1.4 elif (vertical_speed < -racket_height/4) and (speed[1] > 0): # and (vertical_speed < -racket_height/2): speed[1]=-speed[1] # speed[1]*=1.4 elif speed[1] < 0.2: pass # speed[1] = 1.3 else: pass # speed[1]*=1.1 # left wall hit elif location[0] < -1: location[0]=0 # location[0]=-location[0] vertical_speed=0 speed[0]=-speed[0] player2+=1 scorechange= True # player 2 hit elif (location[0] >= (xs-racket_width) -blobsize*0.8) and (speed[0]>0) and (abs(player2_location[1] - location[1] + blobsize/2) < (racket_height/2)): speed[0]=-speed[0] location[0]=(xs-racket_width)-2 ball_colour=red_colour ball_counter=0 vertical_speed=location[1] - player2_location[1] # Corner shot if (vertical_speed > racket_height/4) and (speed[1] < 0): # and (vertical_speed < racket_height/2): speed[1]=-speed[1] # speed[1]*=1.4 elif (vertical_speed < -racket_height/4) and (speed[1] > 0): # and (vertical_speed < -racket_height/2): speed[1]=-speed[1] # speed[1]*=1.4 elif speed[1] < 0.2: pass # speed[1] = 1.3 else: pass # speed[1]*=1.1 elif location[0]>img.size[0]: location[0]=xs-(location[0]-xs) speed[0]=-speed[0] player1+=1 scorechange= True # upper wall hit if location[1] < 0: location[1]= -location[1] speed[1]=-speed[1] # lower wall hit if location[1] > ys: location[1]=ys-(location[1]-ys) speed[1]=-speed[1] if keyboard.is_down(EScancodeDownArrow) and player1_location[1] < ys: player1_racket = racket_speed if keyboard.is_down(EScancodeUpArrow)and player1_location[1] > 0: player1_racket = -racket_speed if keyboard.pressed(EScancodeHash): speed[0]*=0.88 if keyboard.pressed(EScancodeStar): speed[0]*=1.2 if keyboard.pressed(EScancode8): if fluffy: fluffy = False else: fluffy = True if keyboard.pressed(EScancode5): quit() player1_location[1] += player1_racket player1_racket*=0.92 if (location[1] - player2_location[1]) > 10 : player2_location[1] += racket_speed*0.5 elif (location[1] - player2_location[1]) < 10 : player2_location[1] -= racket_speed*0.5 if abs(speed[1]) <= 3 : speed[0]*=1.0005 if ((n_frames % 1000) == 0): speed[0]*=1.1 ball_counter+=1 n_frames+=1 end_time=time.clock() total=end_time-start_time