diff --git a/gameobjects.py b/gameobjects.py index d09d645..45fb86b 100644 --- a/gameobjects.py +++ b/gameobjects.py @@ -6,19 +6,31 @@ WIDTH, HEIGHT = 800, 600 class GameObject: x = 0 y = 0 - def __init__(self, image_path, x=0, y=0): + + def __init__(self, image_path, x=0, y=0, offset = (0, 0)): + self.children = [] self.image = pygame.image.load(image_path) # Load the image self.rect = self.image.get_rect(topleft=(x,y)) # Set the rectangle for the image self.set_position(x, y) # Set initial position + self.offset = offset def draw(self, surface): surface.blit(self.image, self.rect) # Draw the image on the surface + if len(self.children) == 0: return + for child in self.children: + child.draw(surface) def set_position(self, x, y): self.rect.x = x # Update rectangle position self.rect.y = y self.x = x # Update object position self.y = y + if len(self.children) == 0: return + for child in self.children: + child.set_position(x + child.offset[0], y + child.offset[1]) def move(self, dx, dy): self.set_position(self.rect.x + dx, self.rect.y + dy) # Move object by dx and dy + if len(self.children) == 0: return + for child in self.children: + self.set_position(self.rect.x + dx, self.rect.y + dy) def set_size(self, width, height): self.image = pygame.transform.scale(self.image, (width,height)) # Resize the image self.rect = self.image.get_rect(topleft=(self.x,self.y)) # Update rectangle size @@ -27,6 +39,11 @@ class GameObject: self.rect = self.image.get_rect(topleft=(self.x,self.y)) # Update rectangle size def get_position(self): return (self.x, self.y) # Return current position + def set_children(self, child): + self.children.append(child) + def recolor(self, color): #Method to recolor + self.image.fill(color, special_flags=pygame.BLEND_MULT) + self.image.set_colorkey(None) # Explicitly disable colorkey class Horse(GameObject): default_vacceleration = 0.5 # Default acceleration value @@ -34,12 +51,19 @@ class Horse(GameObject): vacceleration = 0 # Vertical acceleration stopped = False # Whether the horse is stopped color = () # Color of the horse's mark + ribbon_fill = None + ribbon_out = None def __init__(self, image_path, x, y): + super().__init__(image_path, x, y) self.vspeed = 0 self.vacceleration = 0 self.stopped = False self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # Random color for the mark - super().__init__(image_path, x, y) + self.ribbon_fill = GameObject("images/Ribbon_fill.png", self.x, self.y, (-13, -26)) + self.ribbon_fill.recolor(self.color) + self.ribbon_out = GameObject("images/Ribbon_out.png", self.x, self.y, (-13, -26)) + self.set_children(self.ribbon_fill) + self.set_children(self.ribbon_out) self.set_size(75, 75) # Set the size of the horse def apply_vspeed(self): pos = self.get_position() @@ -63,8 +87,8 @@ class Horse(GameObject): self.set_vacceleration(0) self.set_vspeed(0) def draw(self, surface): - surface.blit(self.image, self.rect) # Draw the horse - pygame.draw.rect(surface, self.color, (self.x + 40, self.y + 20, 25, 25)) # Draw the horse's mark + super().draw(surface) # Draw the horse + #pygame.draw.rect(surface, self.color, (self.x + 40, self.y + 20, 25, 25)) # Draw the horse's mark class Background(GameObject): def __init__(self, image_path, x, y): @@ -115,6 +139,7 @@ class UI: yu += self.vertical_offset xu = unactive_marks_section_pos[0] + class Spawner: active = True # Whether the spawner is active frequency = 500 # Number of ticks between spawns @@ -133,4 +158,4 @@ class Spawner: def spawn(self): random_y = random.randint(0, HEIGHT - 100) # Random Y position for the barrier new_barrier = Barrier(self.barrier_image_path, WIDTH, random_y) # Create a new barrier - return new_barrier \ No newline at end of file + return new_barrier diff --git a/images/Horse_1.png b/images/Horse_1.png new file mode 100644 index 0000000..6585751 Binary files /dev/null and b/images/Horse_1.png differ diff --git a/images/Horse_2.png b/images/Horse_2.png new file mode 100644 index 0000000..042d0ce Binary files /dev/null and b/images/Horse_2.png differ diff --git a/images/Horse_3.png b/images/Horse_3.png new file mode 100644 index 0000000..5b7e70b Binary files /dev/null and b/images/Horse_3.png differ diff --git a/images/Ribbon_fill.png b/images/Ribbon_fill.png new file mode 100644 index 0000000..531b340 Binary files /dev/null and b/images/Ribbon_fill.png differ diff --git a/images/Ribbon_out.png b/images/Ribbon_out.png new file mode 100644 index 0000000..91c0e22 Binary files /dev/null and b/images/Ribbon_out.png differ diff --git a/images/barrier.png b/images/barrier.png index f30c117..fb634bb 100644 Binary files a/images/barrier.png and b/images/barrier.png differ diff --git a/images/horse.png b/images/horse.png deleted file mode 100644 index 0574bf3..0000000 Binary files a/images/horse.png and /dev/null differ diff --git a/main.py b/main.py index e8be994..4871aca 100644 --- a/main.py +++ b/main.py @@ -11,22 +11,22 @@ WHITE = (255, 255, 255) # Define white color UI = UI() # Initialize UI grass = Background("images/grass.jpg", 0, 0) # Create background -horse1 = Horse("images/horse.png", 50, 50) # Create a horse -barrier1 = Barrier("images/barrier.png", 400, 300) # Create a barrier +horse1 = Horse("images/Horse_1.png", 50, 50) # Create a horse +barrier1 = Barrier("images/Barrier.png", 400, 300) # Create a barrier BARRIER_SPEED = 1 # Speed of the barriers grass.set_size(WIDTH, HEIGHT) # Set background size gameobjects = [] # List of all game objects -horses = [Horse("images/horse.png", 50, 50 * i) for i in range(50)] # Create 50 horses +horses = [Horse("images/Horse_1.png", 50, 50 * i) for i in range(50)] # Create 50 horses barriers = [barrier1] # List of barriers gameobjects.extend([grass]) # Add background to game objects gameobjects.extend(horses) # Add horses to game objects gameobjects.extend(barriers) # Add barriers to game objects -spawner = Spawner("images/barrier.png") # Initialize spawner +spawner = Spawner("images/Barrier.png") # Initialize spawner UI.add_children(horses) # Add horses to UI while True: