dodanie klasy Animal i umieszczenie kilku na mapie

This commit is contained in:
LuminoX 2024-03-10 17:08:39 +01:00
parent 8f20ef1d7b
commit c46e523488
3 changed files with 26 additions and 0 deletions

8
animal.py Normal file
View File

@ -0,0 +1,8 @@
class Animal:
def __init__(self, x, y, image):
self.x = x-1
self.y = y-1
self.image = image
def draw(self, screen ,grid_size):
screen.blit(self.image, (self.x *grid_size, self.y*grid_size))

BIN
elephant.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 KiB

18
main.py
View File

@ -1,5 +1,6 @@
import pygame import pygame
import sys import sys
from animal import Animal
BLACK = (0, 0, 0) BLACK = (0, 0, 0)
@ -20,6 +21,18 @@ agent_image = pygame.transform.scale(agent_image, (GRID_SIZE,GRID_SIZE))
background_image = pygame.image.load('tło.jpg') background_image = pygame.image.load('tło.jpg')
background_image = pygame.transform.scale(background_image, WINDOW_SIZE) 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(): 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):
@ -30,6 +43,10 @@ def draw_agent(agent_pos):
x, y = agent_pos x, y = agent_pos
screen.blit(agent_image, (x*GRID_SIZE,y*GRID_SIZE)) screen.blit(agent_image, (x*GRID_SIZE,y*GRID_SIZE))
def draw_animals():
for animal in animals:
animal.draw(screen,GRID_SIZE)
def main(): def main():
global agent_pos global agent_pos
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -52,6 +69,7 @@ def main():
screen.blit(background_image,(0,0)) screen.blit(background_image,(0,0))
draw_grid() draw_grid()
draw_animals()
draw_agent(agent_pos) draw_agent(agent_pos)
pygame.display.flip() pygame.display.flip()
clock.tick(10) clock.tick(10)