Male_zoo_Projekt_SI/main.py

79 lines
2.2 KiB
Python

import pygame
import sys
from animal import Animal
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)
animals=[]
animals.append(an1)
animals.append(an2)
animals.append(an3)
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)
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()
draw_agent(agent_pos)
pygame.display.flip()
clock.tick(10)
if __name__ == "__main__":
main()