import pygame
import random

pygame.init()

clock = pygame.time.Clock()
fps = 60

#game window
bottom_panel = 150
screen_width = 800
screen_height = 400 + bottom_panel

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Battle')


#define game variables
current_fighter = 1
total_fighters = 3
action_cooldown = 0
action_wait_time = 90
attack = False
potion = False
clicked = False


#define fonts
font = pygame.font.SysFont('Times New Roman', 26)

#define colours
red = (255, 0, 0)
green = (0, 255, 0)

#load images
#background image
background_img = pygame.image.load('img/Background/background.png').convert_alpha()
#panel image
panel_img = pygame.image.load('img/Icons/panel.png').convert_alpha()
#sword image
sword_img = pygame.image.load('img/Icons/sword.png').convert_alpha()


#create function for drawing text
def draw_text(text, font, text_col, x, y):
	img = font.render(text, True, text_col)
	screen.blit(img, (x, y))


#function for drawing background
def draw_bg():
	screen.blit(background_img, (0, 0))


#function for drawing panel
def draw_panel():
	#draw panel rectangle
	screen.blit(panel_img, (0, screen_height - bottom_panel))
	#show knight stats
	draw_text(f'{knight.name} HP: {knight.hp}', font, red, 100, screen_height - bottom_panel + 10)
	for count, i in enumerate(bandit_list):
		#show name and health
		draw_text(f'{i.name} HP: {i.hp}', font, red, 550, (screen_height - bottom_panel + 10) + count * 60)




#fighter class
class Fighter():
	def __init__(self, x, y, name, max_hp, strength, potions):
		self.name = name
		self.max_hp = max_hp
		self.hp = max_hp
		self.strength = strength
		self.start_potions = potions
		self.potions = potions
		self.alive = True
		self.animation_list = []
		self.frame_index = 0
		self.action = 0#0:idle, 1:attack, 2:hurt, 3:dead
		self.update_time = pygame.time.get_ticks()
		#load idle images
		temp_list = []
		for i in range(8):
			img = pygame.image.load(f'img/{self.name}/Idle/{i}.png')
			img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
			temp_list.append(img)
		self.animation_list.append(temp_list)
		#load attack images
		temp_list = []
		for i in range(8):
			img = pygame.image.load(f'img/{self.name}/Attack/{i}.png')
			img = pygame.transform.scale(img, (img.get_width() * 3, img.get_height() * 3))
			temp_list.append(img)
		self.animation_list.append(temp_list)
		self.image = self.animation_list[self.action][self.frame_index]
		self.rect = self.image.get_rect()
		self.rect.center = (x, y)


	def update(self):
		animation_cooldown = 100
		#handle animation
		#update image
		self.image = self.animation_list[self.action][self.frame_index]
		#check if enough time has passed since the last update
		if pygame.time.get_ticks() - self.update_time > animation_cooldown:
			self.update_time = pygame.time.get_ticks()
			self.frame_index += 1
		#if the animation has run out then reset back to the start
		if self.frame_index >= len(self.animation_list[self.action]):
			self.idle()


	
	def idle(self):
		#set variables to attack animation
		self.action = 0
		self.frame_index = 0
		self.update_time = pygame.time.get_ticks()


	def attack(self, target):
		#deal damage to enemy
		rand = random.randint(-5, 5)
		damage = self.strength + rand
		target.hp -= damage
		#check if target has died
		if target.hp < 1:
			target.hp = 0
			target.alive = False
		#set variables to attack animation
		self.action = 1
		self.frame_index = 0
		self.update_time = pygame.time.get_ticks()


	def draw(self):
		screen.blit(self.image, self.rect)



class HealthBar():
	def __init__(self, x, y, hp, max_hp):
		self.x = x
		self.y = y
		self.hp = hp
		self.max_hp = max_hp


	def draw(self, hp):
		#update with new health
		self.hp = hp
		#calculate health ratio
		ratio = self.hp / self.max_hp
		pygame.draw.rect(screen, red, (self.x, self.y, 150, 20))
		pygame.draw.rect(screen, green, (self.x, self.y, 150 * ratio, 20))




knight = Fighter(200, 260, 'Knight', 30, 10, 3)
bandit1 = Fighter(550, 270, 'Bandit', 20, 6, 1)
bandit2 = Fighter(700, 270, 'Bandit', 20, 6, 1)

bandit_list = []
bandit_list.append(bandit1)
bandit_list.append(bandit2)

knight_health_bar = HealthBar(100, screen_height - bottom_panel + 40, knight.hp, knight.max_hp)
bandit1_health_bar = HealthBar(550, screen_height - bottom_panel + 40, bandit1.hp, bandit1.max_hp)
bandit2_health_bar = HealthBar(550, screen_height - bottom_panel + 100, bandit2.hp, bandit2.max_hp)


