如何用python turtle画一个中国象棋的棋盘?

Python017

如何用python turtle画一个中国象棋的棋盘?,第1张

#绘制棋盘,每个格子50

import turtle

t=turtle.Pen()

bs=50

#画直线

def line(x,y,z):

    t.penup()

    t.goto(x,y)

    t.pendown()

    t.fd(z)

#两点直线

def any(a,b,c,d):

    t.penup()

    t.goto(a,b)

    t.pendown()

    t.goto(c,d)

#画L型

def typeL(x,y):

    t.penup()

    t.goto(x-bs*0.25, y+bs*0.075)

    t.pendown()

    t.goto(x-bs*0.075, y+bs*0.075)

    t.goto(x - bs*0.075, y + bs*0.25)

    t.penup()

    t.goto(x - bs*0.25, y - bs*0.075)

    t.pendown()

    t.goto(x - bs*0.075, y - bs*0.075)

    t.goto(x - bs*0.075, y - bs*0.25)

    t.penup()

    t.goto(x+bs*0.25, y+bs*0.075)

    t.pendown()

    t.goto(x+bs*0.075, y+bs*0.075)

    t.goto(x + bs*0.075, y + bs*0.25)

    t.penup()

    t.goto(x + bs*0.25, y - bs*0.075)

    t.pendown()

    t.goto(x + bs*0.075, y - bs*0.075)

    t.goto(x + bs*0.075, y - bs*0.25)

#画半L型

def typehL(x,y,z):

    if(z=='l'):

        t.penup()

        t.goto(x-bs*0.25, y+bs*0.075)

        t.pendown()

        t.goto(x-bs*0.075, y+bs*0.075)

        t.goto(x - bs*0.075, y + bs*0.25)

        t.penup()

        t.goto(x - bs*0.25, y - bs*0.075)

        t.pendown()

        t.goto(x - bs*0.075, y - bs*0.075)

        t.goto(x - bs*0.075, y - bs*0.25)

    if(z=='r'):

        t.penup()

        t.goto(x + bs*0.25, y + bs*0.075)

        t.pendown()

        t.goto(x + bs*0.075, y + bs*0.075)

        t.goto(x + bs*0.075, y + bs*0.25)

        t.penup()

        t.goto(x + bs*0.25, y - bs*0.075)

        t.pendown()

        t.goto(x + bs*0.075, y - bs*0.075)

        t.goto(x + bs*0.075, y - bs*0.25)

#画横线

p=bs*4.5

while(p>=-bs*4.5):

    line(-bs*4,p,bs*8)

    p=p-bs

any(bs*4,bs*4.5,bs*4,-bs*4.5)

any(-bs*4,bs*4.5,-bs*4,-bs*4.5)

t.right(90)

q=-bs*3

while(q<bs*4):

    line(q,bs*4.5,bs*4)

    q=q+bs

q=-bs*3

while(q<bs*4):

    line(q,-bs*0.5,bs*4)

    q=q+bs

#画斜线

any(-bs,-bs*4.5,bs,-bs*2.5)

any(bs,-bs*4.5,-bs,-bs*2.5)

any(-bs,bs*4.5,bs,bs*2.5)

any(bs,bs*4.5,-bs,bs*2.5)

#画L型

typeL(-bs*2,-bs*1.5)

typeL(0,-bs*1.5)

typeL(bs*2,-bs*1.5)

typeL(-bs*2,bs*1.5)

typeL(0,bs*1.5)

typeL(bs*2,bs*1.5)

typeL(-bs*3,-bs*2.5)

typeL(bs*3,-bs*2.5)

typeL(-bs*3,bs*2.5)

typeL(bs*3,bs*2.5)

typehL(-bs*4,-bs*1.5,'r')

typehL(bs*4,-bs*1.5,'l')

typehL(-bs*4,bs*1.5,'r')

typehL(bs*4,bs*1.5,'l')

turtle.done()

if(x<5&&y<5){

if(y>x){

fill(0)

]

if(x<5&&y>5)

......跟之前思路一样。

第四幅图可以用X+1%2=0 这种形式。

反正很简单啦~

BTW,你的语言习惯不太好。。。

#include <stdio.h>#include <stdlib.h>void show(int n,int i,int j)int main(){int n,i,jscanf("%d %d %d",&n,&i,&j)if(1<=i&&i<=n&&1<=j&&j<=n)show(n,i,j)return 0}void show(int n,int i,int j){int a,b,cb=ic=jfor(a=1a<=na++)//输出同一行上的位置{printf("(%d,%d)",i,a)}printf("\n")for(a=1a<=na++)//输出同一列上的位置{printf("(%d,%d)",a,j)}printf("\n")while(i>0&&j>0)//左上到右下对角线上的格子的位置{i--j--}i++j++while(i<=n&&j<=n){printf("(%d,%d)",i,j)i++j++}printf("\n")i=bj=cwhile(i<=n&&j>0)//左下到右上对角线上的格子的位置{i++j--}i--j++while(i<=n&&j<=n&&i>0&&j>0){printf("(%d,%d)",i,j)i--j++}}