[
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0 , 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0 , 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
Круг движется к первому препятствию, обходит его, спускаясь на ряд ниже, движется вправо, вниз, когда достигает край и направо к следующему препятствию. Это ожидаемо.
Но когда он достигает препятствия, он идет вниз, а затем влево? Вместо выполнения метода «препятствие посередине слева», как планировалось.
Что не так? Я думаю, что может возникнуть конфликт между функциями «препятствие в конце» и «нет препятствий».
Код ниже:
Код: Выделить всё
#generate an eviornemnt, represented by Tkinter, consisting of
#a matrix representing the room
#1 and 0 will denote the presence of obstacles
import math
import tkinter as tk
import numpy as np
import threading
matrix = [
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
ROOM_WIDTH = 700
ROOM_HEIGHT = 700
SPACE_SIZE = 75
OBSTACLE_COLOR = "#ADD8E6"
FREE_COLOR = "#90EE90"
rows = 4
columns = 4
window = tk.Tk()
canvas = tk.Canvas(window, height=ROOM_HEIGHT, width=ROOM_WIDTH)
direction_from = "begin_right"
class Bot:
def __init__(self):
self.running = True
self.speed = 8
self.x = ROOM_WIDTH // 8
self.y = ROOM_HEIGHT // 8
self.my_circle = canvas.create_oval(self.x, self.y, self.x + 30, self.y + 30)
canvas.moveto(self.my_circle, 30, 40)
def left(self):
x = 0 - (self.speed)
y = 0
canvas.move(self.my_circle, x, y)
def right(self):
x = self.speed
y = 0
canvas.move(self.my_circle, x, y)
def up(self):
x = 0
y = -self.speed
canvas.move(self.my_circle, x, y)
def down(self):
x = 0
y = 75 // 2
canvas.move(self.my_circle, x, y)
def check_collision(self):
global direction_from
mower_coords = canvas.coords(self.my_circle)
center_x = (mower_coords[0] + mower_coords[2]) / 2
center_y = (mower_coords[1] + mower_coords[3]) / 2
for i, row in enumerate(matrix):
for j, item in enumerate(row):
x1, y1 = j * SPACE_SIZE, i * SPACE_SIZE
x2, y2 = (j + 1) * SPACE_SIZE, (i + 1) * SPACE_SIZE
#have two scenarios: obstacle at end and obstacle in middle
if (x1 - 4
Подробнее здесь: [url]https://stackoverflow.com/questions/78415662/circle-on-a-grid-generated-by-canvas-tkinter-moves-wrong-way[/url]