python画国际象棋_python3 turtle 画国际象棋棋盘

python3 turtle 画国际象棋棋盘

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:Hiuhung Wan

import turtle

n = 60 # 每行间隔

x = -300 # x初始值

y = -300 # x初始值

turtle.speed(11)

turtle.pensize(2)

# 先画8*8的正方形,并按要求涂黑

for i in range(8):

for j in range(1, 9):

turtle.penup()

turtle.goto(x + i * n, y + j * n )

turtle.pendown()

if (i + j) % 2 == 1: #不涂黑

for index in range(4):

turtle.forward(n)

turtle.left(90)

elif (i + j) % 2 == 0: #涂黑

turtle.begin_fill()

turtle.fillcolor('black')

for index in range(4):

turtle.forward(n)

turtle.left(90)

turtle.end_fill()

turtle.penup()

# 再画外面两个正方形

x1 = x - n * 0.12

y1 = y - n * 0.12 + n

turtle.goto(x1, y1)

turtle.pendown()

turtle.pensize(4)

for index in range(4):

turtle.forward(n * 8 + 2 * n * 0.12)

turtle.left(90)

turtle.penup()

# -----------------------------------------------

x2 = x - n * 0.3

y2 = y - n * 0.3 + n

turtle.goto(x2, y2)

turtle.pendown()

turtle.pensize(10)

for index in range(4):

turtle.forward(n * 8 + 2 * n * 0.3)

turtle.left(90)

turtle.hideturtle()

turtle.done()

稍作修改后,代码如下:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:Hiuhung Wan

import turtle

n = 60 # 每行间隔,小格子边长

x = -300 # x初始值

y = -300 # x初始值

def main():

turtle.speed(11)

turtle.pensize(2)

turtle.penup()

# 先画8*8的正方形,并按要求涂黑

for i in range(8):

for j in range(8):

turtle.goto(x + i * n, y + j * n)

if (i + j) % 2 == 0: # 白格子

draw_square(n, "white")

elif (i + j) % 2 == 1: # 黑格子

draw_square(n, "black")

# 再画外面两个正方形

x1 = x - n * 0.12

y1 = y - n * 0.12

n1 = n * 8 + 2 * n * 0.12

turtle.goto(x1, y1)

turtle.pensize(4)

draw_square_2(n1)

# -----------------------------------------------

x2 = x - n * 0.3

y2 = y - n * 0.3

n2 = n * 8 + 2 * n * 0.3

turtle.goto(x2, y2)

turtle.pensize(10)

draw_square_2(n2)

turtle.hideturtle()

turtle.done()

def draw_square(length:float, fill_color:str):

"""

画正方形并填充

:param length: 边长

:param fill_color: 填充颜色

:return: 无

"""

turtle.pendown()

turtle.begin_fill()

turtle.fillcolor(fill_color)

for index in range(4):

turtle.forward(length)

turtle.left(90)

turtle.end_fill()

turtle.penup()

def draw_square_2(length:float):

"""

画正方形,不填充

:param length: 边长

:return: 无

"""

turtle.pendown()

for index in range(4):

turtle.forward(length)

turtle.left(90)

turtle.penup()

if __name__ == '__main__':

main()

效果图如下:

1249181-20180626225904824-235904203.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值