创建棋盘,棋子有问题

2024-09-30 02:33:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我在挣扎着在哪里我甚至放了一个圈为比赛抽签?它是否应该是一个单独需要调用的函数?我能把它放在画正方形的环里面吗?在


Tags: andinforifdefrangefillcolor
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 ()

我强烈建议你不要在方格里画圆圈,而是要创建一只海龟来代表你的跳棋。这将允许你在你的棋盘周围移动棋盘,而不需要删除棋盘格的旧位置和重新绘制空方块。在

我重新编写了您的样式代码,并添加了一个演示部分,其中随机分布了一打关于黑色方块的红色方格:

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

相关问题 更多 >

    热门问题