Skip to content
Advertisement

Drawings rects over the player

EDIT: Reposted question so it is clearer.

My issue is that the player rectangles are not on the player because of the camera offset. As a result, the game looks like this (see image 1). It’s not working because of the camera offset. I successfully repositioned the yellow rect over the player but I am having trouble repositioning the red rectangle.

I have added comments to my Camera class explaining what I tried and noticed. When I remove the offset to 0, the rects are positioned how I want (but obviously the camera doesn’t work anymore). See image 2 for what I am trying to achieve.

This is image 1: https://i.stack.imgur.com/JnFPH.png

This is image 2: https://i.stack.imgur.com/NNw1e.png

Here is the link to the minimum code needed to reproduce my problem (I tried to make it as short as possible):

JavaScript

Advertisement

Answer

Since the player is always in the center of the screen, the rectangles are also always in the center of the screen:

pygame.draw.rect(screen, "red", self.base_player_rect, width=2)
pygame.draw.rect(screen, "yellow", self.rect, width=2)

JavaScript

You can also copy the rectangles, shift them by the offset and draw them in the Camera class:

JavaScript

Also, you need to synchronize the center of the rectangles after moving the player:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement