from enum import Enum from typing import List from Interface.grid_draw import GridDraw, Colors import sys import pygame from Interface.movement import movement_key_press # 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_dimensions / board_size # initialize board array newGrid = Grid(board_size) newGrid.add(objectOnTile(1, 1, acceptedType.PLAYER)) player = newGrid.findFirst(acceptedType.PLAYER) newGrid.move(1, 1, 1, 2) newGrid.move(1, 2, 1, 1) # set window dimension window_width = window_dimensions window_height = window_dimensions # initialize drawer drawer = GridDraw(window_width, window_height) # rendering loop while True: drawer.start_draw() drawer.board(board_size, board_size) player = newGrid.findFirst(acceptedType.PLAYER) (x, y) = movement_key_press(board_size, player.position_x, player.position_y) newGrid.move(player.position_x, player.position_y, x, y) newGrid.render(drawer, window_dimensions, board_size) drawer.end_draw() pygame.time.delay(30) # 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, tile_size_window) -> int: return array_position * tile_size_window + tile_size_window / 2 class Grid: def __init__(self, size_array): 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, drawer: GridDraw, window_dimensions, board_size): tile_size = window_dimensions / board_size # 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, tile_size ) render_y = _translate_array_to_window_position( item.position_y, tile_size ) # image rendering function drawer.circle( render_x, render_y, tile_size / 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) # deletes object from game # untested, potentially not working def delete(self, position_x: int, position_y: int): # 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) # move: update position from (start_x, start_y) to (end_x, end_y) def move(self, start_x: int, start_y: int, end_x: int, end_y: int): # no change if start_x == end_x and start_y == end_y: return # 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 # change position attribute in array self.array[start_x][start_y].position_x = end_x self.array[start_x][start_y].position_y = end_y # change position in array self.array[end_x][end_y] = self.array[start_x][start_y] self.array[start_x][start_y] = objectOnTile(start_x, start_y) def findFirst(self, find_type: acceptedType) -> objectOnTile: for item in self.list: if item.type == find_type: return item else: print(f"Cannot find object of type: ({find_type})!")