Advertisement
phjoe

Ots60

Dec 15th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Crazy othelo for pys60, i said crazy because its hard to win
  2. # Joe, 15/12/2014
  3. # cara mainnya seperti main tic tac toe
  4.  
  5. import appuifw as A
  6. import graphics as G
  7.  
  8. # warna
  9. box_color=0x84c300
  10. bgcolor=0x426900
  11. txtcolor=0xffffff
  12. focus_color=0x8cd300
  13. win_bgcolor=0x393939
  14. win_txtcolor=0xffffff
  15. win_bordercolor=0x9ceb00
  16. bar_bgcolor=0x990000
  17. bar_txtcolor=0xffffff
  18. cur_color=0xff0000
  19. player_color=0xffffff
  20. cpu_color=0
  21. fontb='legend'
  22.  
  23.  
  24.  
  25. class Othelo:
  26.  def __init__(self):
  27.   self.w,self.h=G.sysinfo.display_pixels()
  28.   self.img=G.Image.new((self.w,self.h))
  29.   self.main_loop,self.pause,self.info,self.hint=0,0,0,0
  30.   self.illegal,self.over=0,1
  31.   self.turn,self.remain=0,60
  32.   self.curx,self.cury,self.cur_m=3,3,0
  33.   self.list_menu=[(u'Permainan Baru',self.new_game),(u'Bantuan',self.help),(u'Tentang',self.about),(u'Keluar',self.exit)]
  34.  
  35.   # papan
  36.   self.board=[
  37.    [0,0,0,0,0,0,0,0],
  38.    [0,0,0,0,0,0,0,0],
  39.    [0,0,0,0,0,0,0,0],
  40.    [0,0,0,2,1,0,0,0],
  41.    [0,0,0,1,2,0,0,0],
  42.    [0,0,0,0,0,0,0,0],
  43.    [0,0,0,0,0,0,0,0],
  44.    [0,0,0,0,0,0,0,0]
  45.     ]
  46.   self.old=(self.turn,self.remain,self.curx,self.cury,self.cur_m)
  47.   A.app.screen='full'
  48.   self.c=A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
  49.   A.app.body=self.c
  50.   A.app.exit_key_handler=lambda:None
  51.  
  52.  def _search(self,x,y,a,b):
  53.   pos=[(x,y)]
  54.   dx,dy=x+a[0],y+a[1]
  55.   if (dx>=0) and (dx<8) and (dy>=0) and (dy<8):
  56.    if self.board[dy][dx]==0:
  57.     return []
  58.    elif self.board[dy][dx]==b:
  59.     return pos
  60.    else:
  61.     res=self._search(dx,dy,a,b)
  62.     if len(res)>0:
  63.      pos+=res
  64.   if len(pos)>1:
  65.    return pos
  66.   else:
  67.    return []
  68.  
  69.  def switch(self):
  70.   if not self.turn:
  71.    self.turn=1
  72.   else:
  73.    self.turn=0
  74.  
  75.  def _move(self,x,y):
  76.   dirs=[(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1)]
  77.   if (x<0) or (x>7) or (y<0) or (y>7) or self.board[y][x] <> 0:
  78.    return []
  79.   if not self.turn:
  80.    p=1
  81.   else:
  82.    p=0
  83.   res=[]
  84.   for n in dirs:
  85.    dx,dy=x+n[0],y+n[1]
  86.    if (dx>=0) and (dx<8) and (dy>=0) and (dy<8) and (self.board[dy][dx]==p+1):
  87.     res2=self._search(dx,dy,n,self.turn+1)
  88.     if len(res2)>0:
  89.      res+=res2
  90.   return res
  91.  
  92.  def choose(self,x,y):
  93.   res=self._move(x,y)
  94.   if len(res)<1:
  95.    return False
  96.   for p in res:
  97.    self.board[p[1]][p[0]]=self.turn+1
  98.    self.board[y][x]=1+self.turn
  99.    self.remain-=1
  100.    return True
  101.  
  102.  def cpu_play(self):
  103.   res=[]
  104.   px,py=0,0
  105.   for y in range(8):
  106.    for x in range(8):
  107.     if self.board[y][x]==0:
  108.      res2=self._move(x,y)
  109.      if len(res2)>len(res):
  110.       res=res2
  111.       px,py=x,y
  112.       if len(res)<1:
  113.        return False
  114.   for p in res:
  115.    self.board[p[1]][p[0]]=self.turn+1
  116.    self.board[py][px]=1+self.turn
  117.    self.remain-=1
  118.    return True
  119.  
  120.  def get_skor(self):
  121.   player,cpu=0,0
  122.   for y in range(8):
  123.    for x in range(8):
  124.     if self.board[y][x]==1:
  125.      player+=1
  126.     elif self.board[y][x]==2:
  127.      cpu+=1
  128.   return player,cpu
  129.  
  130.  def _event(self,sc):
  131.   k=sc['scancode']
  132.   if not self.pause:
  133.    if self.main_loop:
  134.     if sc['type']==3 and k==164:
  135.      self.illegal,self.cur_m,self.pause,self.info,self.hint=0,0,1,0,0
  136.  
  137.    if sc['type'] is not A.EEventKey:return
  138.    self.illegal,self.info,self.hint=0,0,0
  139.    if not self.over:
  140.     if k in (0xf,0x36): # kanan
  141.      self.curx+=1
  142.      if self.curx>7:
  143.       self.curx=0
  144.       self.cury+=1
  145.       if self.cury>7:
  146.        self.cury=0
  147.        self.curx=0
  148.     elif k in (0xe,0x34): # kiri
  149.      self.curx-=1
  150.      if self.curx<0:
  151.       self.curx=7
  152.       self.cury-=1
  153.       if self.cury<0:
  154.        self.cury=7
  155.        self.curx=7
  156.     elif k in (0x10,0x32): # atas
  157.      self.cury-=1
  158.      if self.cury<0:
  159.       self.cury=7
  160.     elif k in (0x11,0x38): # bawah
  161.      self.cury+=1
  162.      if self.cury>7:
  163.       self.cury=0
  164.     elif k in (0xa7,0x35): # OK / 5
  165.      if self.choose(self.curx,self.cury):
  166.       self.turn=1
  167.       self.illegal=0
  168.       self.cpu_play()
  169.       self.switch()
  170.      else:
  171.       self.illegal=1
  172.     if k==0x30: # O pass
  173.      self.turn=1
  174.      self.cpu_play()
  175.      self.switch()
  176.  
  177.   else: # Menu
  178.    if sc['type']==3 and k in (164,165):
  179.     if self.pause:
  180.      self.pause=0
  181.      return
  182.    if sc['type'] is not A.EEventKey:return
  183.    lem=len(self.list_menu)-1
  184.    if k in (0x10,0x32): # atas
  185.     self.cur_m-=1
  186.     if self.cur_m<0:
  187.      self.cur_m=lem
  188.    elif k in (0x11,0x38): # bawah
  189.     self.cur_m+=1
  190.     if self.cur_m>lem:
  191.      self.cur_m=0
  192.    elif k in (0xa7,0x35): # OK / 5
  193.     if callable(self.list_menu[self.cur_m][1]):
  194.      self.pause=0
  195.      self.list_menu[self.cur_m][1]()
  196.  
  197.  def exit(self):
  198.   self.main_loop=0
  199.  
  200.  def new_game(self):
  201.   self.board=[
  202.    [0,0,0,0,0,0,0,0],
  203.    [0,0,0,0,0,0,0,0],
  204.    [0,0,0,0,0,0,0,0],
  205.    [0,0,0,2,1,0,0,0],
  206.    [0,0,0,1,2,0,0,0],
  207.    [0,0,0,0,0,0,0,0],
  208.    [0,0,0,0,0,0,0,0],
  209.    [0,0,0,0,0,0,0,0]
  210.     ]
  211.   (self.turn,self.remain,self.curx,self.cury,self.cur_m)=self.old
  212.   self.play()
  213.  
  214.  def help(self):
  215.   self.hint=1
  216.  
  217.  def about(self):
  218.   self.info=1
  219.  
  220.  def _redraw(self,rect):
  221.   if self.img:
  222.    self.c.blit(self.img)
  223.  
  224.  def _tsize(self,text,fnt):
  225.   if not isinstance(text,unicode):
  226.    text=unicode(text)
  227.   m=self.img.measure_text(text,font=fnt)[0]
  228.   w,h=(m[2]-m[0],m[3]-m[1])
  229.   return (w,h)
  230.  
  231.  def draw_menu(self):
  232.   x,y=self.w/2,self.h/2
  233.   lem=len(self.list_menu)
  234.   th=14
  235.   rw,rh=60,(lem*th+10)/2
  236.   self.img.rectangle((x-rw,y-rh,x+rw,y+rh),win_bordercolor,win_bgcolor)
  237.   for i in range(len(self.list_menu)):
  238.    col=(win_txtcolor,focus_color)[i==self.cur_m]
  239.    self.img.text((x-rw+12,y-rh+th+(i*th)),u'%s' %self.list_menu[i][0],col)
  240.   # pointer
  241.   cy=y-rh+th+(self.cur_m*th)
  242.   self.img.rectangle((x-rw+5,cy-6,x-rw+9,cy-2),fill=focus_color)
  243.  
  244.  def note(self,text):
  245.   t=map(unicode,text)
  246.   x,y=self.w/2,self.h/2
  247.   lem,th=len(t),14
  248.   rw,rh=60,(lem*th+10)/2
  249.   self.img.rectangle((x-rw,y-rh,x+rw,y+rh),win_bordercolor,win_bgcolor)
  250.   for i in range(lem):
  251.    self.img.text((x-rw+5,y-rh+th+(i*th)),u'%s' %t[i],win_txtcolor)
  252.  
  253.  def intro(self):
  254.   self.img.clear(16777215)
  255.   for i in range(16777215,0,-1052688):
  256.    self.img.clear(i)
  257.    self._redraw(())
  258.    A.e32.ao_sleep(0.05)
  259.   for i in range(0,16776960,1052672):
  260.    self.img.clear(0)
  261.    self._redraw(())
  262.    A.e32.ao_sleep(0.05)
  263.  
  264.  def play(self):
  265.   self.main_loop,self.over=1,0
  266.   px,py,width,pad=5,5,20,1
  267.   while self.main_loop:
  268.    self.img.clear(bgcolor)
  269.    for y in range(8):
  270.     for x in range(8):
  271.      col=box_color
  272.      if self.board[y][x]>0:
  273.       col=[0xfefefe,0][self.board[y][x]-1]
  274.      dx=px+(x*width)+(x*pad)
  275.      dy=py+(y*width)+(y*pad)
  276.      self.img.rectangle((dx,dy,dx+width,dy+width),fill=col)
  277.    # kursor
  278.    cx=px+(self.curx*width) + (self.curx*pad)
  279.    cy=py+(self.cury*width) + (self.cury*pad)
  280.    if not self.pause:
  281.     self.img.rectangle((cx,cy,cx+width,cy+width),cur_color,width=2)
  282.    if self.illegal:
  283.     self.note(['Ow ow..', 'Gak bisa jalan disini!'])
  284.  
  285.    skor=self.get_skor()
  286.    # skor player
  287.    self.img.rectangle((px,self.h-28,px+8,self.h-20),fill=player_color)
  288.    self.img.text((px+12,self.h-20),u'Player: %d' %skor[0],player_color)
  289.  
  290.    # skor AI CPU
  291.    self.img.rectangle((px,self.h-14,px+8,self.h-6),fill=cpu_color)
  292.    self.img.text((px+12,self.h-6),u'CPU: %d' %skor[1],cpu_color)
  293.    if skor[0]+skor[1]==64 or skor[0]<1:
  294.     self.over=1
  295.     self.note([u'Game Over'])
  296.  
  297.    if self.hint:self.note(['Gunakan Joystik','atau 2, 4, 6, 8','untuk navigasi.','','5 > Memilih','0 > Skip Giliran'])
  298.    if self.info:self.note(['Crazy Othelo','',u'\u00a9 2014, Joe','Some Rights Reserved'])
  299.    if self.pause:self.draw_menu()
  300.    if self.turn:
  301.     if not self.cpu_play():
  302.      self.turn=0
  303.      self.switch()
  304.      self.note(['Giliran Kamu'])
  305.    self._redraw(())
  306.    A.e32.ao_sleep(1E-08)
  307.  
  308.  
  309.  
  310. game=Othelo()
  311. game.intro()
  312. game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement