[tkinter] 三子棋游戏
双方各三子,轮流走子。不能吃子,只能走到空位。将对方三子全逼到上部圆内为胜。如一方无棋可走(没有被逼到圆内),另一方要退一步,让对方走棋。本代码还未实现这个功能。
from tkinter import *
from tkinter import messagebox
root=Tk()
root.title("三子棋")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
w=500
h=600
x=(screen_width-w)/2
y=(screen_height-h)/2
root.geometry('%dx%d+%d+%d' % (w,h,x,y))
cv=Canvas(root,width=500,height=500,bg='#008B8B')
chessname=['红','黑']
chessmap=[[-1,-1,-1] for x in range(3)]
dict_ChessName={}
LocalPlayer='红'
first=True
rect1=0
rect2=0
player=0
def DrawBoard():
for i in range(0, 3):
cv.create_oval(100+160*i-50,50,100+160*i+50,150, width=5,outline='#C0C0C0')
cv.create_line(100+160*i,150, 100+160*i, 420, width=5,fill='#C0C0C0')
cv.create_line(100 , 260, 420,260, width=5,fill='#C0C0C0')
cv.create_line(100, 420, 420, 420, width=5,fill='#C0C0C0')
def LoadChess():
global chessmap
for i in range(0,3):
id=cv.create_oval(70,70 + 160 * i , 130, 130 + 160 * i,fill='#FF7F50',outline='#FF7F50')
chessmap=id
dict_ChessName = '红'
print('d0=', dict_ChessName)
for i in range(0,3):
id = cv.create_oval(390,70 + 160 * i, 450, 130 + 160 * i , fill='black')
chessmap=id
dict_ChessName = '黑'
print('d=',dict_ChessName)
def play(event):
global LocalPlayer,player,player1 ,player2 ,player3 ,chessmap,rect1,rect2,x1,x2,y1,y2,first
player1 = chessmap
player2 = chessmap
player3 = chessmap
x=(event.x-20)//160
y=(event.y-20)//160
cv.delete(rect2)
if first:
x1=x
y1=y
player=chessmap
if not(chessmap==-1):
print('第一次单击')
first=False
rect1=cv.create_rectangle(160*x1+100-40,y1*160+100-40,160*x1+100+40,y1*160+100+40,outline='red')
else:
x2=x
y2=y
print(x1,y1,x2,y2)
if x2==x1 and y2==y1:
myMsg3()
print('取消第一次单击')
cv.delete(rect1)
first = True
return
if not(chessmap==-1):
myMsg2()
print('只能走到空位!')
return None
if IsAbleToPut(x1,x2,y1,y2):
rect2 = cv.create_rectangle(160 * x2 + 100 - 40, y2 * 160 + 100 - 40, 160 * x2 + 100 + 40,
y2 * 160 + 100 + 40, outline='yellow')
cv.move(player,160*(x2-x1),160*(y2-y1))
chessmap=-1
chessmap=player
print('chessmap=',chessmap)
print('chessmap=',chessmap)
cv.delete(rect1)
if game_over():
root.destroy()
first=True
SetMyTurn(False)
return
else:
return None
def game_over():
try:
if dict_ChessName==dict_ChessName and dict_ChessName==dict_ChessName :
if dict_ChessName == '黑':
label1['text'] = '红方胜利!'
myMsg4()
return True
elif dict_ChessName == '红':
label1['text'] = '黑方胜利!'
myMsg5()
return True
except KeyError :
pass
else:
return False
def SetMyTurn(flag):
global LocalPlayer
IsMyTurn=flag
if LocalPlayer=='红':
LocalPlayer='黑'
label1['text']='轮到黑方走'
else:
LocalPlayer='红'
label1['text'] = '轮到红方走'
def IsAbleToPut(x1,x2,y1,y2):
if ((x2-x1)*(y2-y1)!=0):
return False
if(abs(x2-x1)>1 or abs(y2-y1)>1):
return False
if (x2<0 or x2>2 or y2<0 or y2>2):
return False
if x1==0 and y1==0 and x2==1 and y2==0:
myMsg1()
print('此路不通!')
return False
if x1==2 and y1==0 and x2==1 and y2==0:
myMsg1()
print('此路不通!')
return False
if x1==1 and y1==0 and x2==0 andy2==0:
myMsg1()
print('此路不通!')
return False
ifx1==1 and y1==0 and x2==2 and y2==0:
myMsg1()
print('此路不通!')
return False
return True
def myMsg1():
messagebox.showerror('错误!','此路不通!')
def myMsg2():
messagebox.showerror('错误!','只能走到空位!')
def myMsg3():
messagebox.showinfo('提示','取消第一次单击!')
def myMsg4():
messagebox.showinfo('提示','红方胜利!')
def myMsg5():
messagebox.showinfo('提示','黑方胜利!')
DrawBoard()
LoadChess()
cv.bind('<Button-1>',play)
cv.pack()
label1=Label(root,fg='red',bg='white',text='红方先走', font=("华文行楷", 20))
label1.pack()
label2=Label(root,fg='blue',bg='white',text='走法:不能吃子,一方三子全被逼到上面圆里为输。', font=("kaiti", 10))
label2.pack()
root.mainloop() 沙发 {:10_256:} @FishC, 能将上面游戏改为人机对战吗? 到这个水平需要多久 初级水平,好玩而已
页:
[1]