Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Pygame camera problem when on full screen

$
0
0

I'm writting a simple 2d game with pygame for learning and I'm having problems with the camera. It works precfectly fine, the player is centered and followed as expected.

The problems appears when I go to full screen: the canvas changes size successfully and the player stays in the same position on the map, but the camera seems to bug because the player appears in the top-left corner of the camera...

Here is the code of the camera:

from pygame import Rectfrom settings.constants import TILES_SIZE, WALK_SPEEDclass Camera:"""Follows the focused character to keep it in the center of the display"""    def __init__(self, w, h, character):        self.__CORRECTION = TILES_SIZE / 2        self.__rect = Rect(*character.coord, w, h)        self.__focus = character        self.__fancy_displacing = False    @property    def x(self):"""Returns the position in pixels of the camera in the X coord."""        return self.__rect.x + self.__CORRECTION    @property    def y(self):"""Returns the position in pixels of the camera in the Y coord."""        return self.__rect.y + self.__CORRECTION    @property    def size(self):        return self.__rect.size    @size.setter    def size(self, new_value):        self.__rect.size = new_value    def update(self) -> None:"""Moves the camera to keep the focus in the center of the display"""        if self.__fancy_displacing:            self.__fancy_displacement()        else:            self.__rect.center = self.__focus.coord    def __fancy_displacement(self) -> None:"""When it is needed to move the camera for a long distance, without teleporting it. like a cinematic"""        finished = [False, False]        if self.__focus.coord.x > self.__rect.center[0] + WALK_SPEED:            self.__rect.x += WALK_SPEED        elif self.__focus.coord.x < self.__rect.center[0] - WALK_SPEED:            self.__rect.x -= WALK_SPEED        else:            finished[0] = True        if self.__focus.coord.y > self.__rect.center[1] + WALK_SPEED:            self.__rect.y += WALK_SPEED        elif self.__focus.coord.y < self.__rect.center[1] - WALK_SPEED:            self.__rect.y -= WALK_SPEED        else:            finished[1] = True        if finished[0] and finished[1]:            self.__fancy_displacing = False    def focus(self, character, with_fancy_displacing=False) -> None:"""Focus the camera in a new character"""        self.__focus = character        self.__fancy_displacing = with_fancy_displacing

The full screen event is started from here, in the Game class...

    def __key_down(self, key):"""        Handle the key press event and perform corresponding actions based on the input key.        :param key: The key that was pressed        :return: None"""        if key == EXIT_KEY:            logging.info('User is quitting')            self.stop_loop()        elif key == ACTION_KEY:            # for player interaction, unused for now            pass        if key == MENU_KEY:            self.__status = PAUSED_ST if self.__status != PAUSED_ST else PLAYING_ST        if key == FULL_SCREEN:            new_size = self.__display.fullscreen()            # FIXME: camera glitches when size is changed...            self.__camera.size = self.__display.size

¿Can you please tell me what is going on here? I really cannot figure out what happened... Everything seems OK to me...

I tried to implement a full screen function, optionally. When you press certain key, the fame should go full screen inmediately. Actually it does that perfectly, but the camera glitches....

Not full screen everything is OK! :D

now in full screen, the camera is a mess :(


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>