import pygame
import sys
pygame.init()
# Screen dimensions
screen_width = 800
screen_height = 300
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Health Bar for AI")
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
black = (0, 0, 0)
light_gray = (200, 200, 200)
# Health bar dimensions and position
bar_width = 200
bar_height = 30
bar_x = 50
bar_y = 50
max_health = 100
current_health = max_health
# Damage amount
damage_amount = 10
# Initial multiplier
multiplier = 1
# Name next to the health bar
health_bar_name = "AI Agent"
name_font = pygame.font.Font(None, 30)
# Direction control
moving_left = False
moving_right = False
move_speed = 5
# Player position (for visual representation - optional for AI)
player_x = screen_width // 2
player_y = 150
player_size = 30
# Font for text display
font = pygame.font.Font(None, 24)
def draw_health_bar(surface, x, y, width, height, health, max_health, name):
ratio = health / max_health
inner_width = int(ratio * width)
# Draw the background of the health bar
pygame.draw.rect(surface, light_gray, (x, y, width, height))
# Draw the health bar
pygame.draw.rect(surface, green, (x, y, inner_width, height))
# Draw the border
pygame.draw.rect(surface, black, (x - 1, y - 1, width + 2, height + 2), 1)
# Display the name
name_text = name_font.render(name, True, black)
surface.blit(name_text, (x + width + 10, y))
def draw_player(surface, x, y, size, moving_left, moving_right):
color = (0, 0, 255) # Blue player (can be thought of as AI's "cursor")
pygame.draw.rect(surface, color, (x - size // 2, y - size // 2, size, size))
direction_text = ""
if moving_left:
direction_text = "Processing Left"
elif moving_right:
direction_text = "Processing Right"
direction_render = font.render(direction_text, True, black)
surface.blit(direction_render, (x - direction_render.get_width() // 2, y + size // 2 + 5))
def display_text(surface, text, x, y, color=black):
text_surface = font.render(text, True, color)
surface.blit(text_surface, (x, y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
current_health -= damage_amount * multiplier
elif event.key == pygame.K_f:
current_health -= damage_amount * multiplier
elif event.key == pygame.K_j:
current_health -= damage_amount * multiplier
elif event.key == pygame.K_k:
current_health -= damage_amount * multiplier
elif event.key == pygame.K_LEFT:
moving_left = True
moving_right = False
elif event.key == pygame.K_RIGHT:
moving_right = True
moving_left = False
elif event.key == pygame.K_m:
new_multiplier_str = input("Enter new multiplier: ")
try:
new_multiplier = float(new_multiplier_str)
if new_multiplier > 0:
multiplier = new_multiplier
else:
print("Multiplier must be greater than 0.")
except ValueError:
print("Invalid multiplier input.")
elif event.key == pygame.K_n:
new_name = input("Enter new name for the AI: ")
health_bar_name = new_name
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
moving_left = False
elif event.key == pygame.K_RIGHT:
moving_right = False
# Keep health within bounds
current_health = max(0, min(current_health, max_health))
# Update player position (optional visual for AI "state")
if moving_left:
player_x -= move_speed
if moving_right:
player_x += move_speed
player_x = max(player_size // 2, min(player_x, screen_width - player_size // 2))
# Drawing
screen.fill(white)
draw_health_bar(screen, bar_x, bar_y, bar_width, bar_height, current_health, max_health, health_bar_name)
draw_player(screen, player_x, player_y, player_size, moving_left, moving_right)
display_text(screen, f"Multiplier: {multiplier}", 50, 100)
display_text(screen, f"AI Health: {int(current_health)}/{max_health}", 50, 130)
pygame.display.flip()
pygame.quit()
sys.exit()