cat #9

Merged
s473593 merged 3 commits from cat into main 2023-03-28 20:11:09 +02:00
3 changed files with 76 additions and 10 deletions
Showing only changes of commit 5818a63e1e - Show all commits

View File

@ -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))

View File

@ -2,7 +2,7 @@ import pygame
import sys
def movement_key_press(board_size, x, y):
def movement_key_press(board_size, x, y, cat_x=None, cat_y=None):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

View File

@ -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