Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### bountyhunter.py
- from graphics import *
- from sensor import *
- import e32,audio,appuifw,os,sys,key_codes,time,math,random
- max_x=500 ### <<< ref
- max_y=360 ### <<< ref
- canvasImage=None
- xm,ym=max_x,max_y
- def handle_redraw(rect):
- if canvasImage: ### Skips only at first pass as "None"
- canvas.blit(canvasImage)
- COLOR_BLACK=(0,0,0)
- COLOR_WHITE=(255,255,255)
- COLOR_GRAYLIGHT=(211,211,211)
- COLOR_GRAY=(128,128,128)
- COLOR_GRAYDARK=(169,169,169)
- COLOR_RED=(255,0,0)
- COLOR_ORANGE=(255,165,0)
- COLOR_YELLOW=(255,255,0)
- COLOR_GREEN=(0,128,0)
- COLOR_BLUE=(0,0,255)
- COLOR_CYAN=(0,255,255)
- COLOR_PURPLE=(128,0,128)
- COLOR_DARKBLUE=(0,0,139)
- COLOR_DARKGREEN=(0,100,0)
- COLOR_DEEPPINK=(255,20,147)
- COLOR_INDIGO=(75,0,130)
- COLOR_LIGHTBLUE=(173,216,230)
- COLOR_LIME=(0,255,0)
- COLOR_OLIVE=(107,142,35)
- COLOR_BROWN=(139,69,19)
- COLOR_GOLD=(255,215,0)
- COLOR_SILVER=(192,192,192)
- COLOR_MIDGREEN=(0,204,0) # ***
- appuifw.app.orientation='landscape'
- appuifw.app.screen='large'
- appuifw.app.directional_pad=False
- loadingStatusLine=1
- canvas=appuifw.Canvas(redraw_callback=handle_redraw)
- appuifw.app.body=canvas
- canvasImage=Image.new(canvas.size)
- def quit():
- global running
- running=0
- appuifw.app.exit_key_handler=quit
- canvasImage.clear(COLOR_BLACK)
- handle_redraw(())
- obj=20
- x2,y2=xm/2-obj/2,ym/2-obj/2
- rec_x=1
- rec_y=1
- x_incr=0
- y_incr=0
- boundary=max_x
- class SensorConnection():
- xAxis=0
- yAxis=0
- def __init__(self):
- """Connect to the sensor."""
- self.accelerometer=AccelerometerXYZAxisData(data_filter=LowPassFilter())
- self.accelerometer.set_callback(data_callback=self.sense_conn)
- def sense_conn(self):
- self.xAxis=self.accelerometer.x*-1
- self.yAxis=self.accelerometer.y*-1
- def run(self):
- self.accelerometer.start_listening()
- def cleanup(self):
- """Cleanup after yourself. *Must be called* before exiting."""
- self.accelerometer.stop_listening()
- running=1
- accl=SensorConnection()
- accl.run()
- '''
- def playSoundShoot():
- if sound_gunshot.state() != audio.EPlaying:
- sound_gunshot.play()
- def statusupdate(im,status,progress):
- global loadingStatusLine
- im.text((10,20),u"Initialising..."+progress,fill=COLOR_BLACK)
- im.text((10,50+loadingStatusLine*20),u" > "+status,fill=COLOR_BLACK)
- loadingStatusLine+=1
- handle_redraw(())
- e32.ao_yield()
- '''
- def doGoalAchieved():
- global goalsAchieved,statusImage,timeLeft
- goalsAchieved+=5000
- statusImage=createStatusBox(goalsAchieved,timeLeft)
- ###ZZZ <<< playSoundShoot()
- def reset():
- global xx,yy,goalsAchieved,timeLeft,timePassed,timeLimit,startTime,redx,redy
- random.seed(123456789) # example sequencing
- redx=random.randrange(0,(xm-200)-obj) # to place a min distance of x 100 pixels away from the center
- if redx>(xm-obj)/2-100:
- redx+=200
- redy=random.randrange(0,(ym-200)-obj) # to place a min distance of y 100 pixels away from the center
- if redy>(xm-obj)/2-100:
- redy+=200
- timePassed=0
- timeLeft=timeLimit
- startTime=time.time()
- goalsAchieved=0
- xx=100
- yy=100
- def createStatusBox(count,timeleft):
- statImg=Image.new((180,60),'RGB16')
- statImg.clear(COLOR_ORANGE)
- statImg.rectangle((0,0,180,60),width=4,outline=COLOR_BLACK)
- ### initial halfway markings both minus zero
- statImg.text((10,25),u"Goals: "+unicode(count),font='dense',fill=COLOR_BLACK)
- statImg.text((10,48),u"Time Left: "+unicode(timeleft)+" s",font='dense',fill=COLOR_BLACK)
- return statImg ### >>> statImg.transpose(ROTATE_90)
- # determine python root (where the script is located)
- # this has to be c:\\...python... or e:\\...python...
- if(os.path.exists("C:\\data\\Python\\bountyhunter.py")):
- PYTHON_ROOT=u"C:\\data\\Python\\bountyhunter\\"
- elif(os.path.exists("E:\\data\\Python\\bountyhunter.py")):
- PYTHON_ROOT=u"E:\\data\\Python\\bountyhunter\\"
- else:
- appuifw.query(u"couldn't determine python source directory" ,"query") # interupt to debug code
- startTime=time.time()
- prevTime=startTime
- totalTime=0
- timePassed=0
- timeLimit=60
- timeLeft=timeLimit
- '''
- if(os.path.exists(PYTHON_ROOT+u"gunshot.wav")):
- statusupdate(canvasImage,"loading sounds","......")
- sound_gunshot=audio.Sound.open(PYTHON_ROOT+u"gunshot.wav")
- else:
- appuifw.query(u"missing audio file","query")
- '''
- dir_x=u"e:\\data\\Python\\bntyhntr.txt"
- if not os.path.isfile(dir_x):
- f=open(dir_x,'w')
- f.write('0')
- f.close
- f=open(dir_x,'r')
- db_x=f.readline()
- f.close
- hscor_db=db_x
- print hscor_db
- hscor_db=int(hscor_db)
- xx=100
- yy=100
- goalsAchieved=0
- random.seed(123456789) # example sequencing as for fairness
- redx=random.randrange(0,(xm-200)-obj) # to place a min distance of x 100 pixels away from the center
- if redx>(xm-obj)/2-100:
- redx+=200
- redy=random.randrange(0,(ym-200)-obj) # to place a min distance of y 100 pixels away from the center
- if redy>(xm-obj)/2-100:
- redy+=200
- gameRunning=0
- res=appuifw.query(u"Start by pressing the -OK- button","query")
- if(res == 1):gameRunning=1
- startTime=time.time()
- statusImage=createStatusBox(goalsAchieved,timeLeft)
- # Function which draws circle with given radius at given co-ordinate
- def circle(x,y,radius=10,outline=0,fill=COLOR_GRAYLIGHT,width=1):
- canvasImage.ellipse((x-radius,y-radius,x+radius,y+radius),outline,fill,width)
- goalrange=2500.0
- byGoal=goalrange/abs((xm/2-obj/2)+abs(ym/2-obj/2))
- while(gameRunning):
- while running:
- if(time.time()-startTime-timePassed>1):# over one second passed
- timePassed=timePassed+1
- timeLeft=timeLimit-timePassed
- goalsAchieved+=int(goalrange-(byGoal*(abs((xm/2-xx)-obj/2)+abs((ym/2-yy)-obj/2))))
- statusImage=createStatusBox(goalsAchieved,timeLeft)
- if timeLeft %25 == 0:
- e32.reset_inactivity()
- if(timeLeft<=0):
- running=0
- y=accl.xAxis*-1 # -1 switches motion orientation
- x=accl.yAxis*-1
- x_incr=x*2
- xx=xx+x_incr
- y_incr=y*2
- yy=yy+y_incr
- if xx+obj>x2 and xx<x2+obj:
- if yy+obj>y2 and yy<y2+obj:
- xx=redx
- redx=random.randrange(0,(xm-200)-obj) # to place a min distance of x 100 pixels away from the center
- if redx>(xm-obj)/2-100:
- redx+=200
- yy=redy
- redy=random.randrange(0,(ym-200)-obj) # to place a min distance of y 100 pixels away from the center
- if redy>(xm-obj)/2-100:
- redy+=200
- doGoalAchieved()
- if xx<xm/2-boundary/2:
- xx=xm/2-boundary/2
- elif xx>xm/2+boundary/2-obj:
- xx=xm/2+boundary/2-obj
- if yy<ym/2-max_y/2:
- yy=ym/2-max_y/2
- elif yy>ym/2+max_y/2-obj:
- yy=ym/2+max_y/2-obj
- # initial halfway markings both minus zero
- canvasImage=Image.new((xm,ym),'RGB16')
- canvasImage.rectangle((redx,redy,redx+obj,redy+obj),outline=COLOR_RED,fill=COLOR_RED)
- canvasImage.rectangle((x2,y2,x2+obj,y2+obj),outline=COLOR_MIDGREEN,fill=COLOR_MIDGREEN)
- canvasImage.rectangle((xx,yy,xx+obj,yy+obj),outline=COLOR_BLUE,fill=COLOR_BLUE)
- canvasImage.text((374,30),unicode(hscor_db),font='title',fill=COLOR_GRAYDARK)
- canvasImage.blit(statusImage,target=(0,canvasImage.size[1]-statusImage.size[1]))
- handle_redraw(())
- e32.ao_yield() # bottom of _running_loop_
- if (goalsAchieved>hscor_db):
- appuifw.query(u"Congratulations !!!\nYou Have Set A New Rank!" ,"query")
- msg=(u'New Highscore: %s\nPrev Highscore: %s\n\n Play again?"' %(goalsAchieved,hscor_db))
- hscor_db=goalsAchieved
- f=open(dir_x,'w')
- f.write('%s' %(hscor_db))
- f.close()
- print hscor_db
- else:
- msg=(u"TIME'S UP! \nYou have scored " + unicode(goalsAchieved) + " given " +
- unicode(timeLimit) + " seconds \n\n Play again?")
- res=appuifw.query(msg,"query")
- if(res != 1):
- gameRunning=0
- print "ended"
- else:
- running=1
- reset()
- # sound_gunshot.close()
- appuifw.app.body=None
- accl.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement