Machine_learning_2023/Interface/vacuum_render.py

145 lines
4.7 KiB
Python
Raw Normal View History

2023-03-20 01:14:48 +01:00
from enum import Enum
from typing import List
2023-03-12 16:22:51 +01:00
from Interface.grid_draw import GridDraw, Colors
2023-03-12 18:11:31 +01:00
import sys
import pygame
2023-03-16 21:55:05 +01:00
from Interface.movement import robot_movement
2023-03-11 19:31:43 +01:00
2023-03-16 20:33:22 +01:00
2023-03-19 22:00:10 +01:00
# window_dimensions says how many pixels window have
2023-03-22 20:48:02 +01:00
# board_size says how many lines board have in one row
2023-03-19 22:00:10 +01:00
def initial_draw(window_dimensions, board_size):
2023-03-16 21:38:42 +01:00
# window name
pygame.display.set_caption("AI Vacuum Cleaner")
2023-03-22 20:48:02 +01:00
# define additional variables
2023-03-22 23:11:13 +01:00
tile_size = window_dimensions / board_size
2023-03-22 20:48:02 +01:00
# initialize board array
2023-03-22 23:11:13 +01:00
newGrid = Grid(board_size)
newGrid.add(objectOnTile(1, 1, acceptedType.PLAYER))
2023-03-22 20:48:02 +01:00
2023-03-16 21:38:42 +01:00
# set window dimension
2023-03-19 22:00:10 +01:00
window_width = window_dimensions
window_height = window_dimensions
2023-03-16 21:38:42 +01:00
# FIXME @countingthedots: please tell me what is going on there and why???
2023-03-19 22:00:10 +01:00
#
2023-03-20 01:14:48 +01:00
renderer = GridDraw(window_width, window_height)
2023-03-16 20:33:22 +01:00
# rendering loop
2023-03-12 16:22:51 +01:00
while True:
2023-03-20 01:14:48 +01:00
renderer.start_draw()
renderer.board(board_size, board_size)
2023-03-22 23:11:13 +01:00
newGrid.render(renderer, window_dimensions, board_size)
2023-03-20 01:14:48 +01:00
renderer.end_draw()
2023-03-22 23:11:13 +01:00
pygame.time.delay(30)
2023-03-20 01:14:48 +01:00
# 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
2023-03-22 20:48:02 +01:00
# calculate position from array position to window position eg.: array_position = 0 => window_position = 50 (px)
2023-03-22 23:11:13 +01:00
def _translate_array_to_window_position(array_position, tile_size_window) -> int:
2023-03-22 20:48:02 +01:00
return array_position * tile_size_window + tile_size_window / 2
2023-03-20 01:14:48 +01:00
class Grid:
2023-03-22 23:11:13 +01:00
def __init__(self, size_array):
2023-03-20 01:14:48 +01:00
self.array = [
[objectOnTile(i, j) for j in range(size_array)] for i in range(size_array)
]
self.list: List[objectOnTile] = []
# render the array
2023-03-22 23:11:13 +01:00
def render(self, renderer: GridDraw, window_dimensions, board_size):
tile_size = window_dimensions / board_size
2023-03-20 01:14:48 +01:00
# 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
2023-03-22 20:48:02 +01:00
# position on screen
2023-03-20 01:14:48 +01:00
render_x = _translate_array_to_window_position(
2023-03-22 23:11:13 +01:00
item.position_x, tile_size
2023-03-20 01:14:48 +01:00
)
render_y = _translate_array_to_window_position(
2023-03-22 23:11:13 +01:00
item.position_y, tile_size
2023-03-20 01:14:48 +01:00
)
2023-03-22 20:48:02 +01:00
# image rendering function
2023-03-20 01:14:48 +01:00
renderer.circle(
render_x,
render_y,
2023-03-22 23:11:13 +01:00
tile_size / PLAYER_RADIUS_RATIO,
2023-03-20 01:14:48 +01:00
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)
2023-03-22 20:48:02 +01:00
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
2023-03-20 01:14:48 +01:00
# TODO change movement to work with arrays