diff --git a/Interface/grid_draw.py b/Interface/grid_draw.py index e3900e7..250ac92 100644 --- a/Interface/grid_draw.py +++ b/Interface/grid_draw.py @@ -1,3 +1,4 @@ +import random import sys import pygame @@ -19,7 +20,7 @@ def default_color(func): result = func(*args, **kwargs) return result - return wrap + return wrap class GridDraw: @@ -55,3 +56,6 @@ class GridDraw: @default_color def circle(self, x, y, radius, color=None): pygame.draw.circle(self.screen, color, (x, y), radius) + + def image(self, x, y, image): + self.screen.blit(image, (x, y)) diff --git a/Interface/images/cat/standing_back.png b/Interface/images/cat/standing_back.png new file mode 100644 index 0000000..40956bf Binary files /dev/null and b/Interface/images/cat/standing_back.png differ diff --git a/Interface/images/cat/standing_front.png b/Interface/images/cat/standing_front.png new file mode 100644 index 0000000..2ac41f3 Binary files /dev/null and b/Interface/images/cat/standing_front.png differ diff --git a/Interface/images/cat/standing_left.png b/Interface/images/cat/standing_left.png new file mode 100644 index 0000000..3f2db69 Binary files /dev/null and b/Interface/images/cat/standing_left.png differ diff --git a/Interface/images/cat/standing_right.png b/Interface/images/cat/standing_right.png new file mode 100644 index 0000000..95914b7 Binary files /dev/null and b/Interface/images/cat/standing_right.png differ diff --git a/Interface/images/plant1.jpg b/Interface/images/plant1.jpg new file mode 100644 index 0000000..383ef9e Binary files /dev/null and b/Interface/images/plant1.jpg differ diff --git a/Interface/images/plant2.jpg b/Interface/images/plant2.jpg new file mode 100644 index 0000000..e55fe6a Binary files /dev/null and b/Interface/images/plant2.jpg differ diff --git a/Interface/images/plant3.jpg b/Interface/images/plant3.jpg new file mode 100644 index 0000000..f2fee9f Binary files /dev/null and b/Interface/images/plant3.jpg differ diff --git a/Interface/vacuum_render.py b/Interface/vacuum_render.py index a4fca0d..903c3e4 100644 --- a/Interface/vacuum_render.py +++ b/Interface/vacuum_render.py @@ -1,4 +1,5 @@ from enum import Enum +import random from typing import List from Interface.grid_draw import GridDraw, Colors import sys @@ -16,8 +17,9 @@ def initial_draw(window_dimensions, board_size): tile_size = window_dimensions / board_size # initialize board array - newGrid = Grid(board_size) + newGrid = Grid(board_size, window_dimensions=window_dimensions, board_size=board_size) newGrid.add(objectOnTile(1, 1, acceptedType.PLAYER)) + newGrid.add(objectOnTile(7, 8, acceptedType.ANIMAL)) player = newGrid.findFirst(acceptedType.PLAYER) newGrid.move(1, 1, 1, 2) newGrid.move(1, 2, 1, 1) @@ -40,7 +42,7 @@ def initial_draw(window_dimensions, board_size): newGrid.move(player.position_x, player.position_y, x, y) - newGrid.render(drawer, window_dimensions, board_size) + newGrid.render(drawer, newGrid=newGrid) drawer.end_draw() pygame.time.delay(30) @@ -50,6 +52,7 @@ class acceptedType(Enum): EMPTY = "empty" PLAYER = "player" RUBBISH = "rubbish" + PLANT = "plant" ANIMAL = "animal" @@ -68,15 +71,31 @@ def _translate_array_to_window_position(array_position, tile_size_window) -> int class Grid: - def __init__(self, size_array): + + def __init__(self, size_array, window_dimensions, board_size): self.array = [ [objectOnTile(i, j) for j in range(size_array)] for i in range(size_array) ] self.list: List[objectOnTile] = [] + self.tile_size = window_dimensions / board_size + + self.cat_last_tick = pygame.time.get_ticks() + self.cat_cooldown = 1000 + self.cat_velocity = 1 + self.cat_busy = False + + #region images + self.cat_front_image = pygame.transform.scale(pygame.image.load("Interface/images/cat/standing_front.png"), (self.tile_size, self.tile_size)) + self.cat_back_image = pygame.transform.scale(pygame.image.load("Interface/images/cat/standing_back.png"), (self.tile_size, self.tile_size)) + self.cat_left_image = pygame.transform.scale(pygame.image.load("Interface/images/cat/standing_left.png"), (self.tile_size, self.tile_size)) + self.cat_right_image = pygame.transform.scale(pygame.image.load("Interface/images/cat/standing_right.png"), (self.tile_size, self.tile_size)) + self.cat_current_image = self.cat_front_image + #endregion + # render the array - def render(self, drawer: GridDraw, window_dimensions, board_size): - tile_size = window_dimensions / board_size + def render(self, drawer: GridDraw, newGrid): + #tile_size = window_dimensions / board_size # render object with respect to type for item in self.list: @@ -87,19 +106,62 @@ class Grid: # position on screen render_x = _translate_array_to_window_position( - item.position_x, tile_size + item.position_x, self.tile_size ) render_y = _translate_array_to_window_position( - item.position_y, tile_size + item.position_y, self.tile_size ) # image rendering function drawer.circle( render_x, render_y, - tile_size / PLAYER_RADIUS_RATIO, + self.tile_size / PLAYER_RADIUS_RATIO, color=PLAYER_COLOR, ) + if item.type == acceptedType.ANIMAL: + now = pygame.time.get_ticks() + #region cat random movement + if now - self.cat_last_tick >= self.cat_cooldown: + if self.cat_busy == False: + self.cat_direction = random.randint(0,3) + + if self.cat_direction == 0: #up + if self.cat_current_image == self.cat_back_image: + newGrid.move(item.position_x, item.position_y, item.position_x, item.position_y - 1) + self.cat_busy = False + else: + self.cat_busy = True + self.cat_current_image = self.cat_back_image + if self.cat_direction == 1: #right + if self.cat_current_image == self.cat_right_image: + newGrid.move(item.position_x, item.position_y, item.position_x + 1, item.position_y) + self.cat_busy = False + else: + self.cat_busy = True + self.cat_current_image = self.cat_right_image + if self.cat_direction == 2: #down + if self.cat_current_image == self.cat_front_image: + newGrid.move(item.position_x, item.position_y, item.position_x, item.position_y + 1) + self.cat_busy = False + else: + self.cat_busy = True + self.cat_current_image = self.cat_front_image + if self.cat_direction == 3: #left + if self.cat_current_image == self.cat_left_image: + newGrid.move(item.position_x, item.position_y, item.position_x - 1, item.position_y) + self.cat_busy = False + else: + self.cat_busy = True + self.cat_current_image = self.cat_left_image + self.cat_last_tick = pygame.time.get_ticks() + #endregion + + render_x = item.position_x * self.tile_size + render_y = item.position_y * self.tile_size + + drawer.image(render_x, render_y, self.cat_current_image) + # TODO act accordingly to other options # add new object on grid