0
\$\begingroup\$

I'm trying to make a simple platformer game, which has code to move the camera towards a player as they jump around.

Now I have two classes: one for the player, and one for the platform. In the Platforms class, I create a rect of the same size (I changed the size for simplicity to make sure everything is working) and the same position. In the Player class, I want to check if the player is colliding with the platform rect.

The player does seem to detect the platform, but at completely different coords.

My whole code:

import math
import sys

import pygame

pygame.init()

display = pygame.display

WHITE = (255, 255, 255)


class Player(pygame.sprite.Sprite):

    def __init__(self, pos, group, player_image_path):
        super().__init__(group)
        self.apply_gravity = True
        self.image = pygame.image.load(player_image_path).convert_alpha()
        self.rect = self.image.get_rect(center=pos)
        self.direction = pygame.math.Vector2()
        self.speed = 5

    def input(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_w]:
            self.direction.y = -1
        elif keys[pygame.K_s]:
            self.direction.y = 1
        else:
            self.direction.y = 0
        if keys[pygame.K_a]:
            self.direction.x = -1
        elif keys[pygame.K_d]:
            self.direction.x = 1
        else:
            self.direction.x = 0

    def gravity(self):
        self.rect.y += math.pi

    def update(self):
        self.input()
        if self.apply_gravity:
            self.gravity()
        self.rect.center += self.direction * self.speed

    def collision(self, col_object):
        if self.rect.colliderect(col_object.collider_rect):
            print("Ehhhhhhhhh")
            self.apply_gravity = False
        else:
            self.apply_gravity = True


class Platforms(pygame.sprite.Sprite):
    def __init__(self, pos, group, image_path):
        super().__init__(group)
        self.rect_color = None
        self.collider_rect = None
        self.surface = display.get_surface()
        self.image = pygame.image.load(image_path)
        self.colorkey = self.image.get_at((0, 0))
        self.image.set_colorkey(self.colorkey)
        self.image = pygame.transform.scale(self.image, (128 * 3, 32 * 3)).convert_alpha()
        self.rect = self.image.get_rect(topleft=pos)

    def draw_collider(self, x, y):
        self.rect_color = (255, 200, 255)
        self.collider_rect = pygame.draw.rect(self.surface, self.rect_color,
                                              pygame.Rect(x, y, 128 * 4, 32 * 4))


class CameraGroup(pygame.sprite.Group):
    def __init__(self, surface, background_image_path):
        super().__init__()
        self.surface = surface

        self.platforms = []

        self.background_image = pygame.image.load(background_image_path).convert_alpha()
        self.background_rect = self.background_image.get_rect(topleft=(0, 0)).topleft

        self.half_x = pygame.display.get_window_size()[0] // 2
        self.half_y = pygame.display.get_window_size()[1] // 2

        self.offset = pygame.math.Vector2()

    def center_camera(self, target):
        self.offset.x = target.rect.centerx - self.half_x
        self.offset.y = target.rect.centery - self.half_y
        self.offset.y -= 10

    def draw_sprites(self, player_object):
        self.center_camera(player_object)
        ground_offset_pos = self.background_rect - self.offset - pygame.math.Vector2(500, 305)
        self.surface.blit(self.background_image, ground_offset_pos)

        for sprite in sorted(self.sprites(), key=lambda sprites: sprites.rect.centery):
            offset_pos = sprite.rect.topleft - self.offset
            if isinstance(sprite, Platforms):
                sprite.draw_collider(offset_pos.x, offset_pos.y)
                self.platforms.append(sprite)
            elif isinstance(sprite, Player):
                for platform_obj in self.platforms:
                    sprite.collision(platform_obj)
            self.surface.blit(sprite.image, offset_pos)


screen = display.set_mode((1200, 800))
screen.fill(WHITE)
display.update()

running = True

camera_group = CameraGroup(screen, 'sym_attractors.jpg')

platform = Platforms((100, 100), camera_group, 'flower_plt.jpeg')
player = Player((0, 0), camera_group, 'player.jpeg')

clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit(0)

    player.input()
    player.update()

    camera_group.update()
    camera_group.draw_sprites(player)

    display.update()
    clock.tick(60)  # Adjust the frame rate as needed (e.g., 60 FPS)

I'm also open to learn mask overlapping if relevant.

\$\endgroup\$
2
  • \$\begingroup\$ "but at completely different coords" What coordinates are you expecting and what are you getting? \$\endgroup\$ Commented Mar 19, 2024 at 12:12
  • \$\begingroup\$ Considering you are drawing using an offset_pos, is there a chance you don't take that variable into account when calculating your collisions? \$\endgroup\$ Commented Mar 19, 2024 at 13:19

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.