【发布时间】:2018-02-14 17:19:21
【问题描述】:
我正在尝试在 Python 中创建一个棋盘格。目前,我已经弄清楚了游戏本身的实际设置,但我想在正方形内画圆圈来创建“游戏”片段。
import turtle
turtle.bgcolor("Grey")
def drawRect(color):
iterations = 0
turtle.begin_fill() # Begin the fill process.
turtle.down()
turtle.color(color)
while iterations < 4:
turtle.forward(40)
turtle.left(90)
iterations += 1
turtle.up() # Pen up
turtle.end_fill()
def pushTurtleForward():
turtle.forward(40)
def drawHorizontal(inverted):
if(inverted):
for horizontal in range(0, 8):
if(horizontal > 0 and horizontal % 2 != 0):
pushTurtleForward()
drawRect("white")
if(horizontal > 0 and horizontal % 2 == 0):
pushTurtleForward()
drawRect("black")
if(horizontal == 0):
drawRect("black")
else:
for horizontal in range(0, 8):
if(horizontal > 0 and horizontal % 2 != 0):
pushTurtleForward()
drawRect("black")
if(horizontal > 0 and horizontal % 2 == 0):
pushTurtleForward()
drawRect("white")
if(horizontal == 0):
drawRect("white")
for drawVertical in range(0, 8):
turtle.setx(0)
turtle.sety(40 * drawVertical)
if(drawVertical % 2 == 0):
drawHorizontal(inverted=True)
else:
drawHorizontal(inverted=False)
turtle.setx(0)
turtle.sety(0)
turtle.done()
我正在为我什至在哪里放置一个循环来为游戏绘制棋子而苦苦挣扎?它应该是一个需要调用的函数吗?我可以将它放在绘制正方形的循环内的某个地方吗?
【问题讨论】: