Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_offset_move.py
- import tkinter as tk
- root = tk.Tk()
- root.title("tk_offset_move.py")
- canvas = tk.Canvas(root, width=300, height=300)
- canvas.pack()
- directions = right, down, left, up = ['Right', 'Down', 'Left', 'Up']
- directions_dict = {right: (1, 0), down: (0, 1), left: (-1, 0), up: (0, -1)}
- direction = right
- matrix_str = """
- 00000
- 0ABC0
- 0DEF0
- 0GHI0
- 00000
- """
- matrix = [list(row) for row in matrix_str.strip().split('\n')]
- target = 'B'
- c0 = len(matrix[0])
- r0 = len(matrix)
- flat = matrix_str.replace('\n', '')
- idx = flat.find(target)
- x, y = idx % c0, idx // c0
- cell_size = 60
- padding = cell_size // 2
- for i in range(r0):
- for j in range(c0):
- text = matrix[i][j]
- canvas.create_text(
- j * cell_size + cell_size // 2,
- i * cell_size + cell_size // 2,
- text=text,
- font=("Arial", 12)
- )
- def chk():
- sight = []
- for dir in [(0, 0), (1, 0), (0, 1), (1, 1)]:
- dx, dy = dir
- new_x = x + dx
- new_y = y + dy
- t = matrix[new_y][new_x]
- sight.append(t)
- print(f"Current Position: ({x}, {y}), Neighbors: {sight}")
- def on_key_press(event):
- global x, y
- key = event.keysym
- if key in directions_dict:
- dx, dy = directions_dict[key]
- new_x = x + dx
- new_y = y + dy
- if 0 <= new_x < c0 - 1 and 0 <= new_y < r0 - 1:
- x, y = new_x, new_y
- chk()
- update_canvas()
- else:
- print(f"Cannot Move {key}: Out Of Bounds")
- def update_canvas():
- canvas.delete("dot")
- canvas.create_oval(
- padding + x * cell_size + cell_size // 4,
- padding + y * cell_size + cell_size // 4,
- padding + x * cell_size + 3 * cell_size // 4,
- padding + y * cell_size + 3 * cell_size // 4,
- fill="orange",
- tags=('dot',)
- )
- root.bind('<Key>', on_key_press)
- chk()
- update_canvas()
- debug_path = [
- (2, 0), (3, 0), (3, 1), (3, 2), (3, 3), (2, 3), (2, 2),
- (1, 2), (1, 3), (0, 3), (0, 2), (0, 1), (1, 1), (1, 0)
- ]
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement