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()

  

效果图如下:

python3 turtle 画国际象棋棋盘的更多相关文章

  1. python3 turtle 画围棋棋盘

    python3 环境 利用turtle模块画出 围棋棋盘 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan impor ...

  2. 使用turtle库画国际象棋棋盘

    import turtle n = 60 # 每行间隔,小格子边长 x = -300 # x初始值 y = -300 # x初始值 def main(): turtle.speed(11) turtl ...

  3. python3 turtle画正方形、矩形、正方体、五角星、奥运五环

    python3 环境 turtle模块 分别画出 正方形.矩形.正方体.五角星.奥运五环 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:H ...

  4. Python语言程序设计之二--用turtle库画围棋棋盘和正、余弦函数图形

    这篇笔记依然是在做<Python语言程序设计>第5章循环的习题.其中有两类问题需要记录下来. 第一是如何画围棋棋盘.围棋棋盘共有19纵19横.其中,位于(0,0)的星位叫天元,其余8个星位 ...

  5. Python3 turtle安装和使用教程

    Python3 turtle安装和使用教程   Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数 ...

  6. GUI(国际象棋棋盘)

    package com.niit.javagui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridB ...

  7. python运用turtle 画出汉诺塔搬运过程

    python运用turtle 画出汉诺塔搬运过程 1.打开 IDLE 点击File-New File 新建立一个py文件 2.向py文件中输入如下代码 import turtle class Stac ...

  8. 函数纹理(国际象棋棋盘纹理&粗布纹理)MFC

    函数纹理(国际象棋棋盘纹理&粗布纹理)MFC实现  源码百度云下载 国际象棋棋盘纹理(效果图见最后) //国际象棋纹理函数 //g(u, v) = a , 向下取整(8u)+向下取整(8v) ...

  9. day 03 turtle 画鹅

    turtle 画鹅 import turtle t=turtle turtle.speed(10) t. setup(800,600) #画头 turtle.penup() turtle.goto(0 ...

随机推荐

  1. Reuse Is About People and Education, Not Just Architecture

     Reuse Is About People and Education, Not Just Architecture Jeremy Meyer you MigHT AdopT THE AppRoA ...

  2. Codeforces 474D Flowers (线性dp 找规律)

    D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...

  3. Ajax的跨域问题分析

    一.Ajax的跨域问题 Ajax是利用javascript内置XMLHttpRequest对象来进行传输的,所以它依赖于XMLHttpRequest对象,而XMLHttpRequest对象却有很多的限 ...

  4. css中margin上下外边距重叠问题

    css的盒子模型里是这样规定两个对象之间的距离的:对象之间的间距是由两个对象的盒子模型的最终计算值得出来的,也就是说两个对象之间的间距就是两个对象的距离,但是当遇到两个对象一个有下外边距margin, ...

  5. TensorFlow的学习

    1.先判断python的版本(因为有些python版本自带pip,可以参考我写的对pip的认识的博客文章),选择是否安装pip,然后安装更新tensorflow如:sudo pip install - ...

  6. Rabin-Karp 算法

    Rabin-Karp字符串查找算法 http://blog.chinaunix.net/uid-26548237-id-3968132.html

  7. Linux硬件信息查看

    more /proc/cpuinfo more /proc/meminfo more /proc/*info lspci 查看主板信息等cat /proc/cpuinfo CPU信息cat /proc ...

  8. 洛谷 P1852 奇怪的字符串

    P1852 奇怪的字符串 题目描述 输入两个01串,输出它们的最长公共子序列的长度 输入输出格式 输入格式: 一行,两个01串 输出格式: 最长公共子序列的长度 输入输出样例 输入样例#1: 复制 0 ...

  9. error c2572重定义默认參数

    因为想省事.在声明过函数之后直接复制粘贴去实现,结果出现error c2572重定义默认參数 顾名思义.该默认參数被定义多次.在一个文件(或一个作用域)中,仅仅能为形參指定默认 实參一次.在编译的时候 ...

  10. 项目EasyUi和JS中遇到的问题总汇

    近期因为项目用到EasyUi,曾经仅仅是听过,可是没有详细用过.仅仅能一边学一边做.如今将做的过程中遇到的一些难点总结例如以下,以备后用: EsayUi使用: Json格式:key:value,key ...