Add active/unactive horse colour management. Add barrier spawning every 500 game ticks. Generated comments to describe code.
This commit is contained in:
Binary file not shown.
Binary file not shown.
+88
-49
@@ -1,97 +1,136 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
WIDTH, HEIGHT = 800, 600
|
||||||
|
|
||||||
class GameObject:
|
class GameObject:
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
def __init__(self, image_path, x=0, y=0):
|
def __init__(self, image_path, x=0, y=0):
|
||||||
self.image = pygame.image.load(image_path)
|
self.image = pygame.image.load(image_path) # Load the image
|
||||||
self.rect = self.image.get_rect(topleft=(x,y))
|
self.rect = self.image.get_rect(topleft=(x,y)) # Set the rectangle for the image
|
||||||
self.set_position(x, y)
|
self.set_position(x, y) # Set initial position
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
surface.blit(self.image, self.rect)
|
surface.blit(self.image, self.rect) # Draw the image on the surface
|
||||||
def set_position(self, x, y): # TODO: FIX CENTERING
|
def set_position(self, x, y):
|
||||||
self.rect.x = x
|
self.rect.x = x # Update rectangle position
|
||||||
self.rect.y = y
|
self.rect.y = y
|
||||||
self.x = x
|
self.x = x # Update object position
|
||||||
self.y = y
|
self.y = y
|
||||||
def move(self, dx, dy):
|
def move(self, dx, dy):
|
||||||
self.set_position(self.rect.x + dx, self.rect.y + dy)
|
self.set_position(self.rect.x + dx, self.rect.y + dy) # Move object by dx and dy
|
||||||
def set_size(self, width, height):
|
def set_size(self, width, height):
|
||||||
self.image = pygame.transform.scale(self.image, (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))
|
self.rect = self.image.get_rect(topleft=(self.x,self.y)) # Update rectangle size
|
||||||
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))
|
self.image = pygame.transform.scale(self.image, (self.image.get_width() * scale, self.image.get_height() * scale)) # Scale the image
|
||||||
self.rect = self.image.get_rect(topleft=(self.x,self.y))
|
self.rect = self.image.get_rect(topleft=(self.x,self.y)) # Update rectangle size
|
||||||
def get_position(self):
|
def get_position(self):
|
||||||
return (self.x, self.y)
|
return (self.x, self.y) # Return current position
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Horse(GameObject):
|
class Horse(GameObject):
|
||||||
default_vacceleration = 0.5
|
default_vacceleration = 0.5 # Default acceleration value
|
||||||
vspeed = 0
|
vspeed = 0 # Vertical speed
|
||||||
vacceleration = 0
|
vacceleration = 0 # Vertical acceleration
|
||||||
stopped = False
|
stopped = False # Whether the horse is stopped
|
||||||
color = ()
|
color = () # Color of the horse's mark
|
||||||
def __init__(self, image_path, x, y):
|
def __init__(self, image_path, x, y):
|
||||||
self.vspeed = 0
|
self.vspeed = 0
|
||||||
self.vacceleration = 0
|
self.vacceleration = 0
|
||||||
self.stopped = False
|
self.stopped = False
|
||||||
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
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)
|
super().__init__(image_path, x, y)
|
||||||
self.set_size(75, 75)
|
self.set_size(75, 75) # Set the size of the horse
|
||||||
def apply_vspeed(self):
|
def apply_vspeed(self):
|
||||||
pos = self.get_position()
|
pos = self.get_position()
|
||||||
self.set_position(pos[0], pos[1] + self.vspeed)
|
self.set_position(pos[0], pos[1] + self.vspeed) # Apply vertical speed to position
|
||||||
def apply_vacceleration(self):
|
def apply_vacceleration(self):
|
||||||
self.vspeed += self.vacceleration
|
self.vspeed += self.vacceleration # Apply acceleration to speed
|
||||||
def set_vspeed(self, speed):
|
def set_vspeed(self, speed):
|
||||||
self.vspeed = speed
|
self.vspeed = speed # Set vertical speed
|
||||||
def add_vspeed(self, delta):
|
def add_vspeed(self, delta):
|
||||||
self.vspeed += delta
|
self.vspeed += delta # Add to vertical speed
|
||||||
def set_vacceleration(self, vacceleration):
|
def set_vacceleration(self, vacceleration):
|
||||||
self.vacceleration = vacceleration
|
self.vacceleration = vacceleration # Set vertical acceleration
|
||||||
def up(self):
|
def up(self):
|
||||||
self.set_vacceleration(-self.default_vacceleration)
|
self.set_vacceleration(-self.default_vacceleration) # Move up
|
||||||
def down(self):
|
def down(self):
|
||||||
self.set_vacceleration(self.default_vacceleration)
|
self.set_vacceleration(self.default_vacceleration) # Move down
|
||||||
def stay(self):
|
def stay(self):
|
||||||
self.set_vacceleration(0)
|
self.set_vacceleration(0) # Stop vertical movement
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.stopped = True
|
self.stopped = True # Stop the horse
|
||||||
self.set_vacceleration(0)
|
self.set_vacceleration(0)
|
||||||
self.set_vspeed(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
|
||||||
|
|
||||||
class Background(GameObject):
|
class Background(GameObject):
|
||||||
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) # Initialize background
|
||||||
|
|
||||||
class Barrier(GameObject):
|
class Barrier(GameObject):
|
||||||
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) # Initialize barrier
|
||||||
|
|
||||||
class UI:
|
class UI:
|
||||||
children = []
|
children = [] # List of child objects
|
||||||
horizontal_offset = 25
|
horizontal_offset = 25 # Horizontal spacing between marks
|
||||||
mark_size = 5
|
vertical_offset = -25 # Vertical spacing between marks
|
||||||
|
mark_size = 25 # Size of the marks
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.children = []
|
self.children = [] # Initialize children list
|
||||||
def add_children(self, children):
|
def add_children(self, children):
|
||||||
new_children = [child for child in children if child not in self.children]
|
new_children = [child for child in children if child not in self.children] # Add new children
|
||||||
self.children.extend(new_children)
|
self.children.extend(new_children)
|
||||||
def draw_marks(self, screen):
|
def draw_marks(self, screen):
|
||||||
x0 = 50
|
active_marks = [] # List of active horses
|
||||||
|
unactive_marks = [] # List of inactive horses
|
||||||
|
for i in range(len(self.children)):
|
||||||
|
if isinstance(self.children[i], Horse):
|
||||||
|
if (self.children[i].stopped == False):
|
||||||
|
active_marks.append(self.children[i]) # Add active horses
|
||||||
|
else:
|
||||||
|
unactive_marks.append(self.children[i]) # Add inactive horses
|
||||||
|
|
||||||
y0 = 300
|
width = 10 # Number of marks per row
|
||||||
|
active_marks_section_pos = (0 - self.mark_size, 600 - self.mark_size) # Starting position for active marks
|
||||||
|
xa = active_marks_section_pos[0]
|
||||||
|
ya = active_marks_section_pos[1]
|
||||||
|
for i in range(len(active_marks)):
|
||||||
|
xa += self.horizontal_offset
|
||||||
|
pygame.draw.rect(screen, self.children[i].color, (xa, ya, self.mark_size, self.mark_size)) # Draw active marks
|
||||||
|
if xa >= width * (self.mark_size - 3): # Move to the next row if the row is full
|
||||||
|
ya += self.vertical_offset
|
||||||
|
xa = active_marks_section_pos[0]
|
||||||
|
|
||||||
for child in self.children:
|
unactive_marks_section_pos = (800, 600 - self.mark_size) # Starting position for inactive marks
|
||||||
if isinstance(child, Horse):
|
xu = unactive_marks_section_pos[0]
|
||||||
x0 += self.horizontal_offset
|
yu = unactive_marks_section_pos[1]
|
||||||
x1 = x0 + self.mark_size
|
for i in range(len(unactive_marks)):
|
||||||
y1 = y0 - self.mark_size
|
xu -= self.horizontal_offset
|
||||||
pygame.draw.rect(screen, child.color, (x0, y0, x1, y1))
|
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
|
||||||
|
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
|
||||||
|
tick_counter = 0 # Counter for ticks
|
||||||
|
|
||||||
|
def __init__(self, barrier_image_path):
|
||||||
|
self.barrier_image_path = barrier_image_path # Path to the barrier image
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
if self.active:
|
||||||
|
self.tick_counter += 1 # Increment tick counter
|
||||||
|
if self.tick_counter >= self.frequency: # Check if it's time to spawn
|
||||||
|
self.spawn()
|
||||||
|
self.tick_counter = 0 # Reset tick counter
|
||||||
|
|
||||||
|
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
|
||||||
@@ -4,63 +4,72 @@ from gameobjects import *
|
|||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
WIDTH, HEIGHT = 800, 600
|
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # Initialize the screen
|
||||||
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
pygame.display.set_caption("NIC_Project") # Set window title
|
||||||
pygame.display.set_caption("NIC_Project")
|
|
||||||
|
|
||||||
WHITE = (255, 255, 255)
|
WHITE = (255, 255, 255) # Define white color
|
||||||
|
|
||||||
UI = UI()
|
UI = UI() # Initialize UI
|
||||||
grass = Background("images/grass.jpg", 0, 0)
|
grass = Background("images/grass.jpg", 0, 0) # Create background
|
||||||
horse1 = Horse("images/horse.png", 50, 50)
|
horse1 = Horse("images/horse.png", 50, 50) # Create a horse
|
||||||
barrier1 = Barrier("images/barrier.png", 400, 300)
|
barrier1 = Barrier("images/barrier.png", 400, 300) # Create a barrier
|
||||||
|
|
||||||
BARRIER_SPEED = 1
|
BARRIER_SPEED = 1 # Speed of the barriers
|
||||||
|
|
||||||
grass.set_size(WIDTH, HEIGHT)
|
grass.set_size(WIDTH, HEIGHT) # Set background size
|
||||||
|
|
||||||
gameobjects = [grass, horse1, barrier1]
|
gameobjects = [] # List of all game objects
|
||||||
horses = [Horse("images/horse.png", 50, 50) for i in range(5)]
|
horses = [Horse("images/horse.png", 50, 50 * i) for i in range(50)] # Create 50 horses
|
||||||
barriers = [barrier1]
|
barriers = [barrier1] # List of barriers
|
||||||
|
|
||||||
UI.add_children(horses)
|
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
|
||||||
|
|
||||||
|
UI.add_children(horses) # Add horses to UI
|
||||||
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: # Handle window close event
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed() # Get pressed keys
|
||||||
|
|
||||||
for horse in horses:
|
for horse in horses:
|
||||||
if horse.stopped == False:
|
if horse.stopped == False: # If the horse is not stopped
|
||||||
if keys[pygame.K_UP]:
|
if keys[pygame.K_UP]: # Move horse up if UP key is pressed
|
||||||
horse.up()
|
horse.up()
|
||||||
elif keys[pygame.K_DOWN]:
|
elif keys[pygame.K_DOWN]: # Move horse down if DOWN key is pressed
|
||||||
horse.down()
|
horse.down()
|
||||||
else:
|
else:
|
||||||
horse.stay()
|
horse.stay() # Stop vertical movement if no key is pressed
|
||||||
else:
|
else:
|
||||||
horse.move(-BARRIER_SPEED, 0)
|
horse.move(-BARRIER_SPEED, 0) # Move stopped horse to the left
|
||||||
|
|
||||||
horse.apply_vacceleration()
|
horse.apply_vacceleration() # Apply acceleration to horse
|
||||||
horse.apply_vspeed()
|
horse.apply_vspeed() # Apply speed to horse
|
||||||
horse.draw(screen)
|
horse.draw(screen) # Draw the horse
|
||||||
|
|
||||||
|
spawner.handle() # Handle spawner logic
|
||||||
|
if spawner.tick_counter == 0: # If a new barrier is spawned
|
||||||
|
new_barrier = spawner.spawn() # Spawn a new barrier
|
||||||
|
barriers.append(new_barrier) # Add barrier to barriers list
|
||||||
|
gameobjects.append(new_barrier) # Add barrier to game objects
|
||||||
|
|
||||||
for barrier in barriers:
|
for barrier in barriers:
|
||||||
barrier.move(-BARRIER_SPEED, 0)
|
barrier.move(-BARRIER_SPEED, 0) # Move barriers to the left
|
||||||
|
|
||||||
# Checking collisions between horses and barriers
|
|
||||||
for horse in horses:
|
for horse in horses:
|
||||||
if horse.stopped == False:
|
if horse.stopped == False: # Check for collisions between horses and barriers
|
||||||
for barrier in barriers:
|
for barrier in barriers:
|
||||||
if horse.rect.colliderect(barrier.rect):
|
if horse.rect.colliderect(barrier.rect): # If collision detected
|
||||||
horse.stop()
|
horse.stop() # Stop the horse
|
||||||
|
|
||||||
|
|
||||||
for object in gameobjects:
|
for object in gameobjects:
|
||||||
object.draw(screen)
|
object.draw(screen) # Draw all game objects
|
||||||
UI.draw_marks(screen)
|
UI.draw_marks(screen) # Draw UI marks
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip() # Update the display
|
||||||
pygame.time.Clock().tick(60)
|
pygame.time.Clock().tick(160) # Limit the frame rate to 160 FPS
|
||||||
Reference in New Issue
Block a user