run = True
while run:

	clock.tick(fps)

	#draw background
	draw_bg()

	#draw panel
	draw_panel()
	knight_health_bar.draw(knight.hp)
	bandit1_health_bar.draw(bandit1.hp)
	bandit2_health_bar.draw(bandit2.hp)

	#draw fighters
	knight.update()
	knight.draw()
	for bandit in bandit_list:
		bandit.update()
		bandit.draw()


	#control player actions
	#reset action variables
	attack = False
	potion = False
	target = None
	#make sure mouse is visible
	pygame.mouse.set_visible(True)
	pos = pygame.mouse.get_pos()
	for count, bandit in enumerate(bandit_list):
		if bandit.rect.collidepoint(pos):
			#hide mouse
			pygame.mouse.set_visible(False)
			#show sword in place of mouse cursor
			screen.blit(sword_img, pos)
			if clicked == True:
				attack = True
				target = bandit_list[count]


	#player action
	if knight.alive == True:
		if current_fighter == 1:
			action_cooldown += 1
			if action_cooldown >= action_wait_time:
				#look for player action
				#attack
				if attack == True and target != None:
					knight.attack(target)
					current_fighter += 1
					action_cooldown = 0


	#enemy action
	for count, bandit in enumerate(bandit_list):
		if current_fighter == 2 + count:
			if bandit.alive == True:
				action_cooldown += 1
				if action_cooldown >= action_wait_time:
					#attack
					bandit.attack(knight)
					current_fighter += 1
					action_cooldown = 0
			else:
				current_fighter += 1

	#if all fighters have had a turn then reset
	if current_fighter > total_fighters:
		current_fighter = 1


	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			run = False
		if event.type == pygame.MOUSEBUTTONDOWN:
			clicked = True
		else:
			clicked = False

	pygame.display.update()

pygame.quit()


[((4, 0, 4, 13), 'pygame.init', 'pygame.init', ({}, {}), '()', False, 'import pygame\n'), ((6, 8, 6, 27), 'pygame.time.Clock', 'pygame.time.Clock', ({}, {}), '()', False, 'import pygame\n'), ((14, 9, 14, 63), 'pygame.display.set_mode', 'pygame.display.set_mode', ({(14, 33, 14, 62): '(screen_width, screen_height)'}, {}), '((screen_width, screen_height))', False, 'import pygame\n'), ((15, 0, 15, 36), 'pygame.display.set_caption', 'pygame.display.set_caption', ({(15, 27, 15, 35): '"""Battle"""'}, {}), "('Battle')", False, 'import pygame\n'), ((29, 7, 29, 49), 'pygame.font.SysFont', 'pygame.font.SysFont', ({(29, 27, 29, 44): '"""Times New Roman"""', (29, 46, 29, 48): '26'}, {}), "('Times New Roman', 26)", False, 'import pygame\n'), ((257, 0, 257, 13), 'pygame.quit', 'pygame.quit', ({}, {}), '()', False, 'import pygame\n'), ((203, 1, 203, 31), 'pygame.mouse.set_visible', 'pygame.mouse.set_visible', ({(203, 26, 203, 30): '(True)'}, {}), '(True)', False, 'import pygame\n'), ((204, 7, 204, 29), 'pygame.mouse.get_pos', 'pygame.mouse.get_pos', ({}, {}), '()', False, 'import pygame\n'), ((247, 14, 247, 32), 'pygame.event.get', 'pygame.event.get', ({}, {}), '()', False, 'import pygame\n'), ((255, 1, 255, 24), 'pygame.display.update', 'pygame.display.update', ({}, {}), '()', False, 'import pygame\n'), ((37, 17, 37, 67), 'pygame.image.load', 'pygame.image.load', ({(37, 35, 37, 66): '"""img/Background/background.png"""'}, {}), "('img/Background/background.png')", False, 'import pygame\n'), ((39, 12, 39, 52), 'pygame.image.load', 'pygame.image.load', ({(39, 30, 39, 51): '"""img/Icons/panel.png"""'}, {}), "('img/Icons/panel.png')", False, 'import pygame\n'), ((41, 12, 41, 52), 'pygame.image.load', 'pygame.image.load', ({(41, 30, 41, 51): '"""img/Icons/sword.png"""'}, {}), "('img/Icons/sword.png')", False, 'import pygame\n'), ((81, 21, 81, 44), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((120, 21, 120, 44), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((125, 9, 125, 30), 'random.randint', 'random.randint', ({(125, 24, 125, 26): '-5', (125, 28, 125, 29): '5'}, {}), '(-5, 5)', False, 'import random\n'), ((135, 21, 135, 44), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((156, 2, 156, 58), 'pygame.draw.rect', 'pygame.draw.rect', ({(156, 19, 156, 25): 'screen', (156, 27, 156, 30): 'red', (156, 32, 156, 57): '(self.x, self.y, 150, 20)'}, {}), '(screen, red, (self.x, self.y, 150, 20))', False, 'import pygame\n'), ((157, 2, 157, 68), 'pygame.draw.rect', 'pygame.draw.rect', ({(157, 19, 157, 25): 'screen', (157, 27, 157, 32): 'green', (157, 34, 157, 67): '(self.x, self.y, 150 * ratio, 20)'}, {}), '(screen, green, (self.x, self.y, 150 * ratio, 20))', False, 'import pygame\n'), ((85, 9, 85, 59), 'pygame.image.load', 'pygame.image.load', ({(85, 27, 85, 58): 'f"""img/{self.name}/Idle/{i}.png"""'}, {}), "(f'img/{self.name}/Idle/{i}.png')", False, 'import pygame\n'), ((92, 9, 92, 61), 'pygame.image.load', 'pygame.image.load', ({(92, 27, 92, 60): 'f"""img/{self.name}/Attack/{i}.png"""'}, {}), "(f'img/{self.name}/Attack/{i}.png')", False, 'import pygame\n'), ((108, 22, 108, 45), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((208, 3, 208, 34), 'pygame.mouse.set_visible', 'pygame.mouse.set_visible', ({(208, 28, 208, 33): '(False)'}, {}), '(False)', False, 'import pygame\n'), ((107, 5, 107, 28), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n')]