创建一个棋盘,有问题

时间:2018-02-14 17:19:21

标签: python turtle-graphics

我试图在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()

我在哪里挣扎,我甚至为比赛画了一个循环?它应该是一个自己需要被调用的函数吗?我可以将它放置在绘制正方形的循环内吗?

2 个答案:

答案 0 :(得分:3)

我强烈建议您不要在方块内绘制圆圈,而是创建单独的海龟来代表您的棋子。这将允许您在棋盘上移动棋盘,而无需擦除棋盘的旧位置并重新绘制空方块。

我已经重新设计了你的代码风格,并添加了一个演示部分,随机分发了十几个关于黑色方块的红色棋子:

from turtle import Turtle, Screen
from random import randrange

CURSOR_SIZE = 20
SQUARE_SIZE = 40
SQUARES_PER_SIDE = 8

def drawRect(color):
    turtle.color(color)
    turtle.pendown()
    turtle.begin_fill()

    for iterations in range(4):
        turtle.forward(SQUARE_SIZE)
        turtle.left(90)

    turtle.end_fill()
    turtle.penup()

def pushTurtleForward():
    turtle.forward(SQUARE_SIZE)

def drawHorizontal(inverted=False):
    if inverted:
        for horizontal in range(SQUARES_PER_SIDE):
            if horizontal > 0:
                if horizontal % 2 == 1:
                    pushTurtleForward()
                    drawRect("white")
                else:
                    pushTurtleForward()
                    drawRect("black")
            else:
                drawRect("black")
    else:
        for horizontal in range(SQUARES_PER_SIDE):
            if horizontal > 0:
                if horizontal % 2 == 1:
                    pushTurtleForward()
                    drawRect("black")
                else:
                    pushTurtleForward()
                    drawRect("white")
            else:
                drawRect("white")

screen = Screen()
screen.bgcolor("Grey")

turtle = Turtle(visible=False)
turtle.speed('fastest')

for drawVertical in range(SQUARES_PER_SIDE):
    turtle.setposition(0, SQUARE_SIZE * drawVertical)

    if drawVertical % 2 == 0:
        drawHorizontal(inverted=True)
    else:
        drawHorizontal()

# Checker graphics demonstration.  Distribute 12 red checkers around
# black squares on board without any two landing on the same spot.

red_checkers = []

for _ in range(12):
    checker = Turtle('circle')
    checker.color('black', 'red')
    checker.shapesize(SQUARE_SIZE / CURSOR_SIZE)
    checker.penup()

    red_checkers.append(checker)

    position = checker.position()  # a position guaranteed to fail

    while any(map(lambda checker, p=position: checker.distance(p) < SQUARE_SIZE/2, red_checkers)):
        x, y = 0, 1  # a parity guaranteed to fail

        while x % 2 != y % 2:
            x, y = randrange(SQUARES_PER_SIDE), randrange(SQUARES_PER_SIDE)

        position = (x * SQUARE_SIZE + SQUARE_SIZE/2, y * SQUARE_SIZE + SQUARE_SIZE/2)

    checker.goto(position)


screen.mainloop()

enter image description here

答案 1 :(得分:2)

确实,圈子的功能本身就是一个好主意。 通常对于像棋盘这样的二维任何东西,最好的方法是在彼此内部使用两个循环(嵌套循环)。外部循环遍历所有8行,并且对于每行,内部循环遍历所有8列。同样在drawRect中,您对while循环执行的操作是正确的,但for循环更常见于此目的。

import turtle

fieldSize = 40
turtle.speed (0)

def drawRect(rowIndex, colIndex, color):
    turtle.sety (rowIndex * fieldSize)
    turtle.setx (colIndex * fieldSize)
    turtle.begin_fill() # Begin the fill process.
    turtle.down()
    turtle.color(color)
    for iterations in range(4):
        turtle.forward(fieldSize)
        turtle.left(90)
    turtle.up()
    turtle.end_fill()

def drawCircle(rowIndex, colIndex, color):
    turtle.sety (rowIndex * fieldSize)
    turtle.setx ((colIndex + 0.5) * fieldSize)
    turtle.begin_fill() # Begin the fill process.
    turtle.down()
    turtle.color(color)
    turtle.circle(fieldSize / 2)
    turtle.up()
    turtle.end_fill()

for rowIndex in range (8):
    for colIndex in range (8):
        inverted = (rowIndex + colIndex) % 2 == 0
        drawRect (rowIndex, colIndex, 'black' if inverted else 'white')
        drawCircle (rowIndex, colIndex, 'white' if inverted else 'black')

turtle.done ()