Advertisement
here2share

### bountyhunter.py

Nov 29th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### bountyhunter.py
  2.  
  3. from graphics import *
  4. from sensor import *
  5. import e32,audio,appuifw,os,sys,key_codes,time,math,random
  6.  
  7. max_x=500 ### <<< ref
  8. max_y=360 ### <<< ref
  9. canvasImage=None
  10. xm,ym=max_x,max_y
  11.  
  12. def handle_redraw(rect):
  13.     if canvasImage: ### Skips only at first pass as "None"
  14.         canvas.blit(canvasImage)
  15.  
  16. COLOR_BLACK=(0,0,0)
  17. COLOR_WHITE=(255,255,255)
  18. COLOR_GRAYLIGHT=(211,211,211)
  19. COLOR_GRAY=(128,128,128)
  20. COLOR_GRAYDARK=(169,169,169)
  21. COLOR_RED=(255,0,0)
  22. COLOR_ORANGE=(255,165,0)
  23. COLOR_YELLOW=(255,255,0)
  24. COLOR_GREEN=(0,128,0)
  25. COLOR_BLUE=(0,0,255)
  26. COLOR_CYAN=(0,255,255)
  27. COLOR_PURPLE=(128,0,128)
  28.  
  29. COLOR_DARKBLUE=(0,0,139)
  30. COLOR_DARKGREEN=(0,100,0)
  31. COLOR_DEEPPINK=(255,20,147)
  32. COLOR_INDIGO=(75,0,130)
  33. COLOR_LIGHTBLUE=(173,216,230)
  34. COLOR_LIME=(0,255,0)
  35. COLOR_OLIVE=(107,142,35)
  36.  
  37. COLOR_BROWN=(139,69,19)
  38. COLOR_GOLD=(255,215,0)
  39. COLOR_SILVER=(192,192,192)
  40.  
  41. COLOR_MIDGREEN=(0,204,0) # ***
  42.  
  43. appuifw.app.orientation='landscape'
  44. appuifw.app.screen='large'
  45. appuifw.app.directional_pad=False
  46. loadingStatusLine=1
  47. canvas=appuifw.Canvas(redraw_callback=handle_redraw)
  48. appuifw.app.body=canvas
  49. canvasImage=Image.new(canvas.size)
  50.  
  51. def quit():
  52.     global running
  53.     running=0
  54. appuifw.app.exit_key_handler=quit
  55. canvasImage.clear(COLOR_BLACK)
  56. handle_redraw(())
  57.  
  58. obj=20
  59. x2,y2=xm/2-obj/2,ym/2-obj/2
  60. rec_x=1
  61. rec_y=1
  62. x_incr=0
  63. y_incr=0
  64.  
  65. boundary=max_x
  66.  
  67. class SensorConnection():
  68.     xAxis=0
  69.     yAxis=0
  70.     def __init__(self):
  71.         """Connect to the sensor."""
  72.         self.accelerometer=AccelerometerXYZAxisData(data_filter=LowPassFilter())
  73.         self.accelerometer.set_callback(data_callback=self.sense_conn)
  74.  
  75.     def sense_conn(self):
  76.         self.xAxis=self.accelerometer.x*-1
  77.         self.yAxis=self.accelerometer.y*-1
  78.  
  79.     def run(self):
  80.         self.accelerometer.start_listening()
  81.  
  82.     def cleanup(self):
  83.         """Cleanup after yourself. *Must be called* before exiting."""
  84.         self.accelerometer.stop_listening()
  85.  
  86. running=1
  87. accl=SensorConnection()
  88. accl.run()
  89.  
  90. '''
  91. def playSoundShoot():
  92.     if sound_gunshot.state() != audio.EPlaying:
  93.         sound_gunshot.play()
  94. def statusupdate(im,status,progress):
  95.     global loadingStatusLine
  96.     im.text((10,20),u"Initialising..."+progress,fill=COLOR_BLACK)
  97.     im.text((10,50+loadingStatusLine*20),u" > "+status,fill=COLOR_BLACK)
  98.     loadingStatusLine+=1
  99.     handle_redraw(())
  100.     e32.ao_yield()
  101. '''
  102.  
  103. def doGoalAchieved():
  104.     global goalsAchieved,statusImage,timeLeft
  105.     goalsAchieved+=5000
  106.     statusImage=createStatusBox(goalsAchieved,timeLeft)
  107.     ###ZZZ <<< playSoundShoot()
  108.  
  109. def reset():
  110.     global xx,yy,goalsAchieved,timeLeft,timePassed,timeLimit,startTime,redx,redy
  111.     random.seed(123456789) # example sequencing
  112.     redx=random.randrange(0,(xm-200)-obj) # to place a min distance of x 100 pixels away from the center
  113.     if redx>(xm-obj)/2-100:
  114.         redx+=200
  115.     redy=random.randrange(0,(ym-200)-obj) # to place a min distance of y 100 pixels away from the center
  116.     if redy>(xm-obj)/2-100:
  117.         redy+=200
  118.     timePassed=0
  119.     timeLeft=timeLimit
  120.     startTime=time.time()
  121.     goalsAchieved=0
  122.     xx=100
  123.     yy=100
  124.  
  125. def createStatusBox(count,timeleft):
  126.     statImg=Image.new((180,60),'RGB16')
  127.     statImg.clear(COLOR_ORANGE)
  128.     statImg.rectangle((0,0,180,60),width=4,outline=COLOR_BLACK)
  129.     ### initial halfway markings both minus zero
  130.     statImg.text((10,25),u"Goals: "+unicode(count),font='dense',fill=COLOR_BLACK)
  131.     statImg.text((10,48),u"Time Left: "+unicode(timeleft)+" s",font='dense',fill=COLOR_BLACK)
  132.  
  133.     return statImg ### >>> statImg.transpose(ROTATE_90)
  134.  
  135. # determine python root (where the script is located)
  136. # this has to be c:\\...python... or e:\\...python...
  137. if(os.path.exists("C:\\data\\Python\\bountyhunter.py")):
  138.     PYTHON_ROOT=u"C:\\data\\Python\\bountyhunter\\"
  139. elif(os.path.exists("E:\\data\\Python\\bountyhunter.py")):
  140.     PYTHON_ROOT=u"E:\\data\\Python\\bountyhunter\\"
  141. else:
  142.     appuifw.query(u"couldn't determine python source directory" ,"query") # interupt to debug code
  143.  
  144. startTime=time.time()
  145. prevTime=startTime
  146. totalTime=0
  147. timePassed=0
  148. timeLimit=60
  149. timeLeft=timeLimit
  150.  
  151. '''
  152. if(os.path.exists(PYTHON_ROOT+u"gunshot.wav")):
  153.     statusupdate(canvasImage,"loading sounds","......")
  154.     sound_gunshot=audio.Sound.open(PYTHON_ROOT+u"gunshot.wav")
  155. else:
  156.     appuifw.query(u"missing audio file","query")
  157. '''
  158. dir_x=u"e:\\data\\Python\\bntyhntr.txt"
  159. if not os.path.isfile(dir_x):
  160.     f=open(dir_x,'w')
  161.     f.write('0')
  162.     f.close
  163.    
  164. f=open(dir_x,'r')
  165. db_x=f.readline()
  166. f.close    
  167.  
  168. hscor_db=db_x
  169. print hscor_db
  170.  
  171. hscor_db=int(hscor_db)
  172.  
  173. xx=100
  174. yy=100
  175. goalsAchieved=0
  176. random.seed(123456789) # example sequencing as for fairness
  177. redx=random.randrange(0,(xm-200)-obj) # to place a min distance of x 100 pixels away from the center
  178. if redx>(xm-obj)/2-100:
  179.     redx+=200
  180. redy=random.randrange(0,(ym-200)-obj) # to place a min distance of y 100 pixels away from the center
  181. if redy>(xm-obj)/2-100:
  182.         redy+=200
  183.    
  184. gameRunning=0
  185. res=appuifw.query(u"Start by pressing the -OK- button","query")
  186. if(res == 1):gameRunning=1
  187. startTime=time.time()
  188. statusImage=createStatusBox(goalsAchieved,timeLeft)
  189.  
  190. # Function which draws circle with given radius at given co-ordinate
  191. def circle(x,y,radius=10,outline=0,fill=COLOR_GRAYLIGHT,width=1):
  192.     canvasImage.ellipse((x-radius,y-radius,x+radius,y+radius),outline,fill,width)
  193. goalrange=2500.0
  194. byGoal=goalrange/abs((xm/2-obj/2)+abs(ym/2-obj/2))
  195. while(gameRunning):
  196.     while running:
  197.         if(time.time()-startTime-timePassed>1):# over one second passed
  198.             timePassed=timePassed+1
  199.             timeLeft=timeLimit-timePassed
  200.             goalsAchieved+=int(goalrange-(byGoal*(abs((xm/2-xx)-obj/2)+abs((ym/2-yy)-obj/2))))
  201.             statusImage=createStatusBox(goalsAchieved,timeLeft)
  202.             if timeLeft %25 == 0:
  203.                 e32.reset_inactivity()
  204.             if(timeLeft<=0):
  205.                 running=0
  206.  
  207.         y=accl.xAxis*-1 # -1 switches motion orientation
  208.         x=accl.yAxis*-1
  209.  
  210.         x_incr=x*2
  211.         xx=xx+x_incr
  212.         y_incr=y*2
  213.         yy=yy+y_incr
  214.  
  215.         if xx+obj>x2 and xx<x2+obj:
  216.             if yy+obj>y2 and yy<y2+obj:
  217.                 xx=redx
  218.                 redx=random.randrange(0,(xm-200)-obj) # to place a min distance of x 100 pixels away from the center
  219.                 if redx>(xm-obj)/2-100:
  220.                     redx+=200
  221.                 yy=redy
  222.                 redy=random.randrange(0,(ym-200)-obj) # to place a min distance of y 100 pixels away from the center
  223.                 if redy>(xm-obj)/2-100:
  224.                     redy+=200
  225.                 doGoalAchieved()
  226.  
  227.         if xx<xm/2-boundary/2:
  228.             xx=xm/2-boundary/2
  229.         elif xx>xm/2+boundary/2-obj:
  230.             xx=xm/2+boundary/2-obj
  231.         if yy<ym/2-max_y/2:
  232.             yy=ym/2-max_y/2
  233.         elif yy>ym/2+max_y/2-obj:
  234.             yy=ym/2+max_y/2-obj
  235.  
  236.         # initial halfway markings both minus zero
  237.         canvasImage=Image.new((xm,ym),'RGB16')
  238.         canvasImage.rectangle((redx,redy,redx+obj,redy+obj),outline=COLOR_RED,fill=COLOR_RED)
  239.         canvasImage.rectangle((x2,y2,x2+obj,y2+obj),outline=COLOR_MIDGREEN,fill=COLOR_MIDGREEN)
  240.         canvasImage.rectangle((xx,yy,xx+obj,yy+obj),outline=COLOR_BLUE,fill=COLOR_BLUE)
  241.         canvasImage.text((374,30),unicode(hscor_db),font='title',fill=COLOR_GRAYDARK)
  242.         canvasImage.blit(statusImage,target=(0,canvasImage.size[1]-statusImage.size[1]))
  243.         handle_redraw(())
  244.  
  245.         e32.ao_yield() # bottom of _running_loop_
  246.  
  247.     if (goalsAchieved>hscor_db):
  248.         appuifw.query(u"Congratulations !!!\nYou Have Set A New Rank!" ,"query")
  249.         msg=(u'New Highscore: %s\nPrev Highscore: %s\n\n Play again?"' %(goalsAchieved,hscor_db))
  250.         hscor_db=goalsAchieved
  251.         f=open(dir_x,'w')
  252.         f.write('%s' %(hscor_db))
  253.         f.close()
  254.         print hscor_db
  255.     else:
  256.         msg=(u"TIME'S UP! \nYou have scored " + unicode(goalsAchieved) + " given " +
  257.                 unicode(timeLimit) + " seconds \n\n Play again?")
  258.  
  259.     res=appuifw.query(msg,"query")
  260.     if(res != 1):
  261.         gameRunning=0
  262.         print "ended"
  263.        
  264.     else:
  265.         running=1
  266.         reset()
  267.  
  268. # sound_gunshot.close()
  269. appuifw.app.body=None
  270. accl.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement