#五子棋——爱心棋子版
#构建棋盘布局
import random
from turtle import*
from math import*
setup(560,560,500,200)
title("五子棋——爱心棋子版")
tracer(0)
pensize(2)
penup()
goto(-280,-280)
pendown()
bgcolor("chocolate")
color('black','chocolate')
begin_fill()
for i in range(4):
fd(560)
left(90)
for j in range(7):
fd(560)
left(90)
fd(40)
left(90)
fd(560)
right(90)
fd(40)
right(90)
for k in range(7):
fd(40)
right(90)
fd(560)
left(90)
fd(40)
left(90)
fd(560)
right(90)
right(90)
fd(560)
right(90)
fd(560)
end_fill()
penup() #画标点
goto(0,0)
dot(15,"black")
goto(160,160)
dot(15,"black")
goto(-160,160)
dot(15,"black")
goto(160,-160)
dot(15,"black")
goto(-160,-160)
dot(15,"black")
ht()
#修改过
#利用列表创建一个二维数组,使用生成器来辅助实现。
arr=[[0 for i in range(15)] for j in range(15)]
#对五子是否相连的判断
def fun_null(x,y):
return ;
def black_is_end(i,j):
global arr
for m in range(11): #竖向
if arr[m][j]==1 and arr[m+1][j]==1 and arr[m+2][j]==1 and \
arr[m+3][j]==1 and arr[m+4][j]==1:
print("游戏结束,金心方胜利!")
onscreenclick(fun_null)
for m in range(11): #横向
if arr[i][m]==1 and arr[i][m+1]==1 and arr[i][m+2]==1 and \
arr[i][m+3]==1 and arr[i][m+4]==1:
print("游戏结束,金心方胜利!")
onscreenclick(fun_null)
if i<=j: #\向
m=j-i
for k in range(m,11):
if arr[k-m][k]==1 and arr[k-m+1][k+1]==1 and arr[k-m+2][k+2]==1 and \
arr[k-m+3][k+3]==1 and arr[k-m+4][k+4]==1:
print("游戏结束,金心方胜利!")
onscreenclick(fun_null)
if i>j: #\向
m=i-j
for k in range(m,11):
if arr[k][k-m]==1 and arr[k+1][k-m+1]==1 and arr[k+2][k-m+2]==1 and \
arr[k+3][k-m+3]==1 and arr[k+4][k-m+4]==1:
print("游戏结束,金心方胜利!")
onscreenclick(fun_null)
n=i+j #/向
if n<=14:
for k in range(0,n-4):
if arr[k][n-k]==1 and arr[k+1][n-k-1]==1 and arr[k+2][n-k-2]==1 and \
arr[k+3][n-k-3]==1 and arr[k+4][n-k-4]==1:
print("游戏结束,金心方胜利!")
onscreenclick(fun_null)
if n>14:
for v in range(n-14,11):
if arr[v][n-v]==1 and arr[v+1][n-v-1]==1 and arr[v+2][n-v-2]==1 and \
arr[v+3][n-v-3]==1 and arr[v+4][n-v-4]==1:
print("游戏结束,金心方胜利!")
onscreenclick(fun_null)
def fun_null(x,y):
return ;
def white_is_end(i,j):
global arr
for m in range(11): #竖向
if arr[m][j]==-1 and arr[m+1][j]==-1 and arr[m+2][j]==-1 and \
arr[m+3][j]==-1 and arr[m+4][j]==-1:
print("游戏结束,红心方胜利!")
onscreenclick(fun_null)
for m in range(11): #横向
if arr[i][m]==-1 and arr[i][m+1]==-1 and arr[i][m+2]==-1 and \
arr[i][m+3]==-1 and arr[i][m+4]==-1:
print("游戏结束,红心方胜利!")
onscreenclick(fun_null)
if i<=j: #\向
m=j-i
for k in range(m,11):
if arr[k-m][k]==-1 and arr[k-m+1][k+1]==-1 and arr[k-m+2][k+2]==-1 and \
arr[k-m+3][k+3]==-1 and arr[k-m+4][k+4]==-1:
print("游戏结束,红心方胜利!")
onscreenclick(fun_null)
if i>j: #\向
m=i-j
for k in range(m,11):
if arr[k][k-m]==-1 and arr[k+1][k-m+1]==-1 and arr[k+2][k-m+2]==-1 and \
arr[k+3][k-m+3]==-1 and arr[k+4][k-m+4]==-1:
print("游戏结束,红心方胜利!")
onscreenclick(fun_null)
n=i+j #/向
if n<=14:
for k in range(0,n-4):
if arr[k][n-k]==-1 and arr[k+1][n-k-1]==-1 and arr[k+2][n-k-2]==-1 and \
arr[k+3][n-k-3]==-1 and arr[k+4][n-k-4]==-1:
print("游戏结束,红心方胜利!")
onscreenclick(fun_null)
if n>14:
for v in range(n-14,11):
if arr[v][n-v]==-1 and arr[v+1][n-v-1]==-1 and arr[v+2][n-v-2]==-1 and \
arr[v+3][n-v-3]==-1 and arr[v+4][n-v-4]==-1:
print("游戏结束,红心方胜利!")
onscreenclick(fun_null)
#使用鼠标点击落子
turn=0 #记录该哪方落子
def funclick(x,y):
global turn
global arr
if turn==0: #金心方先下1颗
for i in range(15):
Y=280-i*40
for j in range(15):
X=-280+j*40 #棋盘上的整点
if abs(X-x)<20 and abs(Y-y)<20 and arr[i][j]==0:
arr[i][j]=1
#设置金心方棋子形态:颜色,大小,位置
goto(X,Y-20)
color('gold','gold')
begin_fill()
right(135)
pendown()
fd(20)
circle(10,180)
right(90)
circle(10,180)
fd(20)
left(225)
penup()
end_fill()
turn=1 #交替下子
black_is_end(i,j)
if turn==1: #红心方棋子-1
for i in range(15):
Y=280-i*40
for j in range(15):
X=-280+j*40 #棋盘上的整点
if abs(X-x)<20 and abs(Y-y)<20 and arr[i][j]==0:
arr[i][j]=-1
#设置红心方棋子形态:颜色,大小,位置
goto(X,Y-20)
color('white','red')
begin_fill()
right(135)
pendown()
fd(20)
circle(10,180)
right(90)
circle(10,180)
fd(20)
left(225)
penup()
end_fill()
turn=0 #交替下子
white_is_end(i,j)
onscreenclick(funclick)
#tracer()为追踪器,tracer(500,100)代表延迟100毫秒展示500个画面,tracer(0)中只有一个数字时,系统默认0为延迟毫秒数值
#添加标点的原因是我方便利用标点建立坐标系,减小部分编码难度。大家可根据自身需要对“画标点”的代码区域自行修改,相信以大家富有灵气的大脑和双手一定可以让代码的运行效果完美符合自身预期。