Gra-SI/agent.py
2023-04-21 15:46:27 +02:00

226 lines
8.7 KiB
Python

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)
# direction =['right','down','left','up'] 0 1 2 3 kierunek w ktory po kliknieciu do przodu pojdzie
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
#self.AGENT_IMG = pygame.image.load("./zdjecia/"+self.AGENT_IMAGES[self.direction]+".png")
self.AGENT_IMG = pygame.image.load("./zdjecia/gandalf-dol.png")
self.AGENT = pygame.transform.scale(self.AGENT_IMG,(64,64))
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
self.level = 1
self.current_health = 500
self.max_health = 1000
self.health_bar_length = 300
self.health_ratio = self.max_health/self.health_bar_length
def update(self):
self.health_bar()
self.movement()
self.collide_mob()
#self.end_game() gra sie konczy gdy wie gdzie sa wszyscy
self.disp_level()
self.rect.x += self.x_change
self.collide_blocks('x')
self.rect.y += self.y_change
self.collide_blocks('y')
self.x_change = 0
self.y_change = 0
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])
if keys[pygame.K_RIGHT]:
self.direction=(self.direction+1)%4
print("DIRECTION: "+self.AGENT_IMAGES[self.direction])
if keys[pygame.K_UP]:
if self.direction==0 and self.rect.x < 832 - 64:
self.x_change += TILE_SIZE/2
if self.direction==1 and self.rect.y < 768 - 64:
self.y_change += TILE_SIZE/2
if self.direction==2 and self.rect.x > 0:
self.x_change -= TILE_SIZE/2
if self.direction==3 and self.rect.y > 0:
self.y_change -= TILE_SIZE/2
"""
if keys[pygame.K_LEFT] and self.rect.x > 0:
self.x_change -= TILE_SIZE
if keys[pygame.K_RIGHT] and self.rect.x < 832 - 64:
self.x_change += TILE_SIZE
if keys[pygame.K_UP] and self.rect.y > 0:
self.y_change -= TILE_SIZE
if keys[pygame.K_DOWN] and self.rect.y < 768 - 64:
self.y_change += TILE_SIZE
"""
def end_game(self):
if (-1 in self.game.state)==False:
pygame.quit()
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_infantry_ork2 = pygame.sprite.spritecollide(self, self.game.infantry_orks2, False)
hits_sauron = pygame.sprite.spritecollide(self, self.game.sauronL, False)
#hits_unknown_mob = pygame.sprite.spritecollide(self, self.game.unknown_mobs, False) #unknown mob
#if hits_unknown_mob:
# self.game.unknown_mob.kill()
# self.game.archer_orks = pygame.sprite.LayeredUpdates()
# self.game.archer_ork = Archer_ork(self,3,2)
if hits_archer_ork:
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)
self.get_damage(self.game.archer_ork.damage)
self.level=self.level+1
#zmniejszenie życia o damage moba
#level up
if hits_infantry_ork:
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()
self.get_damage(self.game.infantry_ork.damage)
self.level=self.level+1
#zmniejszenie życia o damage moba
#level up
if hits_infantry_ork2:
if self.game.infantry_ork2.level > self.level or self.game.infantry_ork2.damage > self.current_health:
self.game.state[4]=self.game.infantry_ork2.x
self.game.state[5]=self.game.infantry_ork2.y
print(self.game.state)
self.kill()
self.game.new()
else:
self.game.state[4]=self.game.infantry_ork2.x
self.game.state[5]=self.game.infantry_ork2.y
print(self.game.state)
self.game.infantry_ork2.kill()
self.get_damage(self.game.infantry_ork2.damage)
self.level=self.level+1
#zmniejszenie życia o damage moba
#level up
if hits_sauron:
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()
#zmniejszenie życia o damage moba
#level up
def get_damage(self,amount):
if self.current_health > 0:
self.current_health -= amount
if self.current_health <= 0:
self.current_health = 0
#zmienic potem na smierc oraz później trzeba będzie tutaj ująć wszystkie statystyki
#i ze statystyk obliczyć ile dmg dostanie agent
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)
def disp_level(self):
font = pygame.font.SysFont(None, 40)
lvlDisplay = font.render(str(self.level)+" "+str(self.direction), 1, WHITE)
#lvlDisplay = font.render(str(self.level), 1, WHITE)
pygame.draw.rect(self.game.SCREEN, BLACK, (370, 780, 40, 40))
self.game.SCREEN.blit(lvlDisplay, (370,780))
# brakuje dodania miejsca w którym agent się leczy