【问题标题】:Creating a checkerboard, trouble with Pieces创建棋盘格,碎片问题
【发布时间】: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()

我正在为我什至在哪里放置一个循环来为游戏绘制棋子而苦苦挣扎?它应该是一个需要调用的函数吗?我可以将它放在绘制正方形的循环内的某个地方吗?

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

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

    我已经修改了您的样式代码并添加了一个演示部分,该部分随机分布了十几个关于黑色方块的红色棋子:

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

    【讨论】:

    • 很好的答案 cdlane。您可以在画布上合理地创建多少只海龟? 100个? 1000 个?
    • 谢谢,如果循环没有正确排列,我会考虑使用不同的海龟!我认为使用海龟是我最初的计划,但我不确定正方形的位置以使圆圈位于顶部!
    【解决方案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 ()
    

    【讨论】:

    • 谢谢!循环对我来说是一个很难理解的东西,我会尝试类似的东西!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2012-02-15
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多