无论计算机屏幕大小如何,如何将棋盘放置在乌龟屏幕的左上角?
How to place checker board in the top left corner of a turtle screen regardless of computer screen size?
我已经编写了如何实际绘制棋盘的代码,但我需要将它放置在乌龟屏幕的左上角,无论某人正在查看什么计算机屏幕(棋盘的左上角是始终位于海龟屏幕的左上角,无论屏幕是 200000 像素还是 2000 像素)。我该怎么做呢?谢谢!
import turtle
def draw_box(t, x, y, size, fill_color):
t.penup() # no drawing!
t.goto(x, y) # move the pen to a different position
t.pendown() # resume drawing
t.fillcolor(fill_color)
t.begin_fill() # Shape drawn after this will be filled with this color!
for i in range(a, b):
board.forward(size) # move forward
board.right(90) # turn pen right 90 degrees
t.end_fill() # Go ahead and fill the rectangle!
def draw_chess_board():
square_color = "black" # first chess board square is black
start_x = 1 # starting x position of the chess board
start_y = 1 # starting y position of the chess board
box_size = 50 # pixel size of each square in the chess board
for i in range(0, 8): # 8x8 chess board
for j in range(0, 8):
draw_box(board, start_x + j * box_size, start_y + i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!
board = turtle.Turtle()
draw_chess_board()
turtle.done()
我将展示执行此操作的几种方法。首先,我将提供 draw_board()
的工作版本,因为发布的版本已损坏。我将包括我首选的导入 turtle 的方式:
from turtle import Screen, Turtle
def draw_box(t, x, y, size, fill_color):
t.penup() # no drawing!
t.goto(x, y) # move the pen to a different position
t.pendown() # resume drawing
t.fillcolor(fill_color)
t.begin_fill() # Shape drawn after this will be filled with this color!
for _ in range(4):
t.forward(size) # move forward
t.right(90) # turn pen right 90 degrees
t.end_fill() # Go ahead and fill the rectangle!
我们将对这两种解决方案使用方法 window_width()
和 window_height()
,并从上到下而不是从下到上绘制板。第一种解决方案只是简单地移动起点,使用上述方法,并沿相反的Y方向移动:
def draw_chess_board():
square_color = 'black' # first chess board square is black
box_size = 50 # pixel size of each square in the chess board
start_x = -width/2 # starting x position of the chess board
start_y = height/2 # starting y position of the chess board
for i in range(8): # 8x8 chess board
for j in range(8):
draw_box(board, start_x + j * box_size, start_y - i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!
screen = Screen()
width, height = screen.window_width(), screen.window_height()
board = Turtle()
board.speed('fastest') # because I have no patience
draw_chess_board()
board.hideturtle()
screen.mainloop()
第二种方法单独保留 X 起点和 Y 方向代码,而是操纵坐标系以获得相同的结果:
def draw_chess_board():
square_color = 'black' # first chess board square is black
box_size = 50 # pixel size of each square in the chess board
start_x = 1 # starting x position of the chess board
start_y = box_size # starting y position of the chess board
for i in range(8): # 8x8 chess board
for j in range(8):
draw_box(board, start_x + j * box_size, start_y + i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!
screen = Screen()
width, height = screen.window_width(), screen.window_height()
screen.setworldcoordinates(0, height, width, 0)
board = Turtle()
board.speed('fastest') # because I have no patience
draw_chess_board()
board.hideturtle()
screen.mainloop()
这种对坐标系的操作会影响您在程序中所做的一切,因此请确保在添加更多代码之前了解它。
最后,如果我从头开始(或者 stamping 优于 drawing 的优点),我可能会如何编写这个程序:
from turtle import Screen, Turtle
BOX_SIZE = 50 # pixel size of each square in the chess board
CURSOR_SIZE = 20
COLOR_MAP = {'red': 'black', 'black': 'red'} # try red just for fun
def draw_chess_board():
square.color('black') # first chess board square is black
start = BOX_SIZE/2 # starting x and/or y position of the chess board
for row in range(8): # 8x8 chess board
square.goto(start, start + row * BOX_SIZE)
for _ in range(8):
square.stamp()
square.color(COLOR_MAP[square.fillcolor()]) # toggle after a column
square.forward(BOX_SIZE)
square.color(COLOR_MAP[square.fillcolor()]) # toggle after a row!
screen = Screen()
screen.setworldcoordinates(0, screen.window_height(), screen.window_width(), 0)
square = Turtle()
square.hideturtle()
square.penup() # no drawing!
square.shape('square')
square.speed('fastest') # because I have no patience
square.shapesize(BOX_SIZE / CURSOR_SIZE)
draw_chess_board()
screen.mainloop()
我已经编写了如何实际绘制棋盘的代码,但我需要将它放置在乌龟屏幕的左上角,无论某人正在查看什么计算机屏幕(棋盘的左上角是始终位于海龟屏幕的左上角,无论屏幕是 200000 像素还是 2000 像素)。我该怎么做呢?谢谢!
import turtle
def draw_box(t, x, y, size, fill_color):
t.penup() # no drawing!
t.goto(x, y) # move the pen to a different position
t.pendown() # resume drawing
t.fillcolor(fill_color)
t.begin_fill() # Shape drawn after this will be filled with this color!
for i in range(a, b):
board.forward(size) # move forward
board.right(90) # turn pen right 90 degrees
t.end_fill() # Go ahead and fill the rectangle!
def draw_chess_board():
square_color = "black" # first chess board square is black
start_x = 1 # starting x position of the chess board
start_y = 1 # starting y position of the chess board
box_size = 50 # pixel size of each square in the chess board
for i in range(0, 8): # 8x8 chess board
for j in range(0, 8):
draw_box(board, start_x + j * box_size, start_y + i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!
board = turtle.Turtle()
draw_chess_board()
turtle.done()
我将展示执行此操作的几种方法。首先,我将提供 draw_board()
的工作版本,因为发布的版本已损坏。我将包括我首选的导入 turtle 的方式:
from turtle import Screen, Turtle
def draw_box(t, x, y, size, fill_color):
t.penup() # no drawing!
t.goto(x, y) # move the pen to a different position
t.pendown() # resume drawing
t.fillcolor(fill_color)
t.begin_fill() # Shape drawn after this will be filled with this color!
for _ in range(4):
t.forward(size) # move forward
t.right(90) # turn pen right 90 degrees
t.end_fill() # Go ahead and fill the rectangle!
我们将对这两种解决方案使用方法 window_width()
和 window_height()
,并从上到下而不是从下到上绘制板。第一种解决方案只是简单地移动起点,使用上述方法,并沿相反的Y方向移动:
def draw_chess_board():
square_color = 'black' # first chess board square is black
box_size = 50 # pixel size of each square in the chess board
start_x = -width/2 # starting x position of the chess board
start_y = height/2 # starting y position of the chess board
for i in range(8): # 8x8 chess board
for j in range(8):
draw_box(board, start_x + j * box_size, start_y - i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!
screen = Screen()
width, height = screen.window_width(), screen.window_height()
board = Turtle()
board.speed('fastest') # because I have no patience
draw_chess_board()
board.hideturtle()
screen.mainloop()
第二种方法单独保留 X 起点和 Y 方向代码,而是操纵坐标系以获得相同的结果:
def draw_chess_board():
square_color = 'black' # first chess board square is black
box_size = 50 # pixel size of each square in the chess board
start_x = 1 # starting x position of the chess board
start_y = box_size # starting y position of the chess board
for i in range(8): # 8x8 chess board
for j in range(8):
draw_box(board, start_x + j * box_size, start_y + i * box_size, box_size, square_color)
square_color = 'black' if square_color == 'white' else 'white' # toggle after a column
square_color = 'black' if square_color == 'white' else 'white' # toggle after a row!
screen = Screen()
width, height = screen.window_width(), screen.window_height()
screen.setworldcoordinates(0, height, width, 0)
board = Turtle()
board.speed('fastest') # because I have no patience
draw_chess_board()
board.hideturtle()
screen.mainloop()
这种对坐标系的操作会影响您在程序中所做的一切,因此请确保在添加更多代码之前了解它。
最后,如果我从头开始(或者 stamping 优于 drawing 的优点),我可能会如何编写这个程序:
from turtle import Screen, Turtle
BOX_SIZE = 50 # pixel size of each square in the chess board
CURSOR_SIZE = 20
COLOR_MAP = {'red': 'black', 'black': 'red'} # try red just for fun
def draw_chess_board():
square.color('black') # first chess board square is black
start = BOX_SIZE/2 # starting x and/or y position of the chess board
for row in range(8): # 8x8 chess board
square.goto(start, start + row * BOX_SIZE)
for _ in range(8):
square.stamp()
square.color(COLOR_MAP[square.fillcolor()]) # toggle after a column
square.forward(BOX_SIZE)
square.color(COLOR_MAP[square.fillcolor()]) # toggle after a row!
screen = Screen()
screen.setworldcoordinates(0, screen.window_height(), screen.window_width(), 0)
square = Turtle()
square.hideturtle()
square.penup() # no drawing!
square.shape('square')
square.speed('fastest') # because I have no patience
square.shapesize(BOX_SIZE / CURSOR_SIZE)
draw_chess_board()
screen.mainloop()