Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # # py60_gamegui.py + demo ZZZ
- from appuifw import *
- import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,copy
- from graphics import *
- from math import *
- from random import *
- from key_codes import *
- def scr(msg):
- txt.set(unicode(msg))
- z_Colors = ((u'RGB_BLACK',(0,0,0)),
- (u'RGB_WHITE',(255,255,255)),
- (u'RGB_GRAYLIGHT',(211,211,211)),
- (u'RGB_GRAY',(128,128,128)),
- (u'RGB_GRAYDARK',(169,169,169)),
- (u'RGB_RED',(255,0,0)),
- (u'RGB_ORANGE',(255,165,0)),
- (u'RGB_YELLOW',(255,255,0)),
- (u'RGB_GREEN',(0,128,0)),
- (u'RGB_BLUE',(0,0,255)),
- (u'RGB_CYAN',(0,255,255)),
- (u'RGB_PURPLE',(128,0,255)),
- (u'RGB_PINK',(255,148,238)),
- (u'RGB_DARKBLUE',(0,0,139)),
- (u'RGB_DARKGREEN',(0,100,0)),
- (u'RGB_DEEPPINK',(255,20,147)),
- (u'RGB_INDIGO',(75,0,130)),
- (u'RGB_LIGHTBLUE',(173,216,230)),
- (u'RGB_LIME',(0,255,0)),
- (u'RGB_OLIVE',(107,142,35)),
- (u'RGB_BROWN',(128,64,0)),
- (u'RGB_GOLD',(255,215,0)),
- (u'RGB_SILVER',(192,192,192)))
- #
- rgb_menu="rgb_menu=["
- for rgb in z_Colors:
- i,ii=rgb[0].replace('RGB_',''),str(rgb[1])
- rgb_menu += "(u'"+i+"',c_"+i+"),"
- exec 'def c_'+i+'(): cv.color='+ii
- exec rgb[0]+'='+ii
- rgb_menu += ']'
- def draw(rect): # >>> draw(())
- if img:
- canvas.blit(img)
- def e_callback(event):
- x,y=event['pos']
- try: timer.cancel()
- except: pass
- cv.x=x
- cv.y=y
- #
- defaultspeed=1200.00
- def sp():
- try:
- e32.ao_sleep(speed/sp)
- speed-=0.9
- except: pass
- return defaultspeed-cv.speed
- #
- def fr_sp(z=0.05): # if glichy or 0.1 for moderate speed
- if time.time() > cv.skip:
- cv.skip=time.time()+z
- return True
- #
- def status(color=RGB_WHITE):
- img.text((5,Y-40),u'Score:',color)
- img.text((5,Y-20),u'%d'%(P.score),color)
- try:
- img.text((X-100,Y-20),u''+str(round(time_limit-secs,2)),color)
- img.text((X-100,Y-40),u'Time Left:',color)
- except: pass
- #
- class cv():
- go=1
- init=1
- skip=0
- color=RGB_WHITE
- xyz=(0,0,0)
- xy=0,0
- x=y=0
- light=0
- evt=None
- tmp=0
- sw=0
- jpad=True
- padx,pady=0,0
- mv='X'
- up=-1
- speed=defaultspeed
- cv=cv()
- app.screen = 'large'
- lg_scr = 576
- app.directional_pad = False
- app.orientation='portrait'
- img=None
- app.body = canvas = Canvas(redraw_callback=draw, event_callback=e_callback) #
- x_scr,y_scr=canvas.size
- X,Y=canvas.size
- img=Image.new((x_scr,y_scr))
- def xout(msg=''):
- if query(u''+msg+'\nExit Game?','query'):
- cv.go=0; return 0
- cv.init=0
- def xout_btn(attr=0):
- attr=str(attr)
- bg,fg=RGB_RED,RGB_WHITE
- a,b=cv.xy
- w,h=180,90
- L=w
- if '1' in attr: h=y_scr # not recommended
- if 'v' in attr:
- img.rectangle((w,h-90,w+L,h),fill=bg)
- img.text((w,h-10),u'EXIT',fg,font=('dense',98))
- if (w < a < w+L) and (h-90 < b < h):
- xout()
- cv.xy=0,0
- def void(): pass
- app.exit_key_handler=void
- run=True
- import sensor
- def xyzsens():
- try:
- cv.xyz=(sens.x,sens.y,sens.z)
- except: pass
- sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
- sens.set_callback(data_callback=xyzsens)
- sens.start_listening()
- ### joypad
- jxa,jya=70,350 ### <<< jpad xy axis point
- jxb,jyb=jxa+220,jya+220
- jx1,jy1=jxa+70,jya+70
- jx2,jy2=jxa+150,jya+150
- def mU(event):
- cv.mv='U'
- def mD(event):
- cv.mv='D'
- def mL(event):
- cv.mv='L'
- def mR(event):
- cv.mv='R'
- def bind_jpad():
- canvas.bind(EButton1Down,mU,((jx1,jya),(jx2,jy1))) # up
- canvas.bind(EButton1Down,mD,((jx1,jy2),(jx2,jyb))) # down
- canvas.bind(EButton1Down,mL,((jxa,jy1),(jx1,jy2))) # left
- canvas.bind(EButton1Down,mR,((jx2,jy1),(jxb,jy2))) # right
- def jpad():
- if cv.jpad:
- grey=(220,220,220)
- img.rectangle((jxa,jy1,jxb,jy2),fill=grey) # w
- img.rectangle((jx1,jya,jx2,jyb),fill=grey) # h
- jpad_key()
- def jpad_key():
- if cv.mv is 'U':
- xy_=(jx1,jya,jx2,jy1)
- elif cv.mv is 'D':
- xy_=(jx1,jy2,jx2,jyb)
- elif cv.mv is 'L':
- xy_=(jxa,jy1,jx1,jy2)
- elif cv.mv is 'R':
- xy_=(jx2,jy1,jxb,jy2)
- else: return
- img.rectangle(xy_,fill=0xffff00) # yellow
- #
- if not touch_enabled():
- note(u"This application only works on devices that support touch input")
- #
- def up_event(event):
- cv.up=-1
- def dn_event(event):
- cv.up=1
- #
- def mv_event(event):
- cv.xy=event
- #
- def full_bind():
- canvas.bind(EButton1Up, up_event, ((0,0), (x_scr,720)))
- canvas.bind(EButton1Down, dn_event, ((0,0), (x_scr,720)))
- canvas.bind(EDrag, mv_event, ((0,0), (x_scr,720)))
- full_bind()
- bind_jpad()
- img.clear(0)
- ### enter code below...
- p='p'
- r='r'
- g='g'
- b='b'
- maze='''
- ∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎
- ∎.p. . . . . . . . .∎.x. . . . . . .∎
- ∎. .∎. .∎.∎.∎. .∎. .∎. .∎. .∎.∎.∎. .∎
- ∎. . . .∎. . . .∎. . . . . . .b.∎. .∎
- ∎.∎.∎. .∎. .∎. .∎. .∎. .∎. .∎. .∎. .∎
- ∎. . . . . .∎. . . . . .∎. . . . . .∎
- ∎. .∎. .∎. .∎.∎.∎. .∎. .∎.x.∎. .∎.∎.∎
- ∎. . .x.∎. . . . . .∎. . . .∎. . . .∎
- ∎.∎.∎. .∎. .∎. .∎.∎.∎.∎.∎. .∎. .∎. .∎
- ∎. . . . . .∎. . . . . . . . . . . .∎
- ∎. .∎.∎.∎. .∎. .∎. .∎.x.∎. .∎.∎.∎. .∎
- ∎. . . .∎. .∎.x. . . . . . .∎. . . .∎
- ∎.∎.∎. .∎. .∎. .∎. .∎.∎.∎. .∎. .∎.∎.∎
- ∎. . . . . . . .∎. . . .∎. . . . . .∎
- ∎. .∎. .∎.∎.∎.∎.∎. .∎. .∎. .∎.∎.∎. .∎
- ∎. . .r. . .∎. . .x.∎. . . . .g. .x.∎
- ∎. .∎.∎.∎. .∎. .∎. .∎. .∎. .∎. .∎. .∎
- ∎. . . . . . . .∎. . . . . .∎. . . .∎
- ∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎.∎
- '''
- def qrepl(zzz,r):
- for z in r:
- zzz=zzz.replace(z[0],z[1])
- zzz=zzz.split()
- r=[]
- for z in zzz:
- r.append([int(x) if x.isdigit() else x for x in list(z)])
- return r
- fix=[['.',''],[' ','0'],['∎','1'],['x','2']]
- retile=qrepl(maze,fix)
- tile=retile[:]
- tileSIZE=X/19
- incr=tileSIZE
- tileX,tileY=15,15
- sb2=tileSIZE/2
- move=[(-incr,0),(incr,0),(0,-incr),(0,incr),(0,0)]
- udlr=move[:-1]
- time_limit=60
- '''
- skip=0
- while run:
- if fr_sp():
- ### CODE ###
- if cv.jpad:
- jpad()
- draw(())
- # @ end of mainloop...
- e32.ao_yield()
- '''
- def Body(s):
- if len(s.hero)>1:
- s.hero.pop(0)
- if len(s.hero)<2:
- s.last=(s.hero[0][0],s.hero[0][1])
- s.hero.append((s.hero[-1][0]+s.way[0],s.hero[-1][1]+s.way[1]))
- #
- lives=1
- class Player:
- def __init__(s):
- s.life=lives
- s.score=0
- s.last=[]
- s.way=move[0]
- for my in range(len(tile)):
- for mx in range(len(tile[my])):
- if tile[my][mx]=='p':
- s.hero=[(mx*incr,my*incr)]
- def Collision(s):
- nx=int(s.hero[-1][0]+(sb2))/tileSIZE
- ny=int(s.hero[-1][1]+(sb2))/tileSIZE
- if tile[ny][nx]==1:
- s.way=move[4]
- s.hero.append(s.last)
- if tile[ny][nx]==0:
- tile[ny][nx]=-1
- s.score+=1
- if tile[ny][nx]==2:
- tile[ny][nx]=-1
- s.score+=5
- def Ctrl(s):
- i=s.hero[0]
- if i[0]%tileSIZE==0 and i[1]%tileSIZE==0:return 1
- else: return 0
- def Handle(s):
- ctrl=s.Ctrl()
- if ctrl:
- if cv.mv is 'L':
- s.way=move[0]
- if cv.mv is 'R':
- s.way=move[1]
- if cv.mv is 'U':
- s.way=move[2]
- if cv.mv is 'D':
- s.way=move[3]
- def Draw(s):
- px=s.hero[-1][0]
- py=s.hero[-1][1]
- img.point((px+tileX,py+tileY),0xdad000,width=tileSIZE)
- #
- class Enemy:
- def __init__(s,obj,color):
- s.color=color
- s.last=[]
- s.way=(incr,0)
- s.pop_udlr=move[:-1]
- for my in range(len(tile)):
- for mx in range(len(tile[my])):
- if tile[my][mx]==obj:
- s.hero=[(mx*incr,my*incr)]
- tile[my][mx]=0
- s.body=[move[2],move[3]]
- def Collision(s):
- nx=int(s.hero[-1][0]+(sb2))/tileSIZE
- ny=int(s.hero[-1][1]+(sb2))/tileSIZE
- if tile[ny][nx]==1:
- if not s.pop_udlr:
- shuffle(udlr)
- s.pop_udlr=udlr[:]
- s.way=s.pop_udlr.pop()
- s.hero.append(s.last)
- if s.hero[-1][0]==P.hero[-1][0] and s.hero[-1][1]==P.hero[-1][1]:
- P.life-=1
- def inTile(s,tx,ty):
- s2=tileSIZE/2
- nx=int(tx+s2)/tileSIZE
- ny=int(ty+s2)/tileSIZE
- ntile=[(0,-1),(-1,0),(1,0),(0,1)]
- for i in ntile:
- nxtile=nx+i[0]
- nytile=ny+i[1]
- if tile[nytile][nxtile] is not 1:
- if len(s.body)<3:
- s.body.append([(nxtile-nx)*incr,(nytile-ny)*incr])
- if len(s.body)>0:s.body.pop(0)
- def Draw(s):
- px=s.hero[-1][0]
- py=s.hero[-1][1]
- img.point((px+tileX,py+tileY),s.color,width=tileSIZE)
- s.inTile(px,py)
- #
- P=Player()
- RED=Enemy('r',0xaa0000)
- BLUE=Enemy('b',0x0000dd)
- GREEN=Enemy('g',0x00dd00)
- Group=[P,RED,GREEN,BLUE]
- Reset=Group[:]
- dots=0
- for my in range(len(tile)):
- for mx in range(len(tile[my])):
- if tile[my][mx]==0:
- dots+=1
- if tile[my][mx]==2:
- dots+=5
- #
- secs=0
- while cv.go:
- if fr_sp(0.1):
- if (P.score > dots-12) or (time_limit < secs):
- secs=time_limit
- msg='With %d pts...\nYou Survived !!!'%(P.score)
- xout(msg)
- elif P.life!=1:
- msg='With %d pts...\nYou Lost...'%(P.score-50)
- xout(msg)
- if cv.init:
- cv.init=0
- secs=0
- Group=Reset[:]
- starting=time.time()
- tile=retile[:]
- [Body(z) for z in Group]
- img.clear(0)
- m2=tileSIZE/2
- for my in range(len(tile)):
- for mx in range(len(tile[my])):
- if tile[my][mx]==1:
- img.rectangle((tileX-m2+(mx*tileSIZE),tileY-m2+(my*tileSIZE),tileX+m2+(mx*tileSIZE),tileY+m2+(my*tileSIZE)),fill=0x999999)
- if tile[my][mx]==0:
- img.point((tileX+(mx*tileSIZE),tileY+(my*tileSIZE)),0xdadada,width=tileSIZE/3)
- if tile[my][mx]==2:
- img.point((tileX+(mx*tileSIZE),tileY+(my*tileSIZE)),0x00ff00,width=tileSIZE/2)
- [z.Collision() for z in Group]
- P.Handle()
- [z.Draw() for z in Group]
- if cv.jpad:
- jpad()
- status(0x00ff00)
- xout_btn() # default is invisible
- ending=time.time()
- secs=ending-starting
- draw(())
- e32.ao_yield()
- #
- print P.score
- print '\n'*4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement