五子棋练习(二)

这是主程序的部分。

到这里已经可以实现黑白轮流落子了。

import chess
import tkinter as tk
import time

#生成窗口和画布
root=tk.Tk()
cv=tk.Canvas(root,width=980,height=700,bg='#C09900')
cv.create_text(700,35,text='五子棋')
cv.pack()

player1=chess.Player('black')
player2=chess.Player('white')

#绘制棋盘线条
for i in chess.locations:
	cv.create_line(i[0]*35,35,i[0]*35,665)
	cv.create_line(35,i[1]*35,665,i[1]*35)
cv.create_rectangle(35,35,665,665,width=3)

#绘制9个角边点
for i in (4,10,16):
	for j in (4,10,16):
		cv.create_oval(i*35-5,j*35-5,i*35+5,j*35+5,fill='black')

null_location=chess.Chess()
chessborad=[null_location,] #初始棋盘为空


player1.token=1 #初始令牌给棋手1
player2.token=0 
player=player1

def match(event):
	if player1.token==1:
		player=player1
	elif player2.token==1:
		player=player2
	location=chess.get_location(event) #获取当前的鼠标位置
	index=0
	
	#当前的位置已经落子,则没有动作
	if location not in (player1.locations + player2.locations):
		piece=chess.Pieces(player.color,location,cv) #生成一个新的棋子
		chess.Pieces.choose_piont(piece) #将棋子放在棋盘
		player.locations.append(location) #将棋子的位置放在棋手的落子记录中
		player.pionts+=1 #棋手的落子数量加1
		player1.token,player2.token=player2.token,player1.token #两个棋手交换令牌
		player.last_piece=piece #记录为棋手的最后落下的棋子。
		newlocation=chess.Chess() #生成一个新的棋盘点
		newlocation.location=location #棋盘点确定位置
		newlocation.state=piece #棋盘点记录棋子
		index=index+1
		newlocation.index=index #记录当前位置的手数
		chessborad.append(newlocation) #将落子点的信息汇集到棋盘中
		print(player.color,'已经在这些点落子:',player.locations)
		print(player.color,'已经落子的数量是:',player.pionts)
		print(player.color,'最后的一个棋子是:',player.last_piece)

cv.bind('<Button-1>',match)
root.mainloop()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值