Array Grid
This commit is contained in:
parent
101136b0b7
commit
e37a889e1e
@ -10,7 +10,6 @@ class Colors:
|
|||||||
|
|
||||||
|
|
||||||
DEFAULT_COLOR = Colors.WHITE
|
DEFAULT_COLOR = Colors.WHITE
|
||||||
RADIUS_SIZE_COEFFICIENT = 3
|
|
||||||
|
|
||||||
|
|
||||||
def default_color(func):
|
def default_color(func):
|
||||||
@ -54,6 +53,5 @@ class GridDraw:
|
|||||||
self.line(0, tiles_height * i, self.width, tiles_height * i, color=color)
|
self.line(0, tiles_height * i, self.width, tiles_height * i, color=color)
|
||||||
|
|
||||||
@default_color
|
@default_color
|
||||||
def circle(self, x, y, tile_height, color=None):
|
def circle(self, x, y, radius, color=None):
|
||||||
radius = tile_height / RADIUS_SIZE_COEFFICIENT
|
|
||||||
pygame.draw.circle(self.screen, color, (x, y), radius)
|
pygame.draw.circle(self.screen, color, (x, y), radius)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from enum import Enum
|
||||||
|
from typing import List
|
||||||
from Interface.grid_draw import GridDraw, Colors
|
from Interface.grid_draw import GridDraw, Colors
|
||||||
import sys
|
import sys
|
||||||
import pygame
|
import pygame
|
||||||
@ -10,29 +12,115 @@ def initial_draw(window_dimensions, board_size):
|
|||||||
# window name
|
# window name
|
||||||
pygame.display.set_caption("AI Vacuum Cleaner")
|
pygame.display.set_caption("AI Vacuum Cleaner")
|
||||||
|
|
||||||
# define array for grid
|
|
||||||
board_array = [["" for j in range(board_size)] for i in range(board_size)]
|
|
||||||
board_render(board_array)
|
|
||||||
|
|
||||||
# set window dimension
|
# set window dimension
|
||||||
window_width = window_dimensions
|
window_width = window_dimensions
|
||||||
window_height = window_dimensions
|
window_height = window_dimensions
|
||||||
|
|
||||||
# FIXME @countingthedots: please tell me what is going on there and why???
|
# FIXME @countingthedots: please tell me what is going on there and why???
|
||||||
#
|
#
|
||||||
grid = GridDraw(window_width, window_height)
|
renderer = GridDraw(window_width, window_height)
|
||||||
tile_width = window_width / board_size
|
tile_width = window_width / board_size
|
||||||
tile_height = window_height / board_size
|
tile_height = window_height / board_size
|
||||||
|
radius = tile_width / 3
|
||||||
x = tile_width / 2
|
x = tile_width / 2
|
||||||
y = tile_height / 2
|
y = tile_height / 2
|
||||||
|
|
||||||
|
# initialize board array
|
||||||
|
newGrid = Grid(board_size, tile_width)
|
||||||
|
|
||||||
# rendering loop
|
# rendering loop
|
||||||
while True:
|
while True:
|
||||||
grid.start_draw()
|
renderer.start_draw()
|
||||||
grid.board(board_size, board_size)
|
renderer.board(board_size, board_size)
|
||||||
(x, y) = robot_movement(
|
(x, y) = robot_movement(
|
||||||
window_width, window_height, tile_width, tile_height, x, y
|
window_width, window_height, tile_width, tile_height, x, y
|
||||||
)
|
)
|
||||||
grid.circle(x, y, tile_height, color=Colors.RED)
|
renderer.circle(x, y, radius, color=Colors.RED)
|
||||||
grid.end_draw()
|
renderer.end_draw()
|
||||||
pygame.time.delay(10)
|
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
|
||||||
|
|
||||||
|
|
||||||
|
def _translate_array_to_window_position(
|
||||||
|
position_in_array, window_dimensions, board_size
|
||||||
|
) -> int:
|
||||||
|
# TODO calculate position from array position to window position eg.: array_position = 0 => window_position = 50 (px)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
class Grid:
|
||||||
|
def __init__(self, size_array, tile_width):
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
# TODO change movement to work with arrays
|
||||||
|
Loading…
Reference in New Issue
Block a user