Add HUD section.

This commit is contained in:
Emil Shanaty
2025-04-07 22:56:35 +03:00
parent 3cdf563a52
commit 46be023d9c
3 changed files with 16 additions and 8 deletions
+11 -5
View File
@@ -2,7 +2,7 @@ import pygame
import random import random
WIDTH, HEIGHT = 800, 600 WIDTH, HEIGHT = 800, 600
HUD_HEIGHT = 150
class GameObject: class GameObject:
x = 0 x = 0
y = 0 y = 0
@@ -122,8 +122,10 @@ class UI:
horizontal_offset = 25 # Horizontal spacing between marks horizontal_offset = 25 # Horizontal spacing between marks
vertical_offset = -25 # Vertical spacing between marks vertical_offset = -25 # Vertical spacing between marks
mark_size = 25 # Size of the marks mark_size = 25 # Size of the marks
def __init__(self): def __init__(self, hud_image_path, x=0, y=HEIGHT):
self.horses = [] # Initialize children list self.horses = [] # Initialize children list
self.hud_image = pygame.image.load(hud_image_path)
self.hud_rect = self.hud_image.get_rect(topleft=(x,y))
def add_horses(self, children): def add_horses(self, children):
new_horses = [child for child in children if child not in self.horses] # Add new children new_horses = [child for child in children if child not in self.horses] # Add new children
self.horses.extend(new_horses) self.horses.extend(new_horses)
@@ -138,7 +140,7 @@ class UI:
unactive_marks.append(self.horses[i]) # Add inactive horses unactive_marks.append(self.horses[i]) # Add inactive horses
width = 10 # Number of marks per row width = 10 # Number of marks per row
active_marks_section_pos = (0 - self.mark_size, 600 - self.mark_size) # Starting position for active marks active_marks_section_pos = (0 - self.mark_size, HEIGHT + HUD_HEIGHT - self.mark_size) # Starting position for active marks
xa = active_marks_section_pos[0] xa = active_marks_section_pos[0]
ya = active_marks_section_pos[1] ya = active_marks_section_pos[1]
for i in range(len(active_marks)): for i in range(len(active_marks)):
@@ -148,15 +150,19 @@ class UI:
ya += self.vertical_offset ya += self.vertical_offset
xa = active_marks_section_pos[0] xa = active_marks_section_pos[0]
unactive_marks_section_pos = (800, 600 - self.mark_size) # Starting position for inactive marks unactive_marks_section_pos = (WIDTH, HEIGHT + HUD_HEIGHT - self.mark_size) # Starting position for inactive marks
xu = unactive_marks_section_pos[0] xu = unactive_marks_section_pos[0]
yu = unactive_marks_section_pos[1] yu = unactive_marks_section_pos[1]
for i in range(len(unactive_marks)): for i in range(len(unactive_marks)):
xu -= self.horizontal_offset xu -= self.horizontal_offset
pygame.draw.rect(screen, unactive_marks[i].color, (xu, yu, self.mark_size, self.mark_size)) # Draw inactive marks pygame.draw.rect(screen, unactive_marks[i].color, (xu, yu, self.mark_size, self.mark_size)) # Draw inactive marks
if 800 - xu >= width * self.mark_size: # Move to the next row if the row is full if WIDTH - xu >= width * self.mark_size: # Move to the next row if the row is full
yu += self.vertical_offset yu += self.vertical_offset
xu = unactive_marks_section_pos[0] xu = unactive_marks_section_pos[0]
def draw(self, surface):
surface.blit(self.hud_image, self.hud_rect)
class Spawner: class Spawner:
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

+5 -3
View File
@@ -10,12 +10,12 @@ FRAME_RATE = 160 #TODO: FIX THE INCORRECT FRAME RATE CORRELATION
pygame.init() pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # Initialize the screen screen = pygame.display.set_mode((WIDTH, HEIGHT + HUD_HEIGHT)) # Initialize the screen
pygame.display.set_caption("NIC_Project") # Set window title pygame.display.set_caption("NIC_Project") # Set window title
WHITE = (255, 255, 255) # Define white color WHITE = (255, 255, 255) # Define white color
UI = UI() # Initialize UI UI = UI("images/HUD.png") # Initialize UI
gameobjects = [] gameobjects = []
horses = [] horses = []
barriers = [] barriers = []
@@ -118,8 +118,10 @@ while True:
for object in gameobjects: for object in gameobjects:
object.draw(screen) # Draw all game objects object.draw(screen) # Draw all game objects
UI.draw_marks(screen) # Draw UI marks
UI.draw(screen)
UI.draw_marks(screen) # Draw UI marks
if all(horse.stopped for horse in horses): if all(horse.stopped for horse in horses):
UI.iteration_num += 1 UI.iteration_num += 1
init_game() init_game()