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 sys
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ def default_color(func):
|
|||||||
result = func(*args, **kwargs)
|
result = func(*args, **kwargs)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
return wrap
|
return wrap
|
||||||
|
|
||||||
|
|
||||||
class GridDraw:
|
class GridDraw:
|
||||||
@ -55,3 +56,6 @@ class GridDraw:
|
|||||||
@default_color
|
@default_color
|
||||||
def circle(self, x, y, radius, color=None):
|
def circle(self, x, y, radius, color=None):
|
||||||
pygame.draw.circle(self.screen, color, (x, y), radius)
|
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
|
from enum import Enum
|
||||||
|
import random
|
||||||
from typing import List
|
from typing import List
|
||||||
from Interface.grid_draw import GridDraw, Colors
|
from Interface.grid_draw import GridDraw, Colors
|
||||||
import sys
|
import sys
|
||||||
@ -16,8 +17,9 @@ def initial_draw(window_dimensions, board_size):
|
|||||||
tile_size = window_dimensions / board_size
|
tile_size = window_dimensions / board_size
|
||||||
|
|
||||||
# initialize board array
|
# 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(1, 1, acceptedType.PLAYER))
|
||||||
|
newGrid.add(objectOnTile(7, 8, acceptedType.ANIMAL))
|
||||||
player = newGrid.findFirst(acceptedType.PLAYER)
|
player = newGrid.findFirst(acceptedType.PLAYER)
|
||||||
newGrid.move(1, 1, 1, 2)
|
newGrid.move(1, 1, 1, 2)
|
||||||
newGrid.move(1, 2, 1, 1)
|
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.move(player.position_x, player.position_y, x, y)
|
||||||
|
|
||||||
newGrid.render(drawer, window_dimensions, board_size)
|
newGrid.render(drawer, newGrid=newGrid)
|
||||||
drawer.end_draw()
|
drawer.end_draw()
|
||||||
pygame.time.delay(30)
|
pygame.time.delay(30)
|
||||||
|
|
||||||
@ -50,6 +52,7 @@ class acceptedType(Enum):
|
|||||||
EMPTY = "empty"
|
EMPTY = "empty"
|
||||||
PLAYER = "player"
|
PLAYER = "player"
|
||||||
RUBBISH = "rubbish"
|
RUBBISH = "rubbish"
|
||||||
|
PLANT = "plant"
|
||||||
ANIMAL = "animal"
|
ANIMAL = "animal"
|
||||||
|
|
||||||
|
|
||||||
@ -68,15 +71,31 @@ def _translate_array_to_window_position(array_position, tile_size_window) -> int
|
|||||||
|
|
||||||
|
|
||||||
class Grid:
|
class Grid:
|
||||||
def __init__(self, size_array):
|
|
||||||
|
def __init__(self, size_array, window_dimensions, board_size):
|
||||||
self.array = [
|
self.array = [
|
||||||
[objectOnTile(i, j) for j in range(size_array)] for i in range(size_array)
|
[objectOnTile(i, j) for j in range(size_array)] for i in range(size_array)
|
||||||
]
|
]
|
||||||
self.list: List[objectOnTile] = []
|
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
|
# render the array
|
||||||
def render(self, drawer: GridDraw, window_dimensions, board_size):
|
def render(self, drawer: GridDraw, newGrid):
|
||||||
tile_size = window_dimensions / board_size
|
#tile_size = window_dimensions / board_size
|
||||||
|
|
||||||
# render object with respect to type
|
# render object with respect to type
|
||||||
for item in self.list:
|
for item in self.list:
|
||||||
@ -87,19 +106,62 @@ class Grid:
|
|||||||
|
|
||||||
# position on screen
|
# position on screen
|
||||||
render_x = _translate_array_to_window_position(
|
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(
|
render_y = _translate_array_to_window_position(
|
||||||
item.position_y, tile_size
|
item.position_y, self.tile_size
|
||||||
)
|
)
|
||||||
|
|
||||||
# image rendering function
|
# image rendering function
|
||||||
drawer.circle(
|
drawer.circle(
|
||||||
render_x,
|
render_x,
|
||||||
render_y,
|
render_y,
|
||||||
tile_size / PLAYER_RADIUS_RATIO,
|
self.tile_size / PLAYER_RADIUS_RATIO,
|
||||||
color=PLAYER_COLOR,
|
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
|
# TODO act accordingly to other options
|
||||||
|
|
||||||
# add new object on grid
|
# add new object on grid
|
||||||
|