upadte + array_to_window_position

This commit is contained in:
Mateusz Dokowicz 2023-03-22 20:48:02 +01:00
parent e55dbcccea
commit 4933c04bd8

View File

@ -7,11 +7,17 @@ from Interface.movement import robot_movement
# window_dimensions says how many pixels window have
# board_size says how many lines board have
# board_size says how many lines board have in one row
def initial_draw(window_dimensions, board_size):
# window name
pygame.display.set_caption("AI Vacuum Cleaner")
# define additional variables
tile_size = window_width / board_size
# initialize board array
newGrid = Grid(board_size, tile_size)
# set window dimension
window_width = window_dimensions
window_height = window_dimensions
@ -25,9 +31,6 @@ def initial_draw(window_dimensions, board_size):
x = tile_width / 2
y = tile_height / 2
# initialize board array
newGrid = Grid(board_size, tile_width)
# rendering loop
while True:
renderer.start_draw()
@ -57,15 +60,16 @@ class objectOnTile:
self.type = type
# calculate position from array position to window position eg.: array_position = 0 => window_position = 50 (px)
def _translate_array_to_window_position(
position_in_array, window_dimensions, board_size
array_position, window_dimensions, board_size
) -> int:
# TODO calculate position from array position to window position eg.: array_position = 0 => window_position = 50 (px)
return 0
tile_size_window = window_dimensions / board_size
return array_position * tile_size_window + tile_size_window / 2
class Grid:
def __init__(self, size_array, tile_width):
def __init__(self, size_array, tile_size):
self.array = [
[objectOnTile(i, j) for j in range(size_array)] for i in range(size_array)
]
@ -80,12 +84,15 @@ class Grid:
PLAYER_RADIUS_RATIO = 3
PLAYER_COLOR = Colors.RED
# position on screen
render_x = _translate_array_to_window_position(
item.position_x, window_dimensions, board_size
)
render_y = _translate_array_to_window_position(
item.position_y, window_dimensions, board_size
)
# image rendering function
renderer.circle(
render_x,
render_y,
@ -123,4 +130,21 @@ class Grid:
self.list.remove(obj)
# TODO update: update position from (start_x, start_y) to (end_x, end_y)
def update(self, start_x, start_y, end_x, end_y):
# check if obj exist at starting position
if self.array[start_x][start_y].type == acceptedType.EMPTY:
print(
f"Cannot move object at ({start_x}, {start_y}): no object on position"
)
return
# check if destination is empty
if self.array[end_x][end_y].type != acceptedType.EMPTY:
print(
f"Cannot move object to ({end_x}, {end_y}): position already occupied"
)
return
# all OK
self.array[end_x][end_y].type = self.array[start_x][start_y].type
self.array[start_x][start_y].type = acceptedType.EMPTY
# TODO change movement to work with arrays