moving cat added

This commit is contained in:
countingthedots 2023-03-28 19:36:57 +02:00
parent ad828cb7d1
commit 5818a63e1e
3 changed files with 76 additions and 10 deletions

View File

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

View File

@ -2,7 +2,7 @@ import pygame
import sys 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(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
sys.exit() sys.exit()

View File

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