Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Crazy othelo for pys60, i said crazy because its hard to win
- # Joe, 15/12/2014
- # cara mainnya seperti main tic tac toe
- import appuifw as A
- import graphics as G
- # warna
- box_color=0x84c300
- bgcolor=0x426900
- txtcolor=0xffffff
- focus_color=0x8cd300
- win_bgcolor=0x393939
- win_txtcolor=0xffffff
- win_bordercolor=0x9ceb00
- bar_bgcolor=0x990000
- bar_txtcolor=0xffffff
- cur_color=0xff0000
- player_color=0xffffff
- cpu_color=0
- fontb='legend'
- class Othelo:
- def __init__(self):
- self.w,self.h=G.sysinfo.display_pixels()
- self.img=G.Image.new((self.w,self.h))
- self.main_loop,self.pause,self.info,self.hint=0,0,0,0
- self.illegal,self.over=0,1
- self.turn,self.remain=0,60
- self.curx,self.cury,self.cur_m=3,3,0
- self.list_menu=[(u'Permainan Baru',self.new_game),(u'Bantuan',self.help),(u'Tentang',self.about),(u'Keluar',self.exit)]
- # papan
- self.board=[
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,2,1,0,0,0],
- [0,0,0,1,2,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0]
- ]
- self.old=(self.turn,self.remain,self.curx,self.cury,self.cur_m)
- 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=lambda:None
- def _search(self,x,y,a,b):
- pos=[(x,y)]
- dx,dy=x+a[0],y+a[1]
- if (dx>=0) and (dx<8) and (dy>=0) and (dy<8):
- if self.board[dy][dx]==0:
- return []
- elif self.board[dy][dx]==b:
- return pos
- else:
- res=self._search(dx,dy,a,b)
- if len(res)>0:
- pos+=res
- if len(pos)>1:
- return pos
- else:
- return []
- def switch(self):
- if not self.turn:
- self.turn=1
- else:
- self.turn=0
- def _move(self,x,y):
- dirs=[(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1)]
- if (x<0) or (x>7) or (y<0) or (y>7) or self.board[y][x] <> 0:
- return []
- if not self.turn:
- p=1
- else:
- p=0
- res=[]
- for n in dirs:
- dx,dy=x+n[0],y+n[1]
- if (dx>=0) and (dx<8) and (dy>=0) and (dy<8) and (self.board[dy][dx]==p+1):
- res2=self._search(dx,dy,n,self.turn+1)
- if len(res2)>0:
- res+=res2
- return res
- def choose(self,x,y):
- res=self._move(x,y)
- if len(res)<1:
- return False
- for p in res:
- self.board[p[1]][p[0]]=self.turn+1
- self.board[y][x]=1+self.turn
- self.remain-=1
- return True
- def cpu_play(self):
- res=[]
- px,py=0,0
- for y in range(8):
- for x in range(8):
- if self.board[y][x]==0:
- res2=self._move(x,y)
- if len(res2)>len(res):
- res=res2
- px,py=x,y
- if len(res)<1:
- return False
- for p in res:
- self.board[p[1]][p[0]]=self.turn+1
- self.board[py][px]=1+self.turn
- self.remain-=1
- return True
- def get_skor(self):
- player,cpu=0,0
- for y in range(8):
- for x in range(8):
- if self.board[y][x]==1:
- player+=1
- elif self.board[y][x]==2:
- cpu+=1
- return player,cpu
- def _event(self,sc):
- k=sc['scancode']
- if not self.pause:
- if self.main_loop:
- if sc['type']==3 and k==164:
- self.illegal,self.cur_m,self.pause,self.info,self.hint=0,0,1,0,0
- if sc['type'] is not A.EEventKey:return
- self.illegal,self.info,self.hint=0,0,0
- if not self.over:
- if k in (0xf,0x36): # kanan
- self.curx+=1
- if self.curx>7:
- self.curx=0
- self.cury+=1
- if self.cury>7:
- self.cury=0
- self.curx=0
- elif k in (0xe,0x34): # kiri
- self.curx-=1
- if self.curx<0:
- self.curx=7
- self.cury-=1
- if self.cury<0:
- self.cury=7
- self.curx=7
- elif k in (0x10,0x32): # atas
- self.cury-=1
- if self.cury<0:
- self.cury=7
- elif k in (0x11,0x38): # bawah
- self.cury+=1
- if self.cury>7:
- self.cury=0
- elif k in (0xa7,0x35): # OK / 5
- if self.choose(self.curx,self.cury):
- self.turn=1
- self.illegal=0
- self.cpu_play()
- self.switch()
- else:
- self.illegal=1
- if k==0x30: # O pass
- self.turn=1
- self.cpu_play()
- self.switch()
- else: # Menu
- if sc['type']==3 and k in (164,165):
- if self.pause:
- self.pause=0
- return
- if sc['type'] is not A.EEventKey:return
- lem=len(self.list_menu)-1
- if k in (0x10,0x32): # atas
- self.cur_m-=1
- if self.cur_m<0:
- self.cur_m=lem
- elif k in (0x11,0x38): # bawah
- self.cur_m+=1
- if self.cur_m>lem:
- self.cur_m=0
- elif k in (0xa7,0x35): # OK / 5
- if callable(self.list_menu[self.cur_m][1]):
- self.pause=0
- self.list_menu[self.cur_m][1]()
- def exit(self):
- self.main_loop=0
- def new_game(self):
- self.board=[
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,2,1,0,0,0],
- [0,0,0,1,2,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0],
- [0,0,0,0,0,0,0,0]
- ]
- (self.turn,self.remain,self.curx,self.cury,self.cur_m)=self.old
- self.play()
- def help(self):
- self.hint=1
- def about(self):
- self.info=1
- def _redraw(self,rect):
- if self.img:
- self.c.blit(self.img)
- def _tsize(self,text,fnt):
- if not isinstance(text,unicode):
- text=unicode(text)
- m=self.img.measure_text(text,font=fnt)[0]
- w,h=(m[2]-m[0],m[3]-m[1])
- return (w,h)
- def draw_menu(self):
- x,y=self.w/2,self.h/2
- lem=len(self.list_menu)
- th=14
- rw,rh=60,(lem*th+10)/2
- self.img.rectangle((x-rw,y-rh,x+rw,y+rh),win_bordercolor,win_bgcolor)
- for i in range(len(self.list_menu)):
- col=(win_txtcolor,focus_color)[i==self.cur_m]
- self.img.text((x-rw+12,y-rh+th+(i*th)),u'%s' %self.list_menu[i][0],col)
- # pointer
- cy=y-rh+th+(self.cur_m*th)
- self.img.rectangle((x-rw+5,cy-6,x-rw+9,cy-2),fill=focus_color)
- def note(self,text):
- t=map(unicode,text)
- x,y=self.w/2,self.h/2
- lem,th=len(t),14
- rw,rh=60,(lem*th+10)/2
- self.img.rectangle((x-rw,y-rh,x+rw,y+rh),win_bordercolor,win_bgcolor)
- for i in range(lem):
- self.img.text((x-rw+5,y-rh+th+(i*th)),u'%s' %t[i],win_txtcolor)
- def intro(self):
- self.img.clear(16777215)
- for i in range(16777215,0,-1052688):
- self.img.clear(i)
- self._redraw(())
- A.e32.ao_sleep(0.05)
- for i in range(0,16776960,1052672):
- self.img.clear(0)
- self._redraw(())
- A.e32.ao_sleep(0.05)
- def play(self):
- self.main_loop,self.over=1,0
- px,py,width,pad=5,5,20,1
- while self.main_loop:
- self.img.clear(bgcolor)
- for y in range(8):
- for x in range(8):
- col=box_color
- if self.board[y][x]>0:
- col=[0xfefefe,0][self.board[y][x]-1]
- dx=px+(x*width)+(x*pad)
- dy=py+(y*width)+(y*pad)
- self.img.rectangle((dx,dy,dx+width,dy+width),fill=col)
- # kursor
- cx=px+(self.curx*width) + (self.curx*pad)
- cy=py+(self.cury*width) + (self.cury*pad)
- if not self.pause:
- self.img.rectangle((cx,cy,cx+width,cy+width),cur_color,width=2)
- if self.illegal:
- self.note(['Ow ow..', 'Gak bisa jalan disini!'])
- skor=self.get_skor()
- # skor player
- self.img.rectangle((px,self.h-28,px+8,self.h-20),fill=player_color)
- self.img.text((px+12,self.h-20),u'Player: %d' %skor[0],player_color)
- # skor AI CPU
- self.img.rectangle((px,self.h-14,px+8,self.h-6),fill=cpu_color)
- self.img.text((px+12,self.h-6),u'CPU: %d' %skor[1],cpu_color)
- if skor[0]+skor[1]==64 or skor[0]<1:
- self.over=1
- self.note([u'Game Over'])
- if self.hint:self.note(['Gunakan Joystik','atau 2, 4, 6, 8','untuk navigasi.','','5 > Memilih','0 > Skip Giliran'])
- if self.info:self.note(['Crazy Othelo','',u'\u00a9 2014, Joe','Some Rights Reserved'])
- if self.pause:self.draw_menu()
- if self.turn:
- if not self.cpu_play():
- self.turn=0
- self.switch()
- self.note(['Giliran Kamu'])
- self._redraw(())
- A.e32.ao_sleep(1E-08)
- game=Othelo()
- game.intro()
- game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement