python 바둑 게임 실현(순수 tkinter gui)
이 프로그램 은 tkinter 를 기반 으로 GUI 를 생 성 합 니 다.사용 하기 전에 tkinter 가 설치 되 어 있 는 지 확인 하 십시오.
windows 사용자 에 게 는 tkinter 가 설치 되 어 있 을 수 있 습 니 다.cmd 에 python-c'import tkinter'를 입력 하 십시오.오류 가 없 으 면 tkinter 가 설치 되 어 있 습 니 다.
Liux 사용자 에 게 는 tkinter 가 설치 되 어 있 지 않 을 수 있 습 니 다.패키지 관리 도 구 를 사용 하여 tkinter 를 검색 할 수 있 습 니 다.예 를 들 어 apt search tkinter,pacman-ss tkinter,yum search tkinter,요구 에 맞 는 패 키 지 를 선택 한 다음 설치 합 니 다.찾 지 못 하면 원본 코드 를 다운로드 하고 컴 파일 하여 설치 하 십시오.
2.전체 코드
#!/usr/bin/python3
# Python GUI tkinter
from tkinter import *
# ttk tkinter ,ttk tkinter
from tkinter.ttk import *
# copy
import copy
import tkinter.messagebox
#
class Application(Tk):
# ,
def __init__(self,my_mode_num=9):
Tk.__init__(self)
# , :9, :13, :19
self.mode_num=my_mode_num
# , :1.8
self.size=1.8
#
self.dd=360*self.size/(self.mode_num-1)
#
self.p=1 if self.mode_num==9 else (2/3 if self.mode_num==13 else 4/9)
# , :-1, :0, :1, :2
self.positions=[[0 for i in range(self.mode_num+2)] for i in range(self.mode_num+2)]
# , -1
for m in range(self.mode_num+2):
for n in range(self.mode_num+2):
if (m*n==0 or m==self.mode_num+1 or n==self.mode_num+1):
self.positions[m][n]=-1
# “ ”, “ ”
self.last_3_positions=copy.deepcopy(self.positions)
self.last_2_positions=copy.deepcopy(self.positions)
self.last_1_positions=copy.deepcopy(self.positions)
# , shadow
self.cross_last=None
# , :0, :1,
self.present=0
# , “ ”
self.stop=True
# , 0 , 0( ), 0, 1,
self.regretchance=0
# , /Pictures/
self.photoW=PhotoImage(file = "./Pictures/W.png")
self.photoB=PhotoImage(file = "./Pictures/B.png")
self.photoBD=PhotoImage(file = "./Pictures/"+"BD"+"-"+str(self.mode_num)+".png")
self.photoWD=PhotoImage(file = "./Pictures/"+"WD"+"-"+str(self.mode_num)+".png")
self.photoBU=PhotoImage(file = "./Pictures/"+"BU"+"-"+str(self.mode_num)+".png")
self.photoWU=PhotoImage(file = "./Pictures/"+"WU"+"-"+str(self.mode_num)+".png")
#
self.photoWBU_list=[self.photoBU,self.photoWU]
self.photoWBD_list=[self.photoBD,self.photoWD]
#
self.geometry(str(int(600*self.size))+'x'+str(int(400*self.size)))
# ,
self.canvas_bottom=Canvas(self,bg='#369',bd=0,width=600*self.size,height=400*self.size)
self.canvas_bottom.place(x=0,y=0)
#
self.startButton=Button(self,text=' ',command=self.start)
self.startButton.place(x=480*self.size,y=200*self.size)
self.passmeButton=Button(self,text=' ',command=self.passme)
self.passmeButton.place(x=480*self.size,y=225*self.size)
self.regretButton=Button(self,text=' ',command=self.regret)
self.regretButton.place(x=480*self.size,y=250*self.size)
#
self.regretButton['state']=DISABLED
self.replayButton=Button(self,text=' ',command=self.reload)
self.replayButton.place(x=480*self.size,y=275*self.size)
self.newGameButton1=Button(self,text=(' ' if self.mode_num==9 else ' ')+' ',command=self.newGame1)
self.newGameButton1.place(x=480*self.size,y=300*self.size)
self.newGameButton2=Button(self,text=(' ' if self.mode_num==19 else ' ')+' ',command=self.newGame2)
self.newGameButton2.place(x=480*self.size,y=325*self.size)
self.quitButton=Button(self,text=' ',command=self.quit)
self.quitButton.place(x=480*self.size,y=350*self.size)
# ,
self.canvas_bottom.create_rectangle(0*self.size,0*self.size,400*self.size,400*self.size,fill='#c51')
#
#
self.canvas_bottom.create_rectangle(20*self.size,20*self.size,380*self.size,380*self.size,width=3)
# , , ,
for m in [-1,0,1]:
for n in [-1,0,1]:
self.oringinal=self.canvas_bottom.create_oval(200*self.size-self.size*2,200*self.size-self.size*2,
200*self.size+self.size*2,200*self.size+self.size*2,fill='#000')
self.canvas_bottom.move(self.oringinal,m*self.dd*(2 if self.mode_num==9 else (3 if self.mode_num==13 else 6)),
n*self.dd*(2 if self.mode_num==9 else (3 if self.mode_num==13 else 6)))
#
for i in range(1,self.mode_num-1):
self.canvas_bottom.create_line(20*self.size,20*self.size+i*self.dd,380*self.size,20*self.size+i*self.dd,width=2)
self.canvas_bottom.create_line(20*self.size+i*self.dd,20*self.size,20*self.size+i*self.dd,380*self.size,width=2)
#
self.pW=self.canvas_bottom.create_image(500*self.size+11, 65*self.size,image=self.photoW)
self.pB=self.canvas_bottom.create_image(500*self.size-11, 65*self.size,image=self.photoB)
# image , reload
self.canvas_bottom.addtag_withtag('image',self.pW)
self.canvas_bottom.addtag_withtag('image',self.pB)
# , shadow ,
self.canvas_bottom.bind('<Motion>',self.shadow)
# , getdown ,
self.canvas_bottom.bind('<Button-1>',self.getDown)
# <Ctrl>+<D>,
self.bind('<Control-KeyPress-d>',self.keyboardQuit)
# , “ ”
def start(self):
#
self.canvas_bottom.delete(self.pW)
self.canvas_bottom.delete(self.pB)
#
if self.present==0:
self.create_pB()
self.del_pW()
else:
self.create_pW()
self.del_pB()
# , stop
self.stop=None
# ,
def passme(self):
#
if not self.regretchance==1:
self.regretchance+=1
else:
self.regretButton['state']=NORMAL
# ,
self.last_3_positions=copy.deepcopy(self.last_2_positions)
self.last_2_positions=copy.deepcopy(self.last_1_positions)
self.last_1_positions=copy.deepcopy(self.positions)
self.canvas_bottom.delete('image_added_sign')
#
if self.present==0:
self.create_pW()
self.del_pB()
self.present=1
else:
self.create_pB()
self.del_pW()
self.present=0
# , ,
def regret(self):
# ,
if self.regretchance==1:
self.regretchance=0
self.regretButton['state']=DISABLED
list_of_b=[]
list_of_w=[]
self.canvas_bottom.delete('image')
if self.present==0:
self.create_pB()
else:
self.create_pW()
for m in range(1,self.mode_num+1):
for n in range(1,self.mode_num+1):
self.positions[m][n]=0
for m in range(len(self.last_3_positions)):
for n in range(len(self.last_3_positions[m])):
if self.last_3_positions[m][n]==1:
list_of_b+=[[n,m]]
elif self.last_3_positions[m][n]==2:
list_of_w+=[[n,m]]
self.recover(list_of_b,0)
self.recover(list_of_w,1)
self.last_1_positions=copy.deepcopy(self.last_3_positions)
for m in range(1,self.mode_num+1):
for n in range(1,self.mode_num+1):
self.last_2_positions[m][n]=0
self.last_3_positions[m][n]=0
# , , , , “ ”
def reload(self):
if self.stop==1:
self.stop=0
self.canvas_bottom.delete('image')
self.regretchance=0
self.present=0
self.create_pB()
for m in range(1,self.mode_num+1):
for n in range(1,self.mode_num+1):
self.positions[m][n]=0
self.last_3_positions[m][n]=0
self.last_2_positions[m][n]=0
self.last_1_positions[m][n]=0
#
def create_pW(self):
self.pW=self.canvas_bottom.create_image(500*self.size+11, 65*self.size,image=self.photoW)
self.canvas_bottom.addtag_withtag('image',self.pW)
def create_pB(self):
self.pB=self.canvas_bottom.create_image(500*self.size-11, 65*self.size,image=self.photoB)
self.canvas_bottom.addtag_withtag('image',self.pB)
def del_pW(self):
self.canvas_bottom.delete(self.pW)
def del_pB(self):
self.canvas_bottom.delete(self.pB)
#
def shadow(self,event):
if not self.stop:
# , ,
if (20*self.size<event.x<380*self.size) and (20*self.size<event.y<380*self.size):
dx=(event.x-20*self.size)%self.dd
dy=(event.y-20*self.size)%self.dd
self.cross=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)*self.dd+22*self.p, event.y-dy+round(dy/self.dd)*self.dd-27*self.p,image=self.photoWBU_list[self.present])
self.canvas_bottom.addtag_withtag('image',self.cross)
if self.cross_last!=None:
self.canvas_bottom.delete(self.cross_last)
self.cross_last=self.cross
# ,
def getDown(self,event):
if not self.stop:
#
if (20*self.size-self.dd*0.4<event.x<self.dd*0.4+380*self.size) and (20*self.size-self.dd*0.4<event.y<self.dd*0.4+380*self.size):
dx=(event.x-20*self.size)%self.dd
dy=(event.y-20*self.size)%self.dd
x=int((event.x-20*self.size-dx)/self.dd+round(dx/self.dd)+1)
y=int((event.y-20*self.size-dy)/self.dd+round(dy/self.dd)+1)
#
if self.positions[y][x]==0:
# , ,
self.positions[y][x]=self.present+1
self.image_added=self.canvas_bottom.create_image(event.x-dx+round(dx/self.dd)*self.dd+4*self.p, event.y-dy+round(dy/self.dd)*self.dd-5*self.p,image=self.photoWBD_list[self.present])
self.canvas_bottom.addtag_withtag('image',self.image_added)
# , “ ”
self.canvas_bottom.addtag_withtag('position'+str(x)+str(y),self.image_added)
deadlist=self.get_deadlist(x,y)
self.kill(deadlist)
#
if not self.last_2_positions==self.positions:
#
if len(deadlist)>0 or self.if_dead([[x,y]],self.present+1,[x,y])==False:
# , ,
if not self.regretchance==1:
self.regretchance+=1
else:
self.regretButton['state']=NORMAL
self.last_3_positions=copy.deepcopy(self.last_2_positions)
self.last_2_positions=copy.deepcopy(self.last_1_positions)
self.last_1_positions=copy.deepcopy(self.positions)
# ,
self.canvas_bottom.delete('image_added_sign')
self.image_added_sign=self.canvas_bottom.create_oval(event.x-dx+round(dx/self.dd)*self.dd+0.5*self.dd, event.y-dy+round(dy/self.dd)*self.dd+0.5*self.dd,event.x-dx+round(dx/self.dd)*self.dd-0.5*self.dd, event.y-dy+round(dy/self.dd)*self.dd-0.5*self.dd,width=3,outline='#3ae')
self.canvas_bottom.addtag_withtag('image',self.image_added_sign)
self.canvas_bottom.addtag_withtag('image_added_sign',self.image_added_sign)
if self.present==0:
self.create_pW()
self.del_pB()
self.present=1
else:
self.create_pB()
self.del_pW()
self.present=0
else:
# , ,
self.positions[y][x]=0
self.canvas_bottom.delete('position'+str(x)+str(y))
self.bell()
self.showwarningbox(' '," !")
else:
# ,
self.positions[y][x]=0
self.canvas_bottom.delete('position'+str(x)+str(y))
self.recover(deadlist,(1 if self.present==0 else 0))
self.bell()
self.showwarningbox(" "," !")
else:
# ,
self.bell()
else:
# ,
self.bell()
# ( yourChessman, yourPosition) ( ), False,
# , deadlist , , yourPosition , , False ;
# , ( deadlist ) , , , deadlist, , , False ;
# , deadlist,
# def if_dead(self,deadlist,yourChessman,yourPosition):
def if_dead(self,deadList,yourChessman,yourPosition):
for i in [-1,1]:
if [yourPosition[0]+i,yourPosition[1]] not in deadList:
if self.positions[yourPosition[1]][yourPosition[0]+i]==0:
return False
if [yourPosition[0],yourPosition[1]+i] not in deadList:
if self.positions[yourPosition[1]+i][yourPosition[0]]==0:
return False
if ([yourPosition[0]+1,yourPosition[1]] not in deadList) and (self.positions[yourPosition[1]][yourPosition[0]+1]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0]+1,yourPosition[1]]],yourChessman,[yourPosition[0]+1,yourPosition[1]])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
if ([yourPosition[0]-1,yourPosition[1]] not in deadList) and (self.positions[yourPosition[1]][yourPosition[0]-1]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0]-1,yourPosition[1]]],yourChessman,[yourPosition[0]-1,yourPosition[1]])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
if ([yourPosition[0],yourPosition[1]+1] not in deadList) and (self.positions[yourPosition[1]+1][yourPosition[0]]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0],yourPosition[1]+1]],yourChessman,[yourPosition[0],yourPosition[1]+1])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
if ([yourPosition[0],yourPosition[1]-1] not in deadList) and (self.positions[yourPosition[1]-1][yourPosition[0]]==yourChessman):
midvar=self.if_dead(deadList+[[yourPosition[0],yourPosition[1]-1]],yourChessman,[yourPosition[0],yourPosition[1]-1])
if not midvar:
return False
else:
deadList+=copy.deepcopy(midvar)
return deadList
# ,
def showwarningbox(self,title,message):
self.canvas_bottom.delete(self.cross)
tkinter.messagebox.showwarning(title,message)
# , ,
def get_deadlist(self,x,y):
deadlist=[]
for i in [-1,1]:
if self.positions[y][x+i]==(2 if self.present==0 else 1) and ([x+i,y] not in deadlist):
killList=self.if_dead([[x+i,y]],(2 if self.present==0 else 1),[x+i,y])
if not killList==False:
deadlist+=copy.deepcopy(killList)
if self.positions[y+i][x]==(2 if self.present==0 else 1) and ([x,y+i] not in deadlist):
killList=self.if_dead([[x,y+i]],(2 if self.present==0 else 1),[x,y+i])
if not killList==False:
deadlist+=copy.deepcopy(killList)
return deadlist
# list_to_recover b_or_w
def recover(self,list_to_recover,b_or_w):
if len(list_to_recover)>0:
for i in range(len(list_to_recover)):
self.positions[list_to_recover[i][1]][list_to_recover[i][0]]=b_or_w+1
self.image_added=self.canvas_bottom.create_image(20*self.size+(list_to_recover[i][0]-1)*self.dd+4*self.p, 20*self.size+(list_to_recover[i][1]-1)*self.dd-5*self.p,image=self.photoWBD_list[b_or_w])
self.canvas_bottom.addtag_withtag('image',self.image_added)
self.canvas_bottom.addtag_withtag('position'+str(list_to_recover[i][0])+str(list_to_recover[i][1]),self.image_added)
# killList , , 0
def kill(self,killList):
if len(killList)>0:
for i in range(len(killList)):
self.positions[killList[i][1]][killList[i][0]]=0
self.canvas_bottom.delete('position'+str(killList[i][0])+str(killList[i][1]))
#
def keyboardQuit(self,event):
self.quit()
# ,newApp ,
def newGame1(self):
global mode_num,newApp
mode_num=(13 if self.mode_num==9 else 9)
newApp=True
self.quit()
def newGame2(self):
global mode_num,newApp
mode_num=(13 if self.mode_num==19 else 19)
newApp=True
self.quit()
# , Application
global mode_num,newApp
mode_num=9
newApp=False
if __name__=='__main__':
# ,
while True:
newApp=False
app=Application(mode_num)
app.title(' ')
app.mainloop()
if newApp:
app.destroy()
else:
break
3.실행 효과이상 은 python 이 바둑 게임 을 실현 하 는 상세 한 내용 입 니 다.python 바둑 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.