208 lines
7.5 KiB
Python
208 lines
7.5 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)
|
|
|
|
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_RIGHT = pygame.image.load("./pozostale_zdjecia/gandalf-prawo.png")
|
|
self.AGENT_RIGHT = pygame.transform.scale(self.AGENT_IMG_RIGHT,(64,64))
|
|
self.AGENT_IMG_DOWN = pygame.image.load("./pozostale_zdjecia/gandalf-dol.png")
|
|
self.AGENT_DOWN = pygame.transform.scale(self.AGENT_IMG_DOWN,(64,64))
|
|
self.AGENT_IMG_LEFT = pygame.image.load("./pozostale_zdjecia/gandalf-lewo.png")
|
|
self.AGENT_LEFT = pygame.transform.scale(self.AGENT_IMG_LEFT,(64,64))
|
|
self.AGENT_IMG_UP = pygame.image.load("./pozostale_zdjecia/gandalf-gora.png")
|
|
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
|
|
|
|
self.level = 1
|
|
|
|
self.current_health = 200
|
|
self.max_health = 200
|
|
self.health_bar_length = 200
|
|
self.health_ratio = self.max_health/self.health_bar_length
|
|
self._layer = AGENT_LAYER
|
|
self.damage = 50*self.level
|
|
|
|
self.artifact = "tak"
|
|
|
|
def update(self):
|
|
|
|
self.health_bar()
|
|
self.movement()
|
|
self.collide_mob()
|
|
self.collide_flower()
|
|
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 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])
|
|
self.rotate()
|
|
|
|
|
|
if keys[pygame.K_RIGHT]:
|
|
self.direction=(self.direction+1)%4
|
|
print("DIRECTION: "+self.AGENT_IMAGES[self.direction])
|
|
self.rotate()
|
|
|
|
|
|
if keys[pygame.K_UP]:
|
|
if self.direction==0 and self.rect.x < 832 - 64:
|
|
self.x_change += TILE_SIZE
|
|
if self.direction==1 and self.rect.y < 768 - 64:
|
|
self.y_change += TILE_SIZE
|
|
if self.direction==2 and self.rect.x > 0:
|
|
self.x_change -= TILE_SIZE
|
|
if self.direction==3 and self.rect.y > 0:
|
|
self.y_change -= TILE_SIZE
|
|
|
|
|
|
def collide_flower(self):
|
|
hits_flower = pygame.sprite.spritecollide(self, self.game.flowers, False)
|
|
if hits_flower:
|
|
self.current_health = self.max_health
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
def get_damage(self,amount):
|
|
if self.current_health > 0:
|
|
self.current_health -= amount
|
|
if self.current_health <= 0:
|
|
self.current_health = 0
|
|
|
|
|
|
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)
|
|
pygame.draw.rect(self.game.SCREEN, BLACK, (370, 780, 40, 40))
|
|
self.game.SCREEN.blit(lvlDisplay, (370,780))
|
|
|
|
|