Python——绘制象棋

import math
from turtle import *

speed(0)   #调整画的速度 1-10,数字越大,速度越快
width=50
hideturtle()     #隐藏画笔的形状a

#绘制棋盘
for i in range(5):
    forward(8*width)
    penup()
    goto(0,(i+1)*width)
    pendown()
left(90)
for i in range(9):
    penup()
    goto(i*width,0)
    pendown()
    forward(4*width)

for i in range(9):
    penup()
    goto(i*width,-width)
    pendown()
    forward(-4*width)
left(270)
for i in range(5):
    penup()
    goto(0,-width*(i+1))
    pendown()
    forward(8*width)

penup()
goto(-10,4*width+10)
pendown()
right(90)
for i in range(2):
    forward(9*width+20)
    left(90)
    forward(8*width+20)
    left(90)

#田线
penup()
goto(3*width,4*width)
pendown()
left(45)
fd(2*math.sqrt(5000))
penup()
goto(3*width,2*width)
pendown()
left(90)
fd(2*math.sqrt(5000))

penup()
goto(3*width,-5*width)
pendown()
fd(2*math.sqrt(5000))
penup()
goto(3*width,-3*width)
pendown()
right(90)
fd(2*math.sqrt(5000))


#绘制炮兵标记
def fun(x,y):
    penup()
    home()
    goto(x*width,y*width+3)
    fd(-6)
    pendown()
    for i in range(4):
        fd(3)
        left(90)
        fd(3)
        penup()
        right(90)
        fd(6)
        pendown()
        right(90)
for i in range(5):
    fun(i*2,1)
for i in range(5):
    fun(i*2,-2)
fun(1,2)
fun(7,2)
fun(1,-3)
fun(7,-3)

def write_chees(str,color1):  #棋字
    color(color1)
    write(str,font=('隶书',width//2,'normal'))
def drawcircle(radius):   #画圆
    pensize(3)
    begin_fill()
    fillcolor('white')
    circle(radius)
    end_fill()

def chees(x,y,str,color):  #绘制棋子
    penup()
    home()
    goto(x*width,y*width)
    right(90)
    fd(1/3*width)
    left(90)
    pendown()
    pencolor(color)
    drawcircle(1/3*width)
    penup()
    fd(-1/3*width)
    pendown()
    write_chees(str,color)

red='red'
black='black'
#红棋子
for i in range(5):
    chees(i*2,1,'兵','red')
chees(1,2,'炮',red)
chees(7,2,'炮',red)
chees(0,4,'車',red)
chees(1,4,'馬',red)
chees(2,4,'相',red)
chees(3,4,'仕',red)
chees(4,4,'帅',red)
chees(5,4,'仕',red)
chees(6,4,'相',red)
chees(7,4,'馬',red)
chees(8,4,'車',red)
#黑棋子
for i in range(5):
    chees(i*2,-2,'卒','black')
chees(1,-3,'炮',black)
chees(7,-3,'炮',black)
chees(0,-5,'車',black)
chees(1,-5,'馬',black)
chees(2,-5,'象',black)
chees(3,-5,'士',black)
chees(4,-5,'将',black)
chees(5,-5,'士',black)
chees(6,-5,'象',black)
chees(7,-5,'馬',black)
chees(8,-5,'車',black)
done()

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值