dodano klatki na wybiegach, obiekty blokują scieżkę
This commit is contained in:
parent
2e4ba023d0
commit
12b431e748
10
agent.py
10
agent.py
@ -16,15 +16,15 @@ class Agent:
|
|||||||
self.x += dx
|
self.x += dx
|
||||||
self.y += dy
|
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.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)
|
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)
|
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)
|
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)
|
self.move(1, 0)
|
||||||
|
|
||||||
for animal in animals:
|
for animal in animals:
|
||||||
|
58
enclosure.py
Normal file
58
enclosure.py
Normal file
@ -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))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
BIN
images/fenceHor.png
Normal file
BIN
images/fenceHor.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
BIN
images/fenceVer.png
Normal file
BIN
images/fenceVer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 70 KiB |
27
main.py
27
main.py
@ -6,12 +6,13 @@ from penguin import Penguin
|
|||||||
from parrot import Parrot
|
from parrot import Parrot
|
||||||
from bear import Bear
|
from bear import Bear
|
||||||
from agent import Agent
|
from agent import Agent
|
||||||
|
from enclosure import Enclosure
|
||||||
|
|
||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
|
|
||||||
GRID_SIZE = 50
|
GRID_SIZE = 50
|
||||||
GRID_WIDTH = 20
|
GRID_WIDTH = 30
|
||||||
GRID_HEIGHT = 10
|
GRID_HEIGHT = 15
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
@ -23,8 +24,11 @@ pygame.display.set_caption("Mini Zoo")
|
|||||||
|
|
||||||
background_image = pygame.image.load('images/tło.jpg')
|
background_image = pygame.image.load('images/tło.jpg')
|
||||||
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
|
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]
|
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():
|
def draw_grid():
|
||||||
for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE):
|
for y in range(0, GRID_HEIGHT * GRID_SIZE, GRID_SIZE):
|
||||||
for x in range(0, GRID_WIDTH * GRID_SIZE, GRID_SIZE):
|
for x in range(0, GRID_WIDTH * GRID_SIZE, GRID_SIZE):
|
||||||
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
|
rect = pygame.Rect(x, y, GRID_SIZE, GRID_SIZE)
|
||||||
pygame.draw.rect(screen, BLACK, rect, 1)
|
pygame.draw.rect(screen, BLACK, rect, 1)
|
||||||
|
|
||||||
|
def draw_enclosures(set):
|
||||||
|
for enclosure in Enclosures:
|
||||||
|
enclosure.draw(screen, GRID_SIZE, blocked)
|
||||||
|
|
||||||
def draw_Animals():
|
def draw_Animals():
|
||||||
for Animal in Animals:
|
for Animal in Animals:
|
||||||
@ -64,13 +80,14 @@ def main():
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
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))
|
screen.blit(background_image,(0,0))
|
||||||
draw_grid()
|
draw_grid()
|
||||||
draw_Animals()
|
draw_enclosures(blocked)
|
||||||
|
#draw_Animals()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user