Advertisement
here2share

# py60_gamegui.py + demo ZZZ

Sep 21st, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. # # py60_gamegui.py + demo ZZZ
  4.  
  5. from appuifw import *
  6. import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,copy
  7. from graphics import *
  8. from math import *
  9. from random import *
  10. from key_codes import *
  11.  
  12. def scr(msg):
  13.     txt.set(unicode(msg))
  14.  
  15. z_Colors =  ((u'RGB_BLACK',(0,0,0)),
  16.             (u'RGB_WHITE',(255,255,255)),
  17.             (u'RGB_GRAYLIGHT',(211,211,211)),
  18.             (u'RGB_GRAY',(128,128,128)),
  19.             (u'RGB_GRAYDARK',(169,169,169)),
  20.             (u'RGB_RED',(255,0,0)),
  21.             (u'RGB_ORANGE',(255,165,0)),
  22.             (u'RGB_YELLOW',(255,255,0)),
  23.             (u'RGB_GREEN',(0,128,0)),
  24.             (u'RGB_BLUE',(0,0,255)),
  25.             (u'RGB_CYAN',(0,255,255)),
  26.             (u'RGB_PURPLE',(128,0,255)),
  27.  
  28.             (u'RGB_PINK',(255,148,238)),
  29.             (u'RGB_DARKBLUE',(0,0,139)),
  30.             (u'RGB_DARKGREEN',(0,100,0)),
  31.             (u'RGB_DEEPPINK',(255,20,147)),
  32.             (u'RGB_INDIGO',(75,0,130)),
  33.             (u'RGB_LIGHTBLUE',(173,216,230)),
  34.             (u'RGB_LIME',(0,255,0)),
  35.             (u'RGB_OLIVE',(107,142,35)),
  36.  
  37.             (u'RGB_BROWN',(128,64,0)),
  38.             (u'RGB_GOLD',(255,215,0)),
  39.             (u'RGB_SILVER',(192,192,192)))
  40. #
  41. rgb_menu="rgb_menu=["
  42. for rgb in z_Colors:
  43.     i,ii=rgb[0].replace('RGB_',''),str(rgb[1])
  44.     rgb_menu += "(u'"+i+"',c_"+i+"),"
  45.     exec 'def c_'+i+'(): cv.color='+ii
  46.     exec rgb[0]+'='+ii
  47. rgb_menu += ']'
  48.  
  49. def draw(rect): # >>> draw(())
  50.     if img:
  51.         canvas.blit(img)
  52. def e_callback(event):
  53.     x,y=event['pos']
  54.     try: timer.cancel()
  55.     except: pass
  56.     cv.x=x
  57.     cv.y=y
  58. #
  59. defaultspeed=1200.00
  60. def sp():
  61.     try:
  62.         e32.ao_sleep(speed/sp)
  63.         speed-=0.9
  64.     except: pass
  65.     return defaultspeed-cv.speed
  66. #
  67. def fr_sp(z=0.05): # if glichy or 0.1 for moderate speed
  68.     if time.time() > cv.skip:
  69.         cv.skip=time.time()+z
  70.         return True
  71. #
  72. def status(color=RGB_WHITE):
  73.     img.text((5,Y-40),u'Score:',color)
  74.     img.text((5,Y-20),u'%d'%(P.score),color)
  75.     try:
  76.         img.text((X-100,Y-20),u''+str(round(time_limit-secs,2)),color)
  77.         img.text((X-100,Y-40),u'Time Left:',color)
  78.     except: pass
  79. #
  80. class cv():
  81.     go=1
  82.     init=1
  83.     skip=0
  84.     color=RGB_WHITE
  85.     xyz=(0,0,0)
  86.     xy=0,0
  87.     x=y=0
  88.     light=0
  89.     evt=None
  90.     tmp=0
  91.     sw=0
  92.     jpad=True
  93.     padx,pady=0,0
  94.     mv='X'
  95.     up=-1
  96.     speed=defaultspeed
  97. cv=cv()
  98.  
  99. app.screen = 'large'
  100. lg_scr = 576
  101. app.directional_pad = False
  102. app.orientation='portrait'
  103. img=None
  104. app.body = canvas = Canvas(redraw_callback=draw, event_callback=e_callback) #
  105. x_scr,y_scr=canvas.size
  106. X,Y=canvas.size
  107. img=Image.new((x_scr,y_scr))
  108. def xout(msg=''):
  109.     if query(u''+msg+'\nExit Game?','query'):
  110.         cv.go=0; return 0
  111.     cv.init=0
  112. def xout_btn(attr=0):
  113.     attr=str(attr)
  114.     bg,fg=RGB_RED,RGB_WHITE
  115.     a,b=cv.xy
  116.     w,h=180,90
  117.     L=w
  118.     if '1' in attr: h=y_scr # not recommended
  119.     if 'v' in attr:
  120.         img.rectangle((w,h-90,w+L,h),fill=bg)
  121.         img.text((w,h-10),u'EXIT',fg,font=('dense',98))
  122.     if (w < a < w+L) and (h-90 < b < h):
  123.         xout()
  124.         cv.xy=0,0
  125. def void(): pass
  126. app.exit_key_handler=void
  127. run=True
  128.  
  129. import sensor
  130. def xyzsens():
  131.     try:
  132.         cv.xyz=(sens.x,sens.y,sens.z)
  133.     except: pass
  134. sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
  135. sens.set_callback(data_callback=xyzsens)
  136. sens.start_listening()
  137.  
  138. ### joypad
  139. jxa,jya=70,350 ### <<< jpad xy axis point
  140. jxb,jyb=jxa+220,jya+220
  141. jx1,jy1=jxa+70,jya+70
  142. jx2,jy2=jxa+150,jya+150
  143. def mU(event):
  144.     cv.mv='U'
  145. def mD(event):
  146.     cv.mv='D'
  147. def mL(event):
  148.     cv.mv='L'
  149. def mR(event):
  150.     cv.mv='R'
  151. def bind_jpad():
  152.     canvas.bind(EButton1Down,mU,((jx1,jya),(jx2,jy1)))   # up
  153.     canvas.bind(EButton1Down,mD,((jx1,jy2),(jx2,jyb)))   # down
  154.     canvas.bind(EButton1Down,mL,((jxa,jy1),(jx1,jy2)))   # left
  155.     canvas.bind(EButton1Down,mR,((jx2,jy1),(jxb,jy2)))   # right
  156. def jpad():
  157.     if cv.jpad:
  158.         grey=(220,220,220)
  159.         img.rectangle((jxa,jy1,jxb,jy2),fill=grey) # w
  160.         img.rectangle((jx1,jya,jx2,jyb),fill=grey) # h
  161.     jpad_key()
  162. def jpad_key():
  163.     if   cv.mv is 'U':
  164.         xy_=(jx1,jya,jx2,jy1)
  165.     elif cv.mv is 'D':
  166.         xy_=(jx1,jy2,jx2,jyb)
  167.     elif cv.mv is 'L':
  168.         xy_=(jxa,jy1,jx1,jy2)
  169.     elif cv.mv is 'R':
  170.         xy_=(jx2,jy1,jxb,jy2)
  171.     else: return
  172.     img.rectangle(xy_,fill=0xffff00) # yellow
  173. #
  174. if not touch_enabled():
  175.     note(u"This application only works on devices that support touch input")
  176. #
  177. def up_event(event):
  178.     cv.up=-1
  179. def dn_event(event):
  180.     cv.up=1
  181. #
  182. def mv_event(event):
  183.     cv.xy=event
  184. #
  185. def full_bind():
  186.     canvas.bind(EButton1Up, up_event, ((0,0), (x_scr,720)))
  187.     canvas.bind(EButton1Down, dn_event, ((0,0), (x_scr,720)))
  188.     canvas.bind(EDrag, mv_event, ((0,0), (x_scr,720)))
  189. full_bind()
  190. bind_jpad()
  191. img.clear(0)
  192.  
  193. ### enter code below...
  194.  
  195. p='p'
  196. r='r'
  197. g='g'
  198. b='b'
  199.  
  200. maze='''
  201. ∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎
  202. ∎.p. . . . . . . . .∎.x. . . . . . .∎
  203. ∎. .∎. .∎.∎.∎. .∎. .∎. .∎. .∎.∎.∎. .∎
  204. ∎. . . .∎. . . .∎. . . . . . .b.∎. .∎
  205. ∎.∎.∎. .∎. .∎. .∎. .∎. .∎. .∎. .∎. .∎
  206. ∎. . . . . .∎. . . . . .∎. . . . . .∎
  207. ∎. .∎. .∎. .∎.∎.∎. .∎. .∎.x.∎. .∎.∎.∎
  208. ∎. . .x.∎. . . . . .∎. . . .∎. . . .∎
  209. ∎.∎.∎. .∎. .∎. .∎.∎.∎.∎.∎. .∎. .∎. .∎
  210. ∎. . . . . .∎. . . . . . . . . . . .∎
  211. ∎. .∎.∎.∎. .∎. .∎. .∎.x.∎. .∎.∎.∎. .∎
  212. ∎. . . .∎. .∎.x. . . . . . .∎. . . .∎
  213. ∎.∎.∎. .∎. .∎. .∎. .∎.∎.∎. .∎. .∎.∎.∎
  214. ∎. . . . . . . .∎. . . .∎. . . . . .∎
  215. ∎. .∎. .∎.∎.∎.∎.∎. .∎. .∎. .∎.∎.∎. .∎
  216. ∎. . .r. . .∎. . .x.∎. . . . .g. .x.∎
  217. ∎. .∎.∎.∎. .∎. .∎. .∎. .∎. .∎. .∎. .∎
  218. ∎. . . . . . . .∎. . . . . .∎. . . .∎
  219. ∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎
  220. '''
  221.  
  222. def qrepl(zzz,r):
  223.     for z in r:
  224.         zzz=zzz.replace(z[0],z[1])
  225.     zzz=zzz.split()
  226.     r=[]
  227.     for z in zzz:
  228.         r.append([int(x) if x.isdigit() else x for x in list(z)])
  229.     return r
  230. fix=[['.',''],[' ','0'],['∎','1'],['x','2']]
  231.  
  232. retile=qrepl(maze,fix)
  233. tile=retile[:]
  234.  
  235. tileSIZE=X/19
  236. incr=tileSIZE
  237. tileX,tileY=15,15
  238. sb2=tileSIZE/2
  239.  
  240. move=[(-incr,0),(incr,0),(0,-incr),(0,incr),(0,0)]
  241. udlr=move[:-1]
  242. time_limit=60
  243.  
  244. '''
  245. skip=0
  246. while run:
  247.     if fr_sp():
  248.         ### CODE ###
  249.         if cv.jpad:
  250.             jpad()
  251.         draw(())
  252.     # @ end of mainloop...
  253.     e32.ao_yield()
  254. '''
  255.  
  256. def Body(s):
  257.     if len(s.hero)>1:
  258.         s.hero.pop(0)
  259.     if len(s.hero)<2:
  260.         s.last=(s.hero[0][0],s.hero[0][1])
  261.         s.hero.append((s.hero[-1][0]+s.way[0],s.hero[-1][1]+s.way[1]))
  262. #
  263. lives=1
  264. class Player:
  265.     def __init__(s):
  266.         s.life=lives
  267.         s.score=0
  268.         s.last=[]
  269.         s.way=move[0]
  270.         for my in range(len(tile)):
  271.             for mx in range(len(tile[my])):
  272.                 if tile[my][mx]=='p':
  273.                     s.hero=[(mx*incr,my*incr)]
  274.     def Collision(s):
  275.         nx=int(s.hero[-1][0]+(sb2))/tileSIZE
  276.         ny=int(s.hero[-1][1]+(sb2))/tileSIZE
  277.         if tile[ny][nx]==1:
  278.             s.way=move[4]
  279.             s.hero.append(s.last)
  280.         if tile[ny][nx]==0:
  281.             tile[ny][nx]=-1
  282.             s.score+=1
  283.         if tile[ny][nx]==2:
  284.             tile[ny][nx]=-1
  285.             s.score+=5
  286.     def Ctrl(s):
  287.         i=s.hero[0]
  288.         if i[0]%tileSIZE==0 and i[1]%tileSIZE==0:return 1
  289.         else: return 0
  290.     def Handle(s):
  291.         ctrl=s.Ctrl()
  292.         if ctrl:
  293.             if cv.mv is 'L':
  294.                 s.way=move[0]
  295.             if cv.mv is 'R':
  296.                 s.way=move[1]
  297.             if cv.mv is 'U':
  298.                 s.way=move[2]
  299.             if cv.mv is 'D':
  300.                 s.way=move[3]
  301.     def Draw(s):
  302.         px=s.hero[-1][0]
  303.         py=s.hero[-1][1]
  304.         img.point((px+tileX,py+tileY),0xdad000,width=tileSIZE)
  305. #
  306. class Enemy:
  307.     def __init__(s,obj,color):
  308.         s.color=color
  309.         s.last=[]
  310.         s.way=(incr,0)
  311.         s.pop_udlr=move[:-1]
  312.         for my in range(len(tile)):
  313.             for mx in range(len(tile[my])):
  314.                 if tile[my][mx]==obj:
  315.                     s.hero=[(mx*incr,my*incr)]
  316.                     tile[my][mx]=0
  317.         s.body=[move[2],move[3]]
  318.     def Collision(s):
  319.         nx=int(s.hero[-1][0]+(sb2))/tileSIZE
  320.         ny=int(s.hero[-1][1]+(sb2))/tileSIZE
  321.         if tile[ny][nx]==1:
  322.             if not s.pop_udlr:
  323.                 shuffle(udlr)
  324.                 s.pop_udlr=udlr[:]
  325.             s.way=s.pop_udlr.pop()
  326.             s.hero.append(s.last)
  327.         if s.hero[-1][0]==P.hero[-1][0] and s.hero[-1][1]==P.hero[-1][1]:
  328.             P.life-=1
  329.     def inTile(s,tx,ty):
  330.         s2=tileSIZE/2
  331.         nx=int(tx+s2)/tileSIZE
  332.         ny=int(ty+s2)/tileSIZE
  333.         ntile=[(0,-1),(-1,0),(1,0),(0,1)]
  334.         for i in ntile:
  335.             nxtile=nx+i[0]
  336.             nytile=ny+i[1]
  337.             if tile[nytile][nxtile] is not 1:
  338.                 if len(s.body)<3:
  339.                     s.body.append([(nxtile-nx)*incr,(nytile-ny)*incr])
  340.                 if len(s.body)>0:s.body.pop(0)
  341.     def Draw(s):
  342.         px=s.hero[-1][0]
  343.         py=s.hero[-1][1]
  344.         img.point((px+tileX,py+tileY),s.color,width=tileSIZE)
  345.         s.inTile(px,py)
  346. #
  347. P=Player()
  348. RED=Enemy('r',0xaa0000)
  349. BLUE=Enemy('b',0x0000dd)
  350. GREEN=Enemy('g',0x00dd00)
  351. Group=[P,RED,GREEN,BLUE]
  352. Reset=Group[:]
  353.  
  354. dots=0
  355. for my in range(len(tile)):
  356.     for mx in range(len(tile[my])):
  357.         if tile[my][mx]==0:
  358.             dots+=1
  359.         if tile[my][mx]==2:
  360.             dots+=5
  361. #
  362. secs=0
  363. while cv.go:
  364.     if fr_sp(0.1):
  365.         if (P.score > dots-12) or (time_limit < secs):
  366.             secs=time_limit
  367.             msg='With %d pts...\nYou Survived !!!'%(P.score)
  368.             xout(msg)
  369.         elif P.life!=1:
  370.             msg='With %d pts...\nYou Lost...'%(P.score-50)
  371.             xout(msg)
  372.         if cv.init:
  373.             cv.init=0
  374.             secs=0
  375.             Group=Reset[:]
  376.             starting=time.time()
  377.             tile=retile[:]
  378.         [Body(z) for z in Group]
  379.         img.clear(0)
  380.         m2=tileSIZE/2
  381.         for my in range(len(tile)):
  382.             for mx in range(len(tile[my])):
  383.                 if tile[my][mx]==1:
  384.                     img.rectangle((tileX-m2+(mx*tileSIZE),tileY-m2+(my*tileSIZE),tileX+m2+(mx*tileSIZE),tileY+m2+(my*tileSIZE)),fill=0x999999)
  385.                 if tile[my][mx]==0:
  386.                     img.point((tileX+(mx*tileSIZE),tileY+(my*tileSIZE)),0xdadada,width=tileSIZE/3)
  387.                 if tile[my][mx]==2:
  388.                     img.point((tileX+(mx*tileSIZE),tileY+(my*tileSIZE)),0x00ff00,width=tileSIZE/2)
  389.         [z.Collision() for z in Group]
  390.         P.Handle()
  391.         [z.Draw() for z in Group]
  392.        
  393.         if cv.jpad:
  394.             jpad()
  395.         status(0x00ff00)
  396.         xout_btn() # default is invisible
  397.         ending=time.time()
  398.         secs=ending-starting
  399.         draw(())
  400.     e32.ao_yield()
  401. #
  402. print P.score
  403. print '\n'*4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement