Gra-SI/agent.py

208 lines
7.5 KiB
Python
Raw Normal View History

import pygame
from config import *
class Agent(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.game = game
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.AGENT_IMAGES =['gandalf-prawo','gandalf-dol','gandalf-lewo','gandalf-gora']
self.x = x * TILE_SIZE
self.y = y * TILE_SIZE
self.width = TILE_SIZE
self.height = TILE_SIZE
self.direction=0 #najpierw patrzy sie w prawo
self.x_change = 0
self.y_change = 0
2023-06-01 16:09:01 +02:00
self.AGENT_IMG_RIGHT = pygame.image.load("./pozostale_zdjecia/gandalf-prawo.png")
2023-04-29 13:01:47 +02:00
self.AGENT_RIGHT = pygame.transform.scale(self.AGENT_IMG_RIGHT,(64,64))
2023-06-01 16:09:01 +02:00
self.AGENT_IMG_DOWN = pygame.image.load("./pozostale_zdjecia/gandalf-dol.png")
2023-04-29 13:01:47 +02:00
self.AGENT_DOWN = pygame.transform.scale(self.AGENT_IMG_DOWN,(64,64))
2023-06-01 16:09:01 +02:00
self.AGENT_IMG_LEFT = pygame.image.load("./pozostale_zdjecia/gandalf-lewo.png")
2023-04-29 13:01:47 +02:00
self.AGENT_LEFT = pygame.transform.scale(self.AGENT_IMG_LEFT,(64,64))
2023-06-01 16:09:01 +02:00
self.AGENT_IMG_UP = pygame.image.load("./pozostale_zdjecia/gandalf-gora.png")
2023-04-29 13:01:47 +02:00
self.AGENT_UP = pygame.transform.scale(self.AGENT_IMG_UP,(64,64))
self.AGENT = self.AGENT_RIGHT
self.image = pygame.Surface([self.width, self.height])
self.image.blit(self.AGENT, (0,0))
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
2023-03-30 19:54:48 +02:00
self.level = 1
2023-04-18 21:38:22 +02:00
self.current_health = 200
self.max_health = 200
self.health_bar_length = 200
2023-04-18 21:38:22 +02:00
self.health_ratio = self.max_health/self.health_bar_length
2023-05-03 16:10:03 +02:00
self._layer = AGENT_LAYER
self.damage = 50*self.level
2023-06-16 12:46:47 +02:00
self.artifact = "tak"
def update(self):
2023-04-18 21:38:22 +02:00
self.health_bar()
self.movement()
self.collide_mob()
2023-04-29 12:00:36 +02:00
self.collide_flower()
2023-04-18 22:15:23 +02:00
self.disp_level()
self.rect.x += self.x_change
2023-03-30 20:10:29 +02:00
self.collide_blocks('x')
self.rect.y += self.y_change
2023-03-30 20:10:29 +02:00
self.collide_blocks('y')
self.x_change = 0
self.y_change = 0
2023-05-03 16:10:03 +02:00
def rotate(self):
if self.direction == 0:
self.image = self.AGENT_RIGHT
elif self.direction == 1:
self.image = self.AGENT_DOWN
elif self.direction == 2:
self.image = self.AGENT_LEFT
elif self.direction == 3:
self.image = self.AGENT_UP
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.direction=(self.direction-1)%4
print("DIRECTION: "+self.AGENT_IMAGES[self.direction])
2023-05-03 16:10:03 +02:00
self.rotate()
2023-04-29 13:01:47 +02:00
if keys[pygame.K_RIGHT]:
self.direction=(self.direction+1)%4
print("DIRECTION: "+self.AGENT_IMAGES[self.direction])
2023-05-03 16:10:03 +02:00
self.rotate()
2023-04-29 13:01:47 +02:00
if keys[pygame.K_UP]:
if self.direction==0 and self.rect.x < 832 - 64:
2023-05-02 20:36:45 +02:00
self.x_change += TILE_SIZE
if self.direction==1 and self.rect.y < 768 - 64:
2023-05-02 20:36:45 +02:00
self.y_change += TILE_SIZE
if self.direction==2 and self.rect.x > 0:
2023-05-02 20:36:45 +02:00
self.x_change -= TILE_SIZE
if self.direction==3 and self.rect.y > 0:
2023-05-02 20:36:45 +02:00
self.y_change -= TILE_SIZE
2023-05-03 16:10:03 +02:00
2023-03-30 20:10:29 +02:00
2023-04-29 12:00:36 +02:00
def collide_flower(self):
hits_flower = pygame.sprite.spritecollide(self, self.game.flowers, False)
if hits_flower:
self.current_health = self.max_health
2023-03-30 20:10:29 +02:00
def collide_blocks(self, direction):
if direction == "x":
hits = pygame.sprite.spritecollide(self,self.game.rock_sprites, False)
if hits:
if self.x_change > 0:
self.rect.x = hits[0].rect.left - self.rect.width
if self.x_change < 0:
self.rect.x = hits[0].rect.right
if direction == "y":
hits = pygame.sprite.spritecollide(self,self.game.rock_sprites, False)
if hits:
if self.y_change > 0:
self.rect.y = hits[0].rect.top - self.rect.height
if self.y_change < 0:
self.rect.y = hits[0].rect.bottom
def collide_mob(self):
hits_archer_ork = pygame.sprite.spritecollide(self, self.game.archer_orks, False)
hits_infantry_ork = pygame.sprite.spritecollide(self, self.game.infantry_orks, False)
hits_sauron = pygame.sprite.spritecollide(self, self.game.sauronL, False)
2023-03-31 15:26:54 +02:00
if hits_archer_ork:
2023-04-18 21:38:22 +02:00
if self.game.archer_ork.level > self.level or self.game.archer_ork.damage > self.current_health:
self.kill()
self.game.new()
else:
self.game.archer_ork.kill()
self.game.state[0]=self.game.archer_ork.x
self.game.state[1]=self.game.archer_ork.y
print(self.game.state)
2023-04-18 21:38:22 +02:00
self.get_damage(self.game.archer_ork.damage)
self.level=self.level+1
2023-04-29 13:01:47 +02:00
if hits_infantry_ork:
2023-04-18 21:38:22 +02:00
if self.game.infantry_ork.level > self.level or self.game.infantry_ork.damage > self.current_health:
self.game.state[2]=self.game.infantry_ork.x
self.game.state[3]=self.game.infantry_ork.y
print(self.game.state)
self.kill()
self.game.new()
else:
self.game.state[2]=self.game.infantry_ork.x
self.game.state[3]=self.game.infantry_ork.y
print(self.game.state)
self.game.infantry_ork.kill()
2023-04-18 21:38:22 +02:00
self.get_damage(self.game.infantry_ork.damage)
self.level=self.level+1
if hits_sauron:
2023-04-18 21:38:22 +02:00
if self.game.sauron.level > self.level or self.game.sauron.damage > self.current_health:
self.game.state[6]=self.game.sauron.x
self.game.state[7]=self.game.sauron.y
print(self.game.state)
self.kill()
self.game.new()
else:
self.game.state[6]=self.game.sauron.x
self.game.state[7]=self.game.sauron.y
print(self.game.state)
self.game.sauron.kill()
self.level=self.level+1
pygame.quit()
2023-04-18 21:38:22 +02:00
def get_damage(self,amount):
if self.current_health > 0:
self.current_health -= amount
if self.current_health <= 0:
self.current_health = 0
2023-04-18 21:38:22 +02:00
def get_health(self, amount):
if self.current_health < self.max_health:
self.current_health += amount
if self.current_health >= self.max_health:
self.current_health = self.max_health
def health_bar(self):
pygame.draw.rect(self.game.SCREEN, (255,0,0), (10,780,self.health_bar_length,25))
pygame.draw.rect(self.game.SCREEN, (0,255,0), (10,780,self.current_health/self.health_ratio,25))
pygame.draw.rect(self.game.SCREEN, (255,255,255), (10,780,self.health_bar_length, 25),2)
2023-04-18 22:15:23 +02:00
def disp_level(self):
font = pygame.font.SysFont(None, 40)
lvlDisplay = font.render(str(self.level)+" "+str(self.direction), 1, WHITE)
2023-04-18 22:15:23 +02:00
pygame.draw.rect(self.game.SCREEN, BLACK, (370, 780, 40, 40))
self.game.SCREEN.blit(lvlDisplay, (370,780))
2023-04-18 21:38:22 +02:00