Project 1 : Using Turtle to Draw Grid (Python Exercise 9)

Data Processing and Visulisation with Python

Turtle

Turtle_example

import turtle
t = turtle.Turtle()
t.fd(100)
t.lt(90)
t.fd(100)
t.left(90)
t.forward(100)
turtle.done()

在这里插入图片描述

About turtle

在这里插入图片描述

Case Study

drawGride

my method

import turtle

def drawGrid(rowCount, columnCount, cellWidth, cellHeight):
    """
    draw a grid with rowCount*columnCount cells.
    * rowCount: total number of rows in the grid
    * columnCount: total number of columns in the grid
    * cellWidth: number of pixels in width for each cell
    * cellHeight: number of pixels in height for each cell
    so the total number of pixels in width for the whole grid will be (columnCount*cellwidth+1)
    the total number of pixels in height for the whole grid will be (rowCount*cellHeight+1)
    """

    ## You can try and test different paddings
    screenPaddingWidth = 6
    screenPaddingHeight = 3
    gridWidth = columnCount * cellWidth + 1
    gridHeight = rowCount * cellHeight + 1
    screenWidth = gridWidth + screenPaddingWidth
    screenHeight = gridHeight + screenPaddingHeight

    screen = turtle.Screen()
    screen.setup(screenWidth, screenHeight)
    screen.setworldcoordinates(0, 0, screenWidth-1, screenHeight-1)

    ## put your own name in screen title
    screen.title('Grid created by Li YuXiao')

    ## if you uncomment the two statements with screen.tracer(), what will happen
    # screen.tracer(False)
    pen = turtle.Turtle()
    pen.speed(10)
    pen.hideturtle()

    ## finish the following code to draw out the grid
    # draw grid
    #-----------------Code Section--------------------
    def element():
        turtle.forward(cellWidth)
        turtle.right(90)
        turtle.forward(cellHeight)
        turtle.right(90)
        turtle.forward(cellWidth)
        turtle.right(90)
        turtle.forward(cellHeight)
        turtle.right(90)
    for i in range(columnCount+1):
        for j in range(rowCount+1):
            turtle.goto(cellWidth*i,cellHeight*j)
            turtle.pendown()
            element()
            turtle.penup()


    #--------------End of Code Section----------------

    # screen.tracer(True)
    screen.exitonclick()
    turtle.done()


## finish the following code for the file to run as a script
# if the file is run as a script, 
# it will take  4 arguments from the command line and 
# calls drawGrid function with these arguments.

#-----------------Code Section--------------------
if __name__ == '__main__':
    import sys
    numOfRows = int(sys.argv[1])
    numOfColumns = int(sys.argv[2])
    widthOfCell = int(sys.argv[3])
    heightOfCell = int(sys.argv[4])
    drawGrid(numOfRows, numOfColumns, widthOfCell, heightOfCell)

#--------------End of Code Section----------------

drawGrid - Helper


import turtle

def drawGrid(rowCount, columnCount, cellWidth, cellHeight):
    """
    draw a grid with rowCount*columnCount cells.
    * rowCount: total number of rows in the grid
    * columnCount: total number of columns in the grid
    * cellWidth: number of pixels in width for each cell
    * cellHeight: number of pixels in height for each cell
    so the total number of pixels in width for the whole grid will be (columnCount*cellwidth+1)
    the total number of pixels in height for the whole grid will be (rowCount*cellHeight+1)
    """

    ## You can try and test different paddings
    screenPaddingWidth = 6
    screenPaddingHeight = 3
    gridWidth = columnCount * cellWidth + 1
    gridHeight = rowCount * cellHeight + 1
    screenWidth = gridWidth + screenPaddingWidth
    screenHeight = gridHeight + screenPaddingHeight

    screen = turtle.Screen()
    screen.setup(screenWidth, screenHeight)
    screen.setworldcoordinates(0, 0, screenWidth-1, screenHeight-1)

    ## put your own name in screen title
    screen.title('Grid created by YE Huanzhuo')

    ## if you uncomment the two statements with screen.tracer(), what will happen
    # screen.tracer(False)
    pen = turtle.Turtle()
    pen.speed(10)
    pen.hideturtle()

    ## finish the following code to draw out the grid
    # draw grid
    #-----------------Code Section--------------------
    for i in range(rowCount+1):
        pen.up()
        pen.goto(0, i*cellHeight)
        pen.down()
        pen.goto(columnCount*cellWidth, i*cellHeight)
    for i in range(columnCount+1):
        pen.up()
        pen.goto(i*cellWidth, 0)
        pen.down()
        pen.goto(i*cellWidth, rowCount*cellHeight)
    #--------------End of Code Section----------------

    # screen.tracer(True)
    screen.exitonclick()
    turtle.done()


## finish the following code for the file to run as a script
# if the file is run as a script, 
# it will take  4 arguments from the command line and 
# calls drawGrid function with these arguments.

#-----------------Code Section--------------------
if __name__ == '__main__':
    import sys
    numOfRows = int(sys.argv[1])
    numOfColumns = int(sys.argv[2])
    widthOfCell = int(sys.argv[3])
    heightOfCell = int(sys.argv[4])
    drawGrid(numOfRows, numOfColumns, widthOfCell, heightOfCell)
#--------------End of Code Section----------------

drawGridAsUserInput

from drawGrid import drawGrid

numOfRows = int(input('Please input the number of rows in the grid: '))
numOfColumns = int(input('Please input the number of columns in the grid: '))
widthOfCell = int(input('Please input the width in pixels for each cell: '))
heightOfCell = int(input('Please input the height in pixels for each cell: '))
drawGrid(numOfRows, numOfColumns, widthOfCell, heightOfCell)
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值