diff --git a/agent.py b/agent.py index a39f4e8..bb463e2 100644 --- a/agent.py +++ b/agent.py @@ -16,15 +16,15 @@ class Agent: self.x += dx self.y += dy - def handle_event(self, event, grid_height,grid_width, animals): + def handle_event(self, event, grid_height,grid_width, animals, blocked_fields): if event.type == pygame.KEYDOWN: - if event.key == pygame.K_UP and self.y > 0: + if event.key == pygame.K_UP and self.y > 0 and (self.x, self.y-1) not in blocked_fields: self.move(0, -1) - elif event.key == pygame.K_DOWN and self.y < grid_height - 1: + elif event.key == pygame.K_DOWN and self.y < grid_height - 1 and (self.x, self.y+1) not in blocked_fields: self.move(0, 1) - elif event.key == pygame.K_LEFT and self.x > 0: + elif event.key == pygame.K_LEFT and self.x > 0 and (self.x-1, self.y) not in blocked_fields: self.move(-1, 0) - elif event.key == pygame.K_RIGHT and self.x < grid_width - 1: + elif event.key == pygame.K_RIGHT and self.x < grid_width - 1 and (self.x+1, self.y) not in blocked_fields: self.move(1, 0) for animal in animals: diff --git a/enclosure.py b/enclosure.py new file mode 100644 index 0000000..dc80a67 --- /dev/null +++ b/enclosure.py @@ -0,0 +1,58 @@ +import pygame + +class Enclosure: + def __init__(self, x1, y1, x2, y2, type, imageH, imageV): + self.x1 = x1 - 1 + self.y1 = y1 - 1 + #(x1,y1) - wierzchołek przekątnej + self.x2 = x2 - 1 + self.y2 = y2 - 1 + #(x2,y2) - 2 wierzchołek przekątnej + self.type = type #hot/cold/medium + self.imageH = imageH + self.imageV = imageV + + def draw(self, screen, grid_size, blocked_fields): + self.imageH = pygame.transform.scale(self.imageH, (grid_size, grid_size)) + self.imageV = pygame.transform.scale(self.imageV, (grid_size, grid_size)) + if self.x1 < self.x2: + for i in range(self.x1, self.x2+1): + screen.blit(self.imageH, (i * grid_size, self.y1 * grid_size)) + blocked_fields.add((i, self.y1)) + screen.blit(self.imageH, (i * grid_size, self.y2 * grid_size)) + blocked_fields.add((i, self.y2)) + if self.y1 < self.y2: + for j in range(self.y1, self.y2+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + if self.y1 > self.y2: + for j in range(self.y2, self.y1+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + if self.x1 > self.x2: + for i in range(self.x2, self.x1+1): + screen.blit(self.imageH, (i * grid_size, self.y1 * grid_size)) + blocked_fields.add((i, self.y1)) + screen.blit(self.imageH, (i * grid_size, self.y2 * grid_size)) + blocked_fields.add((i, self.y2)) + if self.y1 < self.y2: + for j in range(self.y1, self.y2+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + if self.y1 > self.y2: + for j in range(self.y2, self.y1+1): + screen.blit(self.imageH, (self.x1 * grid_size, j * grid_size)) + blocked_fields.add((self.x1, j)) + screen.blit(self.imageH, (self.x2 * grid_size, j * grid_size)) + blocked_fields.add((self.x2, j)) + + + + + diff --git a/images/fenceHor.png b/images/fenceHor.png new file mode 100644 index 0000000..8beb902 Binary files /dev/null and b/images/fenceHor.png differ diff --git a/images/fenceVer.png b/images/fenceVer.png new file mode 100644 index 0000000..2699618 Binary files /dev/null and b/images/fenceVer.png differ diff --git a/main.py b/main.py index bf1c208..ca708be 100644 --- a/main.py +++ b/main.py @@ -6,12 +6,13 @@ from penguin import Penguin from parrot import Parrot from bear import Bear from agent import Agent +from enclosure import Enclosure BLACK = (0, 0, 0) GRID_SIZE = 50 -GRID_WIDTH = 20 -GRID_HEIGHT = 10 +GRID_WIDTH = 30 +GRID_HEIGHT = 15 pygame.init() @@ -23,8 +24,11 @@ pygame.display.set_caption("Mini Zoo") background_image = pygame.image.load('images/tło.jpg') background_image = pygame.transform.scale(background_image, WINDOW_SIZE) +fenceH = pygame.image.load('images/fenceHor.png') +fenceV = pygame.image.load('images/fenceVer.png') +blocked = set() @@ -37,13 +41,25 @@ an4 = Elephant(4,3) Animals = [an1, an2, an3, an4, old_an1, old_an2] +en1 = Enclosure(1,5,9,11,"medium", fenceH, fenceV) +en2 = Enclosure(29,3, 13,1, 'medium', fenceH, fenceV) +en3 = Enclosure(11,5, 16,11, 'cold', fenceH, fenceV) +en4 = Enclosure(19,11, 30,5, 'hot', fenceH, fenceV) +en5 = Enclosure(4,13, 28,15, 'cold', fenceH, fenceV) + + +Enclosures = [en1, en2, en3, en4, en5] + + def draw_grid(): for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE): for x in range(0, GRID_WIDTH * GRID_SIZE, GRID_SIZE): rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE) pygame.draw.rect(screen, BLACK, rect, 1) - +def draw_enclosures(set): + for enclosure in Enclosures: + enclosure.draw(screen, GRID_SIZE, blocked) def draw_Animals(): for Animal in Animals: @@ -64,13 +80,14 @@ def main(): if event.type == pygame.QUIT: pygame.quit() sys.exit() - agent.handle_event(event,GRID_HEIGHT,GRID_WIDTH,Animals) + agent.handle_event(event,GRID_HEIGHT,GRID_WIDTH,Animals, blocked) screen.blit(background_image,(0,0)) draw_grid() - draw_Animals() + draw_enclosures(blocked) + #draw_Animals()