Advertisement
phjoe

Swap Image

Jan 31st, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # SWAP IMAGE TEST
  2. # joe, 31/01/2015
  3. # TODO: Cek posisi gambar jika cocok dengan posisi sebelum diacak
  4.  
  5. import appuifw as A
  6. import graphics as G
  7.  
  8. class PIX:
  9.  def __init__(self):
  10.   self.run=False
  11.   self.curx,self.cury=0,0
  12.   self.rows,self.cols=4,4
  13.   self.pos=[]
  14.   self.swap=False
  15.   self.cur_index=[(x,y) for y in range(self.cols) for x in range(self.rows)]
  16.   self.a=self.b=None
  17.   A.app.screen='full'
  18.   self.c = A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
  19.   A.app.body=self.c
  20.   A.app.exit_key_handler=self.exit
  21.  
  22.  def exit(self):
  23.   self.run=0
  24.   #A.app.set_exit()
  25.  
  26.  def _redraw(self,rect):
  27.   if img:self.c.blit(img)
  28.  
  29.  def _event(self,sc):
  30.   k=sc['scancode']
  31.   if sc['type'] is not A.EEventKey:return
  32.   if k in (0xf,0x36): # 6 / kanan
  33.    self.curx+=1
  34.    if self.curx>self.rows-1:
  35.     self.curx=0
  36.     self.cury+=1
  37.     if self.cury>self.cols-1:
  38.      self.cury=0
  39.      self.curx=0
  40.   elif k in (0xe,0x34): # 4 / kiri
  41.    self.curx-=1
  42.    if self.curx<0:
  43.     self.curx=self.rows-1
  44.     self.cury-=1
  45.     if self.cury<0:
  46.      self.cury=self.cols-1
  47.      self.curx=self.rows-1
  48.   elif k in (0x10,0x32): # 2 / atas
  49.    self.cury-=1
  50.    if self.cury<0:
  51.     self.cury=self.cols-1
  52.   elif k in (0x11,0x38): # 8 / bawah
  53.    self.cury+=1
  54.    if self.cury>self.cols-1:
  55.     self.cury=0
  56.   elif k in (0xa7,0x35): # OK / 5
  57.    if self.a is None:
  58.     self.a=self.curx + self.cury*self.cols
  59.    else:
  60.     if self.a!=None:
  61.      self.b=self.curx + self.cury*self.cols
  62.      self.swap=True
  63.  
  64.  def play(self):
  65.   self.run=True
  66.   w,h=size[0]/self.rows,size[1]/self.cols
  67.   x0,y0=0,0
  68.   for y in xrange(self.cols):
  69.    y1=y0+(y*h)
  70.    y2=y1+h
  71.    for x in range(self.rows):
  72.     x1=x0+(x*w)
  73.     x2=x1+w
  74.     self.pos.append((x1,y1,x2,y2))
  75.  
  76.   # acak posisi
  77.   random.shuffle(self.pos)
  78.  
  79.   while self.run:
  80.    img.clear(0)
  81.    for i in range(self.rows*self.cols):
  82.     cx,cy=(i%self.rows),(i/self.cols)
  83.     img.blit(gb,target=(x0+w*cx,y0+h*cy),source=self.pos[i])
  84.  
  85.    # kursor
  86.    dx=x0+(self.curx*w)
  87.    dy=y0+(self.cury*h)
  88.    img.rectangle((dx,dy,dx+w,dy+h),0xff0000,width=2)
  89.  
  90.    if self.a!=None:
  91.     # gambar kursor kalo ada yg dipilih
  92.     dx=x0 + (self.cur_index[self.a][0]*w)
  93.     dy=y0 + (self.cur_index[self.a][1]*h)
  94.     img.rectangle((dx,dy,dx+w,dy+h),0x00ff00,width=2)
  95.  
  96.    self._redraw(0)
  97.    A.e32.ao_sleep(1e-04)
  98.  
  99.    if self.swap:
  100.     # swap list
  101.     self.pos[self.a],self.pos[self.b]=self.pos[self.b],self.pos[self.a]
  102.     # netralkan variabel
  103.     self.swap=False
  104.     self.a,self.b=None,None
  105.  
  106.  
  107. import random
  108. size=G.sysinfo.display_pixels()
  109. img=G.Image.new(size)
  110.  
  111. gb=G.Image.open(u'e:\\a.jpg') # edit
  112. if gb.size!=size:
  113.  gb=gb.resize(size)
  114.  
  115. PIX().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement