Add collision and horse stopping on collision. Start implementing flags functionality

This commit is contained in:
Emil Shanaty
2025-03-15 05:11:44 +03:00
parent f156a35da9
commit ded99fca0c
4 changed files with 163 additions and 117 deletions
Binary file not shown.
Binary file not shown.
+97 -65
View File
@@ -1,65 +1,97 @@
import pygame import pygame
class GameObject: import random
x = 0
y = 0 class GameObject:
def __init__(self, image_path, x=0, y=0): x = 0
self.image = pygame.image.load(image_path) y = 0
self.rect = self.image.get_rect(topleft=(x,y)) def __init__(self, image_path, x=0, y=0):
self.set_position(x, y) self.image = pygame.image.load(image_path)
def draw(self, surface): self.rect = self.image.get_rect(topleft=(x,y))
surface.blit(self.image, self.rect) self.set_position(x, y)
def set_position(self, x, y): # TODO: FIX CENTERING def draw(self, surface):
self.rect.x = x surface.blit(self.image, self.rect)
self.rect.y = y def set_position(self, x, y): # TODO: FIX CENTERING
self.x = x self.rect.x = x
self.y = y self.rect.y = y
def move(self, dx, dy): self.x = x
self.set_position(self.rect.x + dx, self.rect.y + dy) self.y = y
def set_size(self, width, height): def move(self, dx, dy):
self.image = pygame.transform.scale(self.image, (width,height)) self.set_position(self.rect.x + dx, self.rect.y + dy)
self.rect = self.image.get_rect(topleft=(self.x,self.y)) def set_size(self, width, height):
def scale_by(self, scale): self.image = pygame.transform.scale(self.image, (width,height))
self.image = pygame.transform.scale(self.image, (self.image.get_width() * scale, self.image.get_height() * scale)) 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 get_position(self): self.image = pygame.transform.scale(self.image, (self.image.get_width() * scale, self.image.get_height() * scale))
return (self.x, self.y) self.rect = self.image.get_rect(topleft=(self.x,self.y))
def get_position(self):
return (self.x, self.y)
class Horse(GameObject):
default_vacceleration = 1
vspeed = 0 class Horse(GameObject):
vacceleration = 0 default_vacceleration = 0.5
def __init__(self, image_path, x, y): vspeed = 0
self.vspeed = 0 vacceleration = 0
self.vacceleration = 0 stopped = False
super().__init__(image_path, x, y) color = ()
def apply_vspeed(self): def __init__(self, image_path, x, y):
pos = self.get_position() self.vspeed = 0
self.set_position(pos[0], pos[1] + self.vspeed) self.vacceleration = 0
def apply_vacceleration(self): self.stopped = False
self.vspeed += self.vacceleration self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
def set_vspeed(self, speed): super().__init__(image_path, x, y)
self.vspeed = speed self.set_size(75, 75)
def add_vspeed(self, delta): def apply_vspeed(self):
self.vspeed += delta pos = self.get_position()
def set_vacceleration(self, vacceleration): self.set_position(pos[0], pos[1] + self.vspeed)
self.vacceleration = vacceleration def apply_vacceleration(self):
def up(self): self.vspeed += self.vacceleration
self.set_vacceleration(-self.default_vacceleration) def set_vspeed(self, speed):
def down(self): self.vspeed = speed
self.set_vacceleration(self.default_vacceleration) def add_vspeed(self, delta):
def stay(self): self.vspeed += delta
self.set_vacceleration(0) def set_vacceleration(self, vacceleration):
self.vacceleration = vacceleration
def up(self):
self.set_vacceleration(-self.default_vacceleration)
def down(self):
class Background(GameObject): self.set_vacceleration(self.default_vacceleration)
def __init__(self, image_path, x, y): def stay(self):
super().__init__(image_path, x, y) self.set_vacceleration(0)
def stop(self):
class Barrier(GameObject): self.stopped = True
def __init__(self, image_path, x, y): self.set_vacceleration(0)
super().__init__(image_path, x, y) self.set_vspeed(0)
class Background(GameObject):
def __init__(self, image_path, x, y):
super().__init__(image_path, x, y)
class Barrier(GameObject):
def __init__(self, image_path, x, y):
super().__init__(image_path, x, y)
class UI:
children = []
horizontal_offset = 25
mark_size = 5
def __init__(self):
self.children = []
def add_children(self, children):
new_children = [child for child in children if child not in self.children]
self.children.extend(new_children)
def draw_marks(self, screen):
x0 = 50
y0 = 300
for child in self.children:
if isinstance(child, Horse):
x0 += self.horizontal_offset
x1 = x0 + self.mark_size
y1 = y0 - self.mark_size
pygame.draw.rect(screen, child.color, (x0, y0, x1, y1))
+66 -52
View File
@@ -1,52 +1,66 @@
import pygame import pygame
import sys import sys
from gameobjects import * from gameobjects import *
pygame.init() pygame.init()
WIDTH, HEIGHT = 800, 600 WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT)) screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("NIC_Project") pygame.display.set_caption("NIC_Project")
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
grass = Background("images/grass.jpg", 0, 0) UI = UI()
horse1 = Horse("images/horse.png", 50, 50) grass = Background("images/grass.jpg", 0, 0)
barrier1 = Barrier("images/barrier.png", 400, 300) horse1 = Horse("images/horse.png", 50, 50)
barrier1 = Barrier("images/barrier.png", 400, 300)
BARRIER_SPEED = 1
BARRIER_SPEED = 1
horse1.set_size(75, 75)
grass.set_size(WIDTH, HEIGHT) grass.set_size(WIDTH, HEIGHT)
gameobjects = [grass, horse1, barrier1] gameobjects = [grass, horse1, barrier1]
horses = [horse1] horses = [Horse("images/horse.png", 50, 50) for i in range(5)]
barriers = [barrier1] barriers = [barrier1]
while True:
for event in pygame.event.get(): UI.add_children(horses)
if event.type == pygame.QUIT: while True:
pygame.quit() for event in pygame.event.get():
sys.exit() if event.type == pygame.QUIT:
pygame.quit()
keys = pygame.key.get_pressed() sys.exit()
for horse in horses: keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
horse.up() for horse in horses:
elif keys[pygame.K_DOWN]: if horse.stopped == False:
horse.down() if keys[pygame.K_UP]:
else: horse.up()
horse.stay() elif keys[pygame.K_DOWN]:
horse.down()
horse.apply_vacceleration() else:
horse.apply_vspeed() horse.stay()
horse.draw(screen) else:
horse.move(-BARRIER_SPEED, 0)
for barrier in barriers:
barrier.move(-BARRIER_SPEED, 0) horse.apply_vacceleration()
horse.apply_vspeed()
for object in gameobjects: horse.draw(screen)
object.draw(screen)
for barrier in barriers:
pygame.display.flip() barrier.move(-BARRIER_SPEED, 0)
pygame.time.Clock().tick(60)
# Checking collisions between horses and barriers
for horse in horses:
if horse.stopped == False:
for barrier in barriers:
if horse.rect.colliderect(barrier.rect):
horse.stop()
for object in gameobjects:
object.draw(screen)
UI.draw_marks(screen)
pygame.display.flip()
pygame.time.Clock().tick(60)