如何在Python中控制游戏棋盘的棋盘格?

时间:2018-04-04 18:30:46

标签: python

所以,我之前曾问过类似的问题,但我仍然无法解决我的问题。我需要在每侧均匀地制作一个有6个游戏棋子的棋盘。这段代码很有帮助,但随机分发游戏片段,我用多行来玩它并试图理解,但它不是为了点击我。如何在上半部分中获得六个部分,在中间部分中间空间中部分六个部分位于下半部分?就像常规的棋盘设置一样?

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 = (SQUARES_PER_SIDE),(SQUARES_PER_SIDE)

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

    checker.goto(position)


screen.mainloop()

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的。将for _ in range(12):循环替换为:

# Returns the drawing co-ords of checker space
def get_square_coords(row, col):
    return (col * SQUARE_SIZE + SQUARE_SIZE/2, row * SQUARE_SIZE + SQUARE_SIZE/2)

# Creates 2 rows of checkers
def create_checkers(row, color):
    checkers = []
    col = 0
    for y in range(row, row+2):
        for x in range(col, SQUARES_PER_SIDE, 2):
            checker = Turtle('circle')
            checker.color('black', color)
            checker.shapesize(SQUARE_SIZE / CURSOR_SIZE)
            checker.penup()
            position = get_square_coords(y, x)
            checker.goto(position)
            checkers.append(checker)
        col += 1
    return checkers

red_checkers = create_checkers(0, 'red')
green_checkers = create_checkers(6, 'green')

它创造了这个: enter image description here