feat: Added Interactable abstract class and example implementation
This commit is contained in:
parent
6fafc26ff8
commit
eeefc150bb
13
app.py
13
app.py
@ -1,6 +1,6 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import prefs
|
import prefs
|
||||||
import random
|
from classes.beerKeg import BeerKeg
|
||||||
from pygame.locals import K_w, K_s, K_a, K_d
|
from pygame.locals import K_w, K_s, K_a, K_d
|
||||||
from classes.cell import Cell
|
from classes.cell import Cell
|
||||||
from classes.agent import Agent
|
from classes.agent import Agent
|
||||||
@ -17,8 +17,7 @@ def initBoard():
|
|||||||
cell = Cell(i, j)
|
cell = Cell(i, j)
|
||||||
row.append(cell)
|
row.append(cell)
|
||||||
cells.append(row)
|
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ść
|
# Na potrzeby prezentacji tworzę sobie prostokatne sciany na które nie da się wejść
|
||||||
x1 = 3
|
x1 = 3
|
||||||
@ -28,13 +27,17 @@ def initBoard():
|
|||||||
cells[i][j].prepareTexture("sprites/wall.png")
|
cells[i][j].prepareTexture("sprites/wall.png")
|
||||||
cells[i][j].blocking_movement = True
|
cells[i][j].blocking_movement = True
|
||||||
|
|
||||||
|
cells[6][6].interactableItem = BeerKeg(cells[6][6], "Beer Keg")
|
||||||
|
|
||||||
def draw_grid(window, cells):
|
def draw_grid(window, cells):
|
||||||
for i in range(prefs.GRID_SIZE):
|
for i in range(prefs.GRID_SIZE):
|
||||||
for j in range(prefs.GRID_SIZE):
|
for j in range(prefs.GRID_SIZE):
|
||||||
cells[i][j].draw(window)
|
cells[i][j].draw(window)
|
||||||
|
if(cells[i][j].interactableItem):
|
||||||
|
cells[i][j].interactableItem.draw(window)
|
||||||
|
|
||||||
initBoard()
|
initBoard()
|
||||||
|
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
@ -54,6 +57,10 @@ while running:
|
|||||||
agent.move_right()
|
agent.move_right()
|
||||||
if not any([keys[K_w], keys[K_s], keys[K_a], keys[K_d]]):
|
if not any([keys[K_w], keys[K_s], keys[K_a], keys[K_d]]):
|
||||||
agent.moved = False
|
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))
|
window.fill((255, 0, 0))
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import prefs
|
import prefs
|
||||||
class Agent:
|
class Agent:
|
||||||
def __init__(self, x, y, cells):
|
def __init__(self, x, y, cells, baseScore=0):
|
||||||
self.sprite = pygame.image.load("sprites/Chef64new.png").convert_alpha()
|
self.sprite = pygame.image.load("sprites/BartenderNew64.png").convert_alpha()
|
||||||
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
|
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
|
||||||
self.current_cell = cells[x][y]
|
self.current_cell = cells[x][y]
|
||||||
self.moved=False
|
self.moved=False
|
||||||
self.last_move_time=pygame.time.get_ticks()
|
self.last_move_time=pygame.time.get_ticks()
|
||||||
|
self.last_interact_time = pygame.time.get_ticks()
|
||||||
self.cells = cells
|
self.cells = cells
|
||||||
|
self.score = baseScore
|
||||||
|
|
||||||
def move_up(self):
|
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:
|
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):
|
def draw(self, surface):
|
||||||
surface.blit(self.sprite, (self.current_cell.X * prefs.CELL_SIZE,
|
surface.blit(self.sprite, (self.current_cell.X * prefs.CELL_SIZE,
|
||||||
self.current_cell.Y * 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
19
classes/beerKeg.py
Normal 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
12
classes/interactable.py
Normal 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))
|
Loading…
Reference in New Issue
Block a user