我正在尝试用 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()
我正在努力解决在哪里放置循环来为游戏绘制棋子?它应该是一个需要调用的独立函数吗?我可以将它放置在绘制正方形的循环内的某个位置吗?
最佳答案
我强烈建议您不要在正方形内画圆圈,而是创建单独的 turtle 来代表您的跳棋。这将允许您在棋盘上移动棋子,而无需删除棋子的旧位置并重新绘制空方 block 。
我重新设计了您的代码样式,并添加了一个演示部分,该部分在黑色方 block 周围随机分布了十几个红色棋子:
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()
关于python - 创建棋盘,棋子出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48792886/