使用Python turtle库画画

故事起源:办公室要重新规划,老板要求手绘办公室的layout,要求尺寸准确。

故事发展:手绘了2副之后,发现尺寸差异太大,修改麻烦。

选择软件解决方案:机构或者IE工程师,这根本不是个事。对于软件工程师,这个也难不倒,发现turtle库可以完美解决这个问题。

下面直接show code,关于turtle具体使用,大家可以搜索相关的文档。

 

import turtle as t
import time


# Office 1660 * 1200 (840 * 600)
# office desk 159 * 69 (80 * 35)
# lab desk 160 * 80 (80 * 40)
# boss cube 300 * 300 (150 * 150)

officeX = 1660/2
officeY = 1200/2
officeDeskX = 159/2
officeDeskY = 69/2
labDeskX = 160/2
labDeskY = 80/2
bossCubeX = 300/2
bossCubeY = 300/2
storageCubeX = 300/2
storageCubeY = officeY - bossCubeY*2

def moveToLeftTop():
	t.home()
	t.up()
	t.left(90)
	t.forward(officeY/2)
	t.left(90)
	t.forward(officeX/2)
	t.setheading(0)

def moveToLeftBottom():
	t.home()
	t.up()
	t.right(90)
	t.forward(officeY/2)
	t.right(90)
	t.forward(officeX/2)
	t.setheading(0)
	
def drawOffice():
	moveToLeftTop()
	t.color("yellow")
	t.begin_fill()
	t.down()
	t.forward(officeX)
	t.right(90)
	t.forward(officeY)
	t.right(90)
	t.forward(officeX)
	t.right(90)
	t.forward(officeY)
	t.end_fill()

def drawDesk(x, y, color):
	t.pensize(4)
	t.pencolor(color)
	t.down()
	t.setheading(0)
	t.left(90)
	t.forward(x)
	t.right(90)
	t.forward(y)
	t.right(90)
	t.forward(x)
	t.right(90)
	t.forward(y)
	t.right(90)
	t.up()
	t.forward(x)
	
def drawDesk4x2(x, y, color):
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)

	t.up()
	t.left(180)
	t.forward(x * 4)
	t.left(90)
	t.forward(y)
	t.left(90)

	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	
	t.up()
	t.right(180)
	t.forward(x * 4)
	t.left(90)
	t.forward(y)
	
def drawDesk6x2(x, y, color):
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)

	t.up()
	t.left(180)
	t.forward(x * 6)
	t.left(90)
	t.forward(y)
	t.left(90)

	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	drawDesk(x, y, color)
	
	t.up()
	t.right(180)
	t.forward(x * 6)
	t.left(90)
	t.forward(y)

def drawDesk6x2Mix(x1, y1, color1, x2, y2, color2):
	drawDesk(x1, y1, color1)
	drawDesk(x1, y1, color1)
	drawDesk(x1, y1, color1)
	drawDesk(x2, y2, color2)
	drawDesk(x2, y2, color2)
	drawDesk(x2, y2, color2)

	t.up()
	t.left(180)
	t.forward(x1*3 + x2*3)
	t.left(90)
	t.forward(y1)
	t.left(90)

	drawDesk(x1, y1, color1)
	drawDesk(x1, y1, color1)
	drawDesk(x1, y1, color1)
	drawDesk(x2, y2, color2)
	drawDesk(x2, y2, color2)
	drawDesk(x2, y2, color2)
	
	t.up()
	t.right(180)
	t.forward(x1*3 + x2*3)
	t.left(90)
	t.forward(y1)

def drawAllDesk():
	moveToLeftBottom()
	t.up()
	t.forward(230/2)
	t.left(90)
	t.forward(60/2)
	drawDesk6x2(x=officeDeskX, y=officeDeskY, color="white")
	
	t.up()
	t.forward(230/2)
	#drawDesk6x2(x=officeDeskX, y=officeDeskY, color="white")
	drawDesk6x2Mix(x1=labDeskX, y1=labDeskY, color1="green", x2=officeDeskX, y2=officeDeskY, color2="white")
	
	t.up()
	t.forward(230/2)
	#drawDesk6x2(x=labDeskX, y=labDeskY, color="green")
	drawDesk6x2Mix(x1=labDeskX, y1=labDeskY, color1="green", x2=officeDeskX, y2=officeDeskY, color2="white")


def moveToRightBottom():
	t.home()
	t.up()
	t.setheading(-90)
	t.forward(officeY/2)
	t.left(90)
	t.forward(officeX/2)

def drawBossCube():
	t.pensize(4)
	t.pencolor("blue")
	t.down()
	t.setheading(-90)
	t.forward(bossCubeY)
	t.right(90)
	t.forward(bossCubeX)
	t.right(90)
	t.forward(bossCubeY)
	t.right(90)
	t.forward(bossCubeX)
	t.up()
	t.right(90)
	t.forward(bossCubeY)
	
def drawBossCubeX2():
	moveToRightTop()
	drawBossCube()
	drawBossCube()
	
def drawStorageCube():
	t.pensize(4)
	t.pencolor("orange")
	t.down()
	t.setheading(-90)
	t.forward(storageCubeY)
	t.right(90)
	t.forward(storageCubeX)
	t.right(90)
	t.forward(storageCubeY)
	t.right(90)
	t.forward(storageCubeX)
	t.up()
	t.right(90)
	t.forward(storageCubeY)

def moveToRightTop():
	t.home()
	t.up()
	t.setheading(90)
	t.forward(officeY/2)
	t.right(90)
	t.forward(officeX/2)
	
def drawGate():
	moveToLeftTop()	
	t.setheading(-90)
	t.forward(100/2)
	t.pensize(4)
	t.pencolor("black")
	t.down()
	t.left(90)
	t.circle(100/2, 90)
	t.left(90)
	t.forward(100/2)
	t.left(90)
	t.forward(100/2)
	t.up()
	
def drawCabinet1():
	cabinetX = 80/2
	cabinetY = officeY -200/2
	
	moveToLeftTop()	
	t.setheading(-90)
	t.forward(200/2)
	t.pensize(2)
	t.pencolor("gray")
	t.down()	
	t.forward(cabinetY)
	t.left(90)
	t.forward(cabinetX)
	t.left(90)
	t.forward(cabinetY)
	t.left(90)
	t.forward(cabinetX)
	t.up()
	
def drawCabinet2():
	cabinetX = officeX - bossCubeX -200/2
	cabinetY = 80/2 
	
	moveToLeftTop()
	t.forward(200/2)
	t.pensize(2)
	t.pencolor("gray")
	t.down()	
	t.forward(cabinetX)
	t.right(90)
	t.forward(cabinetY)
	t.right(90)
	t.forward(cabinetX)
	t.right(90)
	t.forward(cabinetY)
	t.up()
	
def drawCabinet3():
	cabinetX = 80/2
	cabinetY = officeY - bossCubeY
	
	moveToRightBottom()	
	t.setheading(180)
	t.forward(bossCubeX)
	t.pensize(2)
	t.pencolor("gray")
	t.down()	
	t.forward(cabinetX)
	t.right(90)
	t.forward(cabinetY)
	t.right(90)
	t.forward(cabinetX)
	t.right(90)
	t.forward(cabinetY)
	t.up()
	
def drawCabinet():
	drawCabinet1()
	drawCabinet2()
	drawCabinet3()

drawOffice()
drawGate()
drawAllDesk()
drawBossCubeX2()
drawStorageCube()
drawCabinet()

t.done()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值