如何将对象添加到Python-3x 中预定义的正方形中?
How to add an object into a square which is pre-defined in Python-3x?
我用 turtle 模块驱动了一个 8x8 尺寸的板子。它看起来像一个棋盘,但它不是,因为黑块是随机分布的。
目的是想做一个A星算法。算法部分完成。但是,我卡在了图形界面部分。
我用的是turtle-graphics
模块,但据我了解,它是用笔画形状的,也就是通过画线来移动整个特定路径。我正在处理 turtle 模块的功能以了解它们是什么。
我的问题是:
我想将对象或图像放入特定块中。事实上,我放入对象的块将是白色的。
我还想让那个物体像下棋一样从一个方块移动到另一个方块。 (然而,它不是)。
一次移动后,是否可以用颜色填充前一个块? (实际上,这是可能的,我已经完成了这部分,但是有点耗时。我的意思是,程序必须等待一段时间才能完成笔的移动,以便用颜色填充该块。)
import turtle
import image
Tess=turtle.Turtle()
Tess.pensize(1)
Tess.speed(-100)
w=40
def mysquare(who,thecolor,size):
who.pendown()
who.pencolor('black')
who.fillcolor(thecolor)
who.begin_fill()
who.setheading(0)
for i in range(4):
who.forward(size)
who.left(90)
who.end_fill()
for j in range(8):
for i in range(8):
# print(i,j)
if i==5 and j==7 or i==5 and j==6 or i==3 and j==5 or i==7 and j==5 or i==2 and j==4 or i==4 and j==4 or i==5 and j==4 or i==6 and j==4:
scolor='black'
else:
scolor='white'
Tess.penup()
Tess.goto((i-4)*w,(j-4)*w)
mysquare(Tess,scolor,w)
我相信以下表现出您所描述的行为。它设置棋盘,将乌龟移动到一个白色方块,然后随机将它移动到它尚未访问过的方块(避免黑色方块),直到它用完有效移动:
from turtle import Turtle, Screen
from random import choice
WIDTH = 40
BOARD_SIZE = 8
COLOR_EMPTY, COLOR_BLOCKED, COLOR_VISITED, COLOR_FAILED = 'white', 'black', 'green', 'red'
blocked_squares = [(5, 7), (5, 6), (3, 5), (7, 5), (2, 4), (4, 4), (5, 4), (6, 4)] # should generate randomly
def mysquare(turtle, i, j, color, size):
turtle.penup()
turtle.goto((i - BOARD_SIZE//2) * size, (j - BOARD_SIZE//2) * size)
turtle.pendown()
turtle.fillcolor(color)
turtle.setheading(0)
turtle.begin_fill()
for _ in range(4):
turtle.forward(size)
turtle.left(90)
turtle.end_fill()
def board_to_screen(position):
y, x = position
return ((x - BOARD_SIZE//2) * WIDTH + WIDTH / 2, (y - BOARD_SIZE//2) * WIDTH + WIDTH / 2)
def move():
global position
positions = []
for j_delta in (-1, 0, 1):
if 0 <= position[0] + j_delta < BOARD_SIZE:
for i_delta in (-1, 0, 1):
if 0 <= position[1] + i_delta < BOARD_SIZE:
if board[position[0] + j_delta][position[1] + i_delta]:
new_position = (position[0] + j_delta, position[1] + i_delta)
if new_position != position:
positions.append(new_position)
j, i = position
if positions:
board[j][i] = False
mysquare(tess, i, j, COLOR_VISITED, WIDTH)
position = choice(positions)
screen.ontimer(move, 1000)
else:
mysquare(tess, i, j, COLOR_FAILED, WIDTH)
tess.penup()
tess.goto(board_to_screen(position))
screen.update()
tess = Turtle(shape='turtle')
tess.speed('fastest')
screen = Screen()
screen.tracer(0)
board = []
for j in range(BOARD_SIZE):
board.append(list())
for i in range(BOARD_SIZE):
board[j].append(True)
square_color = COLOR_EMPTY
if (i, j) in blocked_squares:
square_color = COLOR_BLOCKED
board[j][-1] = False
mysquare(tess, i, j, square_color, WIDTH)
screen.update()
position = (3, 3)
tess.penup()
tess.goto(board_to_screen(position))
screen.ontimer(move, 1000)
screen.exitonclick()
这可能最好用多只海龟来避免一些移动,而且棋盘上的海龟可以是 GIF 图像而不是内置的海龟形状。黑色方块可能应该是随机生成的(但要小心),乌龟棋盘上的初始起始位置也是如此。
我用 turtle 模块驱动了一个 8x8 尺寸的板子。它看起来像一个棋盘,但它不是,因为黑块是随机分布的。
目的是想做一个A星算法。算法部分完成。但是,我卡在了图形界面部分。
我用的是turtle-graphics
模块,但据我了解,它是用笔画形状的,也就是通过画线来移动整个特定路径。我正在处理 turtle 模块的功能以了解它们是什么。
我的问题是:
我想将对象或图像放入特定块中。事实上,我放入对象的块将是白色的。
我还想让那个物体像下棋一样从一个方块移动到另一个方块。 (然而,它不是)。
一次移动后,是否可以用颜色填充前一个块? (实际上,这是可能的,我已经完成了这部分,但是有点耗时。我的意思是,程序必须等待一段时间才能完成笔的移动,以便用颜色填充该块。)
import turtle import image Tess=turtle.Turtle() Tess.pensize(1) Tess.speed(-100) w=40 def mysquare(who,thecolor,size): who.pendown() who.pencolor('black') who.fillcolor(thecolor) who.begin_fill() who.setheading(0) for i in range(4): who.forward(size) who.left(90) who.end_fill() for j in range(8): for i in range(8): # print(i,j) if i==5 and j==7 or i==5 and j==6 or i==3 and j==5 or i==7 and j==5 or i==2 and j==4 or i==4 and j==4 or i==5 and j==4 or i==6 and j==4: scolor='black' else: scolor='white' Tess.penup() Tess.goto((i-4)*w,(j-4)*w) mysquare(Tess,scolor,w)
我相信以下表现出您所描述的行为。它设置棋盘,将乌龟移动到一个白色方块,然后随机将它移动到它尚未访问过的方块(避免黑色方块),直到它用完有效移动:
from turtle import Turtle, Screen
from random import choice
WIDTH = 40
BOARD_SIZE = 8
COLOR_EMPTY, COLOR_BLOCKED, COLOR_VISITED, COLOR_FAILED = 'white', 'black', 'green', 'red'
blocked_squares = [(5, 7), (5, 6), (3, 5), (7, 5), (2, 4), (4, 4), (5, 4), (6, 4)] # should generate randomly
def mysquare(turtle, i, j, color, size):
turtle.penup()
turtle.goto((i - BOARD_SIZE//2) * size, (j - BOARD_SIZE//2) * size)
turtle.pendown()
turtle.fillcolor(color)
turtle.setheading(0)
turtle.begin_fill()
for _ in range(4):
turtle.forward(size)
turtle.left(90)
turtle.end_fill()
def board_to_screen(position):
y, x = position
return ((x - BOARD_SIZE//2) * WIDTH + WIDTH / 2, (y - BOARD_SIZE//2) * WIDTH + WIDTH / 2)
def move():
global position
positions = []
for j_delta in (-1, 0, 1):
if 0 <= position[0] + j_delta < BOARD_SIZE:
for i_delta in (-1, 0, 1):
if 0 <= position[1] + i_delta < BOARD_SIZE:
if board[position[0] + j_delta][position[1] + i_delta]:
new_position = (position[0] + j_delta, position[1] + i_delta)
if new_position != position:
positions.append(new_position)
j, i = position
if positions:
board[j][i] = False
mysquare(tess, i, j, COLOR_VISITED, WIDTH)
position = choice(positions)
screen.ontimer(move, 1000)
else:
mysquare(tess, i, j, COLOR_FAILED, WIDTH)
tess.penup()
tess.goto(board_to_screen(position))
screen.update()
tess = Turtle(shape='turtle')
tess.speed('fastest')
screen = Screen()
screen.tracer(0)
board = []
for j in range(BOARD_SIZE):
board.append(list())
for i in range(BOARD_SIZE):
board[j].append(True)
square_color = COLOR_EMPTY
if (i, j) in blocked_squares:
square_color = COLOR_BLOCKED
board[j][-1] = False
mysquare(tess, i, j, square_color, WIDTH)
screen.update()
position = (3, 3)
tess.penup()
tess.goto(board_to_screen(position))
screen.ontimer(move, 1000)
screen.exitonclick()
这可能最好用多只海龟来避免一些移动,而且棋盘上的海龟可以是 GIF 图像而不是内置的海龟形状。黑色方块可能应该是随机生成的(但要小心),乌龟棋盘上的初始起始位置也是如此。