Added a cat that can move around a world
Reviewed-on: #9 Reviewed-by: Tim Barvenov <timbar@st.amu.edu.pl>
@ -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))
|
||||
|
BIN
Interface/images/cat/standing_back.png
Normal file
After Width: | Height: | Size: 621 B |
BIN
Interface/images/cat/standing_front.png
Normal file
After Width: | Height: | Size: 765 B |
BIN
Interface/images/cat/standing_left.png
Normal file
After Width: | Height: | Size: 411 B |
BIN
Interface/images/cat/standing_right.png
Normal file
After Width: | Height: | Size: 584 B |
BIN
Interface/images/plant1.jpg
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
Interface/images/plant2.jpg
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
Interface/images/plant3.jpg
Normal file
After Width: | Height: | Size: 39 KiB |
@ -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
|
||||
|