feat: Added Interactable abstract class and example implementation

This commit is contained in:
Jager72 2024-03-14 13:53:26 +01:00
parent 6fafc26ff8
commit eeefc150bb
4 changed files with 50 additions and 5 deletions

13
app.py
View File

@ -1,6 +1,6 @@
import pygame
import prefs
import random
from classes.beerKeg import BeerKeg
from pygame.locals import K_w, K_s, K_a, K_d
from classes.cell import Cell
from classes.agent import Agent
@ -17,8 +17,7 @@ def initBoard():
cell = Cell(i, j)
row.append(cell)
cells.append(row)
global agent
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
# Na potrzeby prezentacji tworzę sobie prostokatne sciany na które nie da się wejść
x1 = 3
@ -28,13 +27,17 @@ def initBoard():
cells[i][j].prepareTexture("sprites/wall.png")
cells[i][j].blocking_movement = True
cells[6][6].interactableItem = BeerKeg(cells[6][6], "Beer Keg")
def draw_grid(window, cells):
for i in range(prefs.GRID_SIZE):
for j in range(prefs.GRID_SIZE):
cells[i][j].draw(window)
if(cells[i][j].interactableItem):
cells[i][j].interactableItem.draw(window)
initBoard()
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
running = True
while running:
for event in pygame.event.get():
@ -54,6 +57,10 @@ while running:
agent.move_right()
if not any([keys[K_w], keys[K_s], keys[K_a], keys[K_d]]):
agent.moved = False
if pygame.key.get_pressed()[pygame.K_e]:
if agent.current_cell.interactableItem and pygame.time.get_ticks() - agent.last_interact_time > 500:
agent.last_interact_time = pygame.time.get_ticks()
agent.current_cell.interactableItem.interact(agent)
window.fill((255, 0, 0))

View File

@ -1,13 +1,15 @@
import pygame
import prefs
class Agent:
def __init__(self, x, y, cells):
self.sprite = pygame.image.load("sprites/Chef64new.png").convert_alpha()
def __init__(self, x, y, cells, baseScore=0):
self.sprite = pygame.image.load("sprites/BartenderNew64.png").convert_alpha()
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
self.current_cell = cells[x][y]
self.moved=False
self.last_move_time=pygame.time.get_ticks()
self.last_interact_time = pygame.time.get_ticks()
self.cells = cells
self.score = baseScore
def move_up(self):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y > 0 and not self.cells[self.current_cell.X][self.current_cell.Y-1].blocking_movement:
@ -36,3 +38,8 @@ class Agent:
def draw(self, surface):
surface.blit(self.sprite, (self.current_cell.X * prefs.CELL_SIZE,
self.current_cell.Y * prefs.CELL_SIZE))
def increase_score(self, amount):
self.score += amount
print("Agent score changed from {} to {}".format(self.score - amount, self.score))

19
classes/beerKeg.py Normal file
View File

@ -0,0 +1,19 @@
from classes.interactable import Interactable
import pygame
import prefs
from classes.agent import Agent
class BeerKeg(Interactable):
def __init__(self, cell, name, sprite="sprites/BeerKeg.png"):
super().__init__(cell, sprite)
self.sprite = pygame.image.load(sprite).convert_alpha()
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
self.name = name
def interact(self,agent):
if not isinstance(agent, Agent):
print("Agent must be of type Agent")
return
agent.increase_score(10)
print("Agent interacted with {}".format(self.name))

12
classes/interactable.py Normal file
View File

@ -0,0 +1,12 @@
import prefs
class Interactable:
def __init__(self, cell, sprite):
self.cell = cell
self.sprite = sprite
def interact(self):
"""Ta metoda powinna być przysłonięta przez klasę dziedziczącą."""
raise NotImplementedError
def draw(self, surface):
surface.blit(self.sprite, (self.cell.X * prefs.CELL_SIZE,
self.cell.Y * prefs.CELL_SIZE))