Male_zoo_Projekt_SI/main.py

90 lines
2.4 KiB
Python
Raw Normal View History

import pygame
import sys
from animal import Animal
2024-03-10 17:40:26 +01:00
from adult_animal import AdultAnimal
BLACK = (0, 0, 0)
GRID_SIZE = 100
GRID_WIDTH = 20
GRID_HEIGHT = 10
pygame.init()
WINDOW_SIZE = (GRID_WIDTH * GRID_SIZE, GRID_HEIGHT * GRID_SIZE)
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Mini Zoo")
agent_pos = [0,0]
agent_image = pygame.image.load('avatar.png')
agent_image = pygame.transform.scale(agent_image, (GRID_SIZE,GRID_SIZE))
background_image = pygame.image.load('tło.jpg')
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)
animal_image = pygame.image.load('elephant.png')
animal_image = pygame.transform.scale(animal_image, (GRID_SIZE, GRID_SIZE))
an1=Animal(10,1,animal_image)
an2=Animal(12,1,animal_image)
an3=Animal(14,7,animal_image)
2024-03-10 17:40:26 +01:00
old_an1=AdultAnimal(3,6, animal_image,width=2,height=2)
animals=[]
animals.append(an1)
animals.append(an2)
animals.append(an3)
2024-03-10 17:40:26 +01:00
old_animals=[]
old_animals.append(old_an1)
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_agent(agent_pos):
x, y = agent_pos
screen.blit(agent_image, (x*GRID_SIZE,y*GRID_SIZE))
def draw_animals():
for animal in animals:
animal.draw(screen,GRID_SIZE)
2024-03-10 17:40:26 +01:00
def draw_old_animals():
for animal in old_animals:
animal.draw(screen,GRID_SIZE)
def main():
global agent_pos
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type ==pygame.KEYDOWN:
if event.key == pygame.K_UP and agent_pos[1] > 0:
agent_pos[1] -= 1
elif event.key == pygame.K_DOWN and agent_pos[1] < GRID_HEIGHT - 1:
agent_pos[1] += 1
elif event.key == pygame.K_LEFT and agent_pos[0] > 0:
agent_pos[0] -= 1
elif event.key == pygame.K_RIGHT and agent_pos[0] < GRID_WIDTH - 1:
agent_pos[0] += 1
screen.blit(background_image,(0,0))
draw_grid()
draw_animals()
2024-03-10 17:40:26 +01:00
draw_old_animals()
draw_agent(agent_pos)
pygame.display.flip()
clock.tick(10)
if __name__ == "__main__":
main()