Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # SWAP IMAGE TEST
- # joe, 31/01/2015
- # TODO: Cek posisi gambar jika cocok dengan posisi sebelum diacak
- import appuifw as A
- import graphics as G
- class PIX:
- def __init__(self):
- self.run=False
- self.curx,self.cury=0,0
- self.rows,self.cols=4,4
- self.pos=[]
- self.swap=False
- self.cur_index=[(x,y) for y in range(self.cols) for x in range(self.rows)]
- self.a=self.b=None
- A.app.screen='full'
- self.c = A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
- A.app.body=self.c
- A.app.exit_key_handler=self.exit
- def exit(self):
- self.run=0
- #A.app.set_exit()
- def _redraw(self,rect):
- if img:self.c.blit(img)
- def _event(self,sc):
- k=sc['scancode']
- if sc['type'] is not A.EEventKey:return
- if k in (0xf,0x36): # 6 / kanan
- self.curx+=1
- if self.curx>self.rows-1:
- self.curx=0
- self.cury+=1
- if self.cury>self.cols-1:
- self.cury=0
- self.curx=0
- elif k in (0xe,0x34): # 4 / kiri
- self.curx-=1
- if self.curx<0:
- self.curx=self.rows-1
- self.cury-=1
- if self.cury<0:
- self.cury=self.cols-1
- self.curx=self.rows-1
- elif k in (0x10,0x32): # 2 / atas
- self.cury-=1
- if self.cury<0:
- self.cury=self.cols-1
- elif k in (0x11,0x38): # 8 / bawah
- self.cury+=1
- if self.cury>self.cols-1:
- self.cury=0
- elif k in (0xa7,0x35): # OK / 5
- if self.a is None:
- self.a=self.curx + self.cury*self.cols
- else:
- if self.a!=None:
- self.b=self.curx + self.cury*self.cols
- self.swap=True
- def play(self):
- self.run=True
- w,h=size[0]/self.rows,size[1]/self.cols
- x0,y0=0,0
- for y in xrange(self.cols):
- y1=y0+(y*h)
- y2=y1+h
- for x in range(self.rows):
- x1=x0+(x*w)
- x2=x1+w
- self.pos.append((x1,y1,x2,y2))
- # acak posisi
- random.shuffle(self.pos)
- while self.run:
- img.clear(0)
- for i in range(self.rows*self.cols):
- cx,cy=(i%self.rows),(i/self.cols)
- img.blit(gb,target=(x0+w*cx,y0+h*cy),source=self.pos[i])
- # kursor
- dx=x0+(self.curx*w)
- dy=y0+(self.cury*h)
- img.rectangle((dx,dy,dx+w,dy+h),0xff0000,width=2)
- if self.a!=None:
- # gambar kursor kalo ada yg dipilih
- dx=x0 + (self.cur_index[self.a][0]*w)
- dy=y0 + (self.cur_index[self.a][1]*h)
- img.rectangle((dx,dy,dx+w,dy+h),0x00ff00,width=2)
- self._redraw(0)
- A.e32.ao_sleep(1e-04)
- if self.swap:
- # swap list
- self.pos[self.a],self.pos[self.b]=self.pos[self.b],self.pos[self.a]
- # netralkan variabel
- self.swap=False
- self.a,self.b=None,None
- import random
- size=G.sysinfo.display_pixels()
- img=G.Image.new(size)
- gb=G.Image.open(u'e:\\a.jpg') # edit
- if gb.size!=size:
- gb=gb.resize(size)
- PIX().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement