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_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 = 500 self.max_health = 1000 self.health_bar_length = 300 self.health_ratio = self.max_health/self.health_bar_length self._layer = AGENT_LAYER def update(self): self.health_bar() self.movement() self.collide_mob() self.collide_flower() #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 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 end_game(self): if (-1 in self.game.state)==False: pygame.quit() 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_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 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() ''' 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 ''' 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))