Machine_learning_2023/Interface/vacuum_render.py
2023-03-22 20:48:02 +01:00

151 lines
5.0 KiB
Python

from enum import Enum
from typing import List
from Interface.grid_draw import GridDraw, Colors
import sys
import pygame
from Interface.movement import robot_movement
# window_dimensions says how many pixels window 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
# FIXME @countingthedots: please tell me what is going on there and why???
#
renderer = GridDraw(window_width, window_height)
tile_width = window_width / board_size
tile_height = window_height / board_size
radius = tile_width / 3
x = tile_width / 2
y = tile_height / 2
# rendering loop
while True:
renderer.start_draw()
renderer.board(board_size, board_size)
(x, y) = robot_movement(
window_width, window_height, tile_width, tile_height, x, y
)
renderer.circle(x, y, radius, color=Colors.RED)
renderer.end_draw()
pygame.time.delay(10)
# TODO wrap it all to another file that handles array rendering
class acceptedType(Enum):
EMPTY = "empty"
PLAYER = "player"
RUBBISH = "rubbish"
ANIMAL = "animal"
class objectOnTile:
def __init__(
self, position_x: int, position_y: int, type: acceptedType = acceptedType.EMPTY
):
self.position_x = position_x
self.position_y = position_y
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(
array_position, window_dimensions, board_size
) -> int:
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_size):
self.array = [
[objectOnTile(i, j) for j in range(size_array)] for i in range(size_array)
]
self.list: List[objectOnTile] = []
# render the array
def render(self, renderer: GridDraw, window_dimensions, board_size, tile_width):
# render object with respect to type
for item in self.list:
if item.type == acceptedType.PLAYER:
# constants for player
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,
tile_width / PLAYER_RADIUS_RATIO,
color=PLAYER_COLOR,
)
# TODO act accordingly to other options
# add new object on grid
def add(self, newObject: objectOnTile):
if (
self.array[newObject.position_x][newObject.position_y].type
!= acceptedType.EMPTY
):
print(
f"Cannot add object at ({newObject.position_x}, {newObject.position_y}): position already occupied"
)
return
self.array[newObject.position_x][newObject.position_y] = newObject
self.list.append(newObject)
def delete(self, position_x, position_y):
# Find the object with the given position in the list
for obj in self.list:
if obj.position_x == position_x and obj.position_y == position_y:
break
else: # No object found with the given position
print(f"No object found at ({position_x}, {position_y})")
return
# Remove the object from both the array and the list
self.array[position_x][position_y] = objectOnTile(position_x, position_y)
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