Add comments in the code.
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 34 KiB |
+68
-38
@@ -6,6 +6,10 @@ HUD_HEIGHT = 150
|
|||||||
BARRIER_SEED = 20
|
BARRIER_SEED = 20
|
||||||
|
|
||||||
class GameObject:
|
class GameObject:
|
||||||
|
"""
|
||||||
|
Base class for all game objects.
|
||||||
|
Handles image loading, positioning, drawing, and child objects.
|
||||||
|
"""
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
|
|
||||||
@@ -17,62 +21,70 @@ class GameObject:
|
|||||||
self.offset = offset
|
self.offset = offset
|
||||||
|
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
|
# Draw self and all children recursively
|
||||||
surface.blit(self.image, self.rect)
|
surface.blit(self.image, self.rect)
|
||||||
if len(self.children) == 0:
|
if not self.children:
|
||||||
return
|
return
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
child.draw(surface)
|
child.draw(surface)
|
||||||
|
|
||||||
def set_position(self, x, y):
|
def set_position(self, x, y):
|
||||||
|
# Set position and update children positions with offsets
|
||||||
self.rect.x = x
|
self.rect.x = x
|
||||||
self.rect.y = y
|
self.rect.y = y
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
if len(self.children) == 0:
|
if not self.children:
|
||||||
return
|
return
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
child.set_position(x + child.offset[0], y + child.offset[1])
|
child.set_position(x + child.offset[0], y + child.offset[1])
|
||||||
|
|
||||||
def move(self, dx, dy):
|
def move(self, dx, dy):
|
||||||
|
# Move object by delta and update children accordingly
|
||||||
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
||||||
if len(self.children) == 0:
|
if not self.children:
|
||||||
return
|
return
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
||||||
|
|
||||||
def set_size(self, width, height):
|
def set_size(self, width, height):
|
||||||
|
# Resize image and update rect
|
||||||
self.image = pygame.transform.scale(self.image, (width, height))
|
self.image = pygame.transform.scale(self.image, (width, height))
|
||||||
self.rect = self.image.get_rect(topleft=(self.x, self.y))
|
self.rect = self.image.get_rect(topleft=(self.x, self.y))
|
||||||
|
|
||||||
def scale_by(self, scale):
|
def scale_by(self, scale):
|
||||||
self.image = pygame.transform.scale(self.image, (self.image.get_width() * scale, self.image.get_height() * scale))
|
# Scale image by a factor
|
||||||
|
new_size = (self.image.get_width() * scale, self.image.get_height() * scale)
|
||||||
|
self.image = pygame.transform.scale(self.image, new_size)
|
||||||
self.rect = self.image.get_rect(topleft=(self.x, self.y))
|
self.rect = self.image.get_rect(topleft=(self.x, self.y))
|
||||||
|
|
||||||
def get_position(self):
|
def get_position(self):
|
||||||
return (self.x, self.y)
|
return (self.x, self.y)
|
||||||
|
|
||||||
def set_children(self, child):
|
def set_children(self, child):
|
||||||
|
# Add a child GameObject
|
||||||
self.children.append(child)
|
self.children.append(child)
|
||||||
|
|
||||||
def recolor(self, color):
|
def recolor(self, color):
|
||||||
|
# Apply color tint to image
|
||||||
self.image.fill(color, special_flags=pygame.BLEND_MULT)
|
self.image.fill(color, special_flags=pygame.BLEND_MULT)
|
||||||
self.image.set_colorkey(None)
|
self.image.set_colorkey(None)
|
||||||
|
|
||||||
class Horse(GameObject):
|
class Horse(GameObject):
|
||||||
|
"""
|
||||||
|
Represents a horse character with vertical movement, animation, and fitness tracking.
|
||||||
|
Supports acceleration, speed, and stopping.
|
||||||
|
"""
|
||||||
default_vacceleration = 0.2
|
default_vacceleration = 0.2
|
||||||
vspeed = 0
|
|
||||||
vacceleration = 0
|
|
||||||
stopped = False
|
|
||||||
color = ()
|
|
||||||
ribbon_fill = None
|
|
||||||
ribbon_out = None
|
|
||||||
|
|
||||||
def __init__(self, image_path, x, y):
|
def __init__(self, image_path, x, y):
|
||||||
super().__init__(image_path, x, y)
|
super().__init__(image_path, x, y)
|
||||||
self.images = []
|
# Load animation frames
|
||||||
self.images.append(pygame.image.load("images/Horse_1.png"))
|
self.images = [
|
||||||
self.images.append(pygame.image.load("images/Horse_2.png"))
|
pygame.image.load("images/Horse_1.png"),
|
||||||
self.images.append(pygame.image.load("images/Horse_3.png"))
|
pygame.image.load("images/Horse_2.png"),
|
||||||
|
pygame.image.load("images/Horse_3.png")
|
||||||
|
]
|
||||||
self.animation_speed = 25
|
self.animation_speed = 25
|
||||||
self.frame_counter = 0
|
self.frame_counter = 0
|
||||||
self.current_frame = 0
|
self.current_frame = 0
|
||||||
@@ -80,7 +92,8 @@ class Horse(GameObject):
|
|||||||
self.vacceleration = 0
|
self.vacceleration = 0
|
||||||
self.stopped = False
|
self.stopped = False
|
||||||
self.fitness = 0
|
self.fitness = 0
|
||||||
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
# Assign random color for ribbons
|
||||||
|
self.color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
|
||||||
self.ribbon_fill = GameObject("images/Ribbon_fill.png", self.x, self.y, (16, -1))
|
self.ribbon_fill = GameObject("images/Ribbon_fill.png", self.x, self.y, (16, -1))
|
||||||
self.ribbon_fill.recolor(self.color)
|
self.ribbon_fill.recolor(self.color)
|
||||||
self.ribbon_out = GameObject("images/Ribbon_out.png", self.x, self.y, (14, -2))
|
self.ribbon_out = GameObject("images/Ribbon_out.png", self.x, self.y, (14, -2))
|
||||||
@@ -88,10 +101,11 @@ class Horse(GameObject):
|
|||||||
self.set_children(self.ribbon_out)
|
self.set_children(self.ribbon_out)
|
||||||
|
|
||||||
def apply_vspeed(self):
|
def apply_vspeed(self):
|
||||||
pos = self.get_position()
|
# Move vertically by current speed
|
||||||
self.set_position(pos[0], pos[1] + self.vspeed)
|
self.set_position(self.x, self.y + self.vspeed)
|
||||||
|
|
||||||
def apply_vacceleration(self):
|
def apply_vacceleration(self):
|
||||||
|
# Update speed by acceleration
|
||||||
self.vspeed += self.vacceleration
|
self.vspeed += self.vacceleration
|
||||||
|
|
||||||
def set_vspeed(self, speed):
|
def set_vspeed(self, speed):
|
||||||
@@ -104,23 +118,25 @@ class Horse(GameObject):
|
|||||||
self.vacceleration = vacceleration
|
self.vacceleration = vacceleration
|
||||||
|
|
||||||
def up(self):
|
def up(self):
|
||||||
|
# Accelerate upward
|
||||||
self.set_vacceleration(-self.default_vacceleration)
|
self.set_vacceleration(-self.default_vacceleration)
|
||||||
|
|
||||||
def down(self):
|
def down(self):
|
||||||
|
# Accelerate downward
|
||||||
self.set_vacceleration(self.default_vacceleration)
|
self.set_vacceleration(self.default_vacceleration)
|
||||||
|
|
||||||
def stay(self):
|
def stay(self):
|
||||||
|
# No vertical acceleration
|
||||||
self.set_vacceleration(0)
|
self.set_vacceleration(0)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
# Stop all movement
|
||||||
self.stopped = True
|
self.stopped = True
|
||||||
self.set_vacceleration(0)
|
self.set_vacceleration(0)
|
||||||
self.set_vspeed(0)
|
self.set_vspeed(0)
|
||||||
|
|
||||||
def draw(self, surface):
|
|
||||||
super().draw(surface)
|
|
||||||
|
|
||||||
def update_animation(self):
|
def update_animation(self):
|
||||||
|
# Animate horse by cycling frames
|
||||||
self.frame_counter += 1
|
self.frame_counter += 1
|
||||||
if self.frame_counter >= self.animation_speed:
|
if self.frame_counter >= self.animation_speed:
|
||||||
self.frame_counter = 0
|
self.frame_counter = 0
|
||||||
@@ -128,21 +144,31 @@ class Horse(GameObject):
|
|||||||
self.image = self.images[self.current_frame]
|
self.image = self.images[self.current_frame]
|
||||||
|
|
||||||
def count_fitness(self):
|
def count_fitness(self):
|
||||||
if self.stopped == False:
|
# Increase fitness if moving; penalize if speed zero
|
||||||
|
if not self.stopped:
|
||||||
self.fitness += 1
|
self.fitness += 1
|
||||||
if self.vspeed == 0:
|
if self.vspeed == 0:
|
||||||
self.fitness -= 0.9
|
self.fitness -= 0.9
|
||||||
|
|
||||||
class Background(GameObject):
|
class Background(GameObject):
|
||||||
|
"""
|
||||||
|
Background image for the game scene.
|
||||||
|
"""
|
||||||
def __init__(self, image_path, x, y):
|
def __init__(self, image_path, x, y):
|
||||||
super().__init__(image_path, x, y)
|
super().__init__(image_path, x, y)
|
||||||
|
|
||||||
class Barrier(GameObject):
|
class Barrier(GameObject):
|
||||||
|
"""
|
||||||
|
Obstacles that move horizontally and interact with horses.
|
||||||
|
"""
|
||||||
def __init__(self, image_path, x, y):
|
def __init__(self, image_path, x, y):
|
||||||
super().__init__(image_path, x, y)
|
super().__init__(image_path, x, y)
|
||||||
self.scale_by(5)
|
self.scale_by(5) # Make barrier larger for visibility
|
||||||
|
|
||||||
class UI:
|
class UI:
|
||||||
|
"""
|
||||||
|
Handles HUD display and horse status markers.
|
||||||
|
"""
|
||||||
iteration_num = 0
|
iteration_num = 0
|
||||||
horses = []
|
horses = []
|
||||||
horizontal_offset = 25
|
horizontal_offset = 25
|
||||||
@@ -155,34 +181,38 @@ class UI:
|
|||||||
self.hud_rect = self.hud_image.get_rect(topleft=(x, y))
|
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 horses to UI tracking list
|
||||||
self.horses.extend(new_horses)
|
self.horses.extend([child for child in children if child not in self.horses])
|
||||||
|
|
||||||
def draw_marks(self, screen):
|
def draw_marks(self, screen):
|
||||||
active_marks = [horse for horse in self.horses if not horse.stopped]
|
# Draw colored squares representing active and stopped horses
|
||||||
unactive_marks = [horse for horse in self.horses if horse.stopped]
|
active = [h for h in self.horses if not h.stopped]
|
||||||
width = 10
|
inactive = [h for h in self.horses if h.stopped]
|
||||||
xa = 0
|
|
||||||
ya = HEIGHT + HUD_HEIGHT - self.mark_size
|
xa, ya = 0, HEIGHT + HUD_HEIGHT - self.mark_size
|
||||||
for i, horse in enumerate(active_marks):
|
for i, horse in enumerate(active):
|
||||||
pygame.draw.rect(screen, horse.color, (xa, ya, self.mark_size, self.mark_size))
|
pygame.draw.rect(screen, horse.color, (xa, ya, self.mark_size, self.mark_size))
|
||||||
xa += self.horizontal_offset
|
xa += self.horizontal_offset
|
||||||
if (i + 1) % width == 0:
|
if (i+1) % 10 == 0:
|
||||||
ya += self.vertical_offset
|
ya += self.vertical_offset
|
||||||
xa = 0
|
xa = 0
|
||||||
xu = WIDTH - self.mark_size
|
|
||||||
yu = HEIGHT + HUD_HEIGHT - self.mark_size
|
xu, yu = WIDTH - self.mark_size, HEIGHT + HUD_HEIGHT - self.mark_size
|
||||||
for i, horse in enumerate(unactive_marks):
|
for i, horse in enumerate(inactive):
|
||||||
pygame.draw.rect(screen, horse.color, (xu, yu, self.mark_size, self.mark_size))
|
pygame.draw.rect(screen, horse.color, (xu, yu, self.mark_size, self.mark_size))
|
||||||
xu -= self.horizontal_offset
|
xu -= self.horizontal_offset
|
||||||
if (i + 1) % width == 0:
|
if (i+1) % 10 == 0:
|
||||||
yu += self.vertical_offset
|
yu += self.vertical_offset
|
||||||
xu = WIDTH - self.mark_size
|
xu = WIDTH - self.mark_size
|
||||||
|
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
|
# Draw HUD image
|
||||||
surface.blit(self.hud_image, self.hud_rect)
|
surface.blit(self.hud_image, self.hud_rect)
|
||||||
|
|
||||||
class Spawner:
|
class Spawner:
|
||||||
|
"""
|
||||||
|
Spawns barriers at intervals to challenge horses.
|
||||||
|
"""
|
||||||
active = True
|
active = True
|
||||||
delay = 500
|
delay = 500
|
||||||
tick_counter = 0
|
tick_counter = 0
|
||||||
@@ -192,6 +222,7 @@ class Spawner:
|
|||||||
self.delay = delay
|
self.delay = delay
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
# Increment tick counter and spawn barrier when delay reached
|
||||||
if self.active:
|
if self.active:
|
||||||
self.tick_counter += 1
|
self.tick_counter += 1
|
||||||
if self.tick_counter >= self.delay:
|
if self.tick_counter >= self.delay:
|
||||||
@@ -199,6 +230,5 @@ class Spawner:
|
|||||||
self.tick_counter = 0
|
self.tick_counter = 0
|
||||||
|
|
||||||
def spawn(self):
|
def spawn(self):
|
||||||
random_y = random.randint(0, HEIGHT - 100)
|
# Create a new barrier at a random vertical position on the right edge
|
||||||
new_barrier = Barrier(self.barrier_image_path, WIDTH, random_y)
|
return Barrier(self.barrier_image_path, WIDTH, random.randint(0, HEIGHT - 100))
|
||||||
return new_barrier
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ matplotlib.use('Agg')
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import random
|
import random
|
||||||
|
|
||||||
POPULATION_SIZE = 50 # SET TO 500 IF YOU HAVE A GOOD PC
|
POPULATION_SIZE = 50 # Number of horses in population
|
||||||
MUTATION_RATE = 0.5
|
MUTATION_RATE = 0.5
|
||||||
POPULATION_NEW = 0.1
|
POPULATION_NEW = 0.1
|
||||||
POPULATION_BEST = 0.3
|
POPULATION_BEST = 0.3
|
||||||
@@ -42,6 +42,7 @@ last_barrier = None
|
|||||||
|
|
||||||
def init_game():
|
def init_game():
|
||||||
global gameobjects, horses, barriers, upper_bound_rect, lower_bound_rect, spawner, last_barrier, BARRIER_SPEED
|
global gameobjects, horses, barriers, upper_bound_rect, lower_bound_rect, spawner, last_barrier, BARRIER_SPEED
|
||||||
|
# Initialize background and game objects for a new iteration
|
||||||
#random.seed(BARRIER_SEED) #SET SEED
|
#random.seed(BARRIER_SEED) #SET SEED
|
||||||
grass = Background("images/Grass.jpg", 0, 0)
|
grass = Background("images/Grass.jpg", 0, 0)
|
||||||
grass.set_size(WIDTH, HEIGHT)
|
grass.set_size(WIDTH, HEIGHT)
|
||||||
@@ -49,6 +50,7 @@ def init_game():
|
|||||||
gameobjects = [grass]
|
gameobjects = [grass]
|
||||||
barriers = []
|
barriers = []
|
||||||
|
|
||||||
|
# Reset horses to starting position and state
|
||||||
for horse in horses:
|
for horse in horses:
|
||||||
horse.set_position(50, HEIGHT / 2)
|
horse.set_position(50, HEIGHT / 2)
|
||||||
horse.stopped = False
|
horse.stopped = False
|
||||||
@@ -64,6 +66,7 @@ def init_game():
|
|||||||
gameobjects.extend(horses)
|
gameobjects.extend(horses)
|
||||||
gameobjects.extend(barriers)
|
gameobjects.extend(barriers)
|
||||||
|
|
||||||
|
# Define upper and lower bounds for horse movement
|
||||||
upper_bound_rect = pygame.Rect(0, 0, WIDTH, -10)
|
upper_bound_rect = pygame.Rect(0, 0, WIDTH, -10)
|
||||||
lower_bound_rect = pygame.Rect(0, HEIGHT, WIDTH, 10)
|
lower_bound_rect = pygame.Rect(0, HEIGHT, WIDTH, 10)
|
||||||
|
|
||||||
@@ -73,6 +76,7 @@ def init_game():
|
|||||||
|
|
||||||
|
|
||||||
def get_features(horse: Horse):
|
def get_features(horse: Horse):
|
||||||
|
# Extract normalized features for neural network input
|
||||||
features = [last_barrier.rect.topleft[0], last_barrier.rect.topleft[1],
|
features = [last_barrier.rect.topleft[0], last_barrier.rect.topleft[1],
|
||||||
last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1],
|
last_barrier.rect.bottomright[0], last_barrier.rect.bottomright[1],
|
||||||
horse.rect.topleft[0], horse.rect.topleft[1],
|
horse.rect.topleft[0], horse.rect.topleft[1],
|
||||||
@@ -92,6 +96,7 @@ genecticAlg = GeneticAlgorithm(POPULATION_SIZE, MUTATION_RATE, POPULATION_BEST,
|
|||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
# Save fitness plot on exit
|
||||||
plt.plot(running_fitnesses)
|
plt.plot(running_fitnesses)
|
||||||
plt.xlabel("Iteration")
|
plt.xlabel("Iteration")
|
||||||
plt.ylabel("Current Fitness")
|
plt.ylabel("Current Fitness")
|
||||||
@@ -106,6 +111,7 @@ while True:
|
|||||||
if not horse.stopped:
|
if not horse.stopped:
|
||||||
data = get_features(horse)
|
data = get_features(horse)
|
||||||
res = genecticAlg.predict(data, i)
|
res = genecticAlg.predict(data, i)
|
||||||
|
# Control horse movement based on neural network output
|
||||||
if res == 0:
|
if res == 0:
|
||||||
horse.up()
|
horse.up()
|
||||||
elif res == 1:
|
elif res == 1:
|
||||||
@@ -113,6 +119,7 @@ while True:
|
|||||||
else:
|
else:
|
||||||
horse.stay()
|
horse.stay()
|
||||||
else:
|
else:
|
||||||
|
# Move stopped horses left with barriers
|
||||||
horse.move(-BARRIER_SPEED, 0)
|
horse.move(-BARRIER_SPEED, 0)
|
||||||
|
|
||||||
horse.apply_vacceleration()
|
horse.apply_vacceleration()
|
||||||
@@ -121,6 +128,7 @@ while True:
|
|||||||
|
|
||||||
spawner.handle()
|
spawner.handle()
|
||||||
if spawner.tick_counter == 0:
|
if spawner.tick_counter == 0:
|
||||||
|
# Spawn new barrier periodically
|
||||||
new_barrier = spawner.spawn()
|
new_barrier = spawner.spawn()
|
||||||
barriers.append(new_barrier)
|
barriers.append(new_barrier)
|
||||||
gameobjects.append(new_barrier)
|
gameobjects.append(new_barrier)
|
||||||
@@ -133,6 +141,7 @@ while True:
|
|||||||
horse.update_animation()
|
horse.update_animation()
|
||||||
|
|
||||||
if not horse.stopped:
|
if not horse.stopped:
|
||||||
|
# Check collisions with barriers and bounds
|
||||||
for barrier in barriers:
|
for barrier in barriers:
|
||||||
if horse.rect.colliderect(barrier.rect):
|
if horse.rect.colliderect(barrier.rect):
|
||||||
horse.stop()
|
horse.stop()
|
||||||
@@ -146,8 +155,8 @@ while True:
|
|||||||
current_fitnesses = [horse.fitness for horse in horses]
|
current_fitnesses = [horse.fitness for horse in horses]
|
||||||
current_fitness = max(current_fitnesses)
|
current_fitness = max(current_fitnesses)
|
||||||
|
|
||||||
|
|
||||||
if all(horse.stopped for horse in horses):
|
if all(horse.stopped for horse in horses):
|
||||||
|
# All horses stopped: evolve population and start new iteration
|
||||||
UI.iteration_num += 1
|
UI.iteration_num += 1
|
||||||
genecticAlg.learn([x.fitness for x in horses])
|
genecticAlg.learn([x.fitness for x in horses])
|
||||||
|
|
||||||
@@ -160,6 +169,7 @@ while True:
|
|||||||
UI.draw(screen)
|
UI.draw(screen)
|
||||||
UI.draw_marks(screen)
|
UI.draw_marks(screen)
|
||||||
|
|
||||||
|
# Display iteration and fitness info
|
||||||
iteration_num_txt = font.render(f"Iteration: {current_iteration}", True, BLACK)
|
iteration_num_txt = font.render(f"Iteration: {current_iteration}", True, BLACK)
|
||||||
current_fitness_txt = font.render(
|
current_fitness_txt = font.render(
|
||||||
f"Current Fitness: {current_fitness}", True, BLACK)
|
f"Current Fitness: {current_fitness}", True, BLACK)
|
||||||
|
|||||||
Reference in New Issue
Block a user