Pygame - " display.set_mode() " creates a smaller window, than requested ( Surfaces are outside of the window )

1k views Asked by At

I´m trying to draw a ship on the bottom-right of the screen, but it´s not appearing on the window! Coordinates seem to be off on X, Y by approximately 50 points. No matter what kind of resolution is set through pygame.display.set_mode(), the window is always smaller than the defined dimensions ( by 50 ).

External FULL HD screen is connected to the laptop through HDMI, but disconnecting it had no effect. Using Windows 10, Python 3.6.2 and Pygame 1.9.3.

Using "centerx", "bottom" to display the ship

Same as above, but substracting both "centerx" and "bottom" by 50.

import sys
import pygame


def main():
    #Initialize the screen.
    pygame.init()
    screen = pygame.display.set_mode( ( 1024, 768 ) )

    screen_rect = screen.get_rect() 

    bg_color = ( 235, 235, 235 )

    # Load the ship surface, get its rect.
    ship_image = pygame.image.load( "images/ship.bmp" ) 
    ship_rect = ship_image.get_rect()

    # TRYING TO POSITION THE SHIP TO THE BOTTOM-RIGHT OF THE SCREEN.
    screen_bottom_right = screen_rect.centerx, screen_rect.bottom

    while True:
        for event in pygame.event.get():
            if ( event == "QUIT" ):
                sys.exit()


        # Redraw the screen.
        screen.fill( bg_color )
        # Blit the ship´s image.
        screen.blit( ship_image, ( screen_bottom_right ) )

        pygame.display.flip()
main()

Tried searching for answers, but none of them had worked / explicitly mentioned this issue. Tutorials, which used the code didn´t substract the X/Y coordinates to obtain exactly positioned image. "0, 0" as rect drawing position works flawlessly. The bottom-right suffers from the above-mentioned issue.

1

There are 1 answers

0
skrx On BEST ANSWER

Pygame blits the image so that its top left corner is positioned at the coordinates that you passed. So by passing the screen bottom as the y-coord you're telling pygame to draw the ship below the screen. To fix this you can assign the bottomright attribute of the screen_rect to the bottomright of the ship_rect and then just blit the image at the ship_rect.

import sys
import pygame


def main():
    pygame.init()
    screen = pygame.display.set_mode((1024, 768))
    screen_rect = screen.get_rect() 

    bg_color = (235, 235, 235)

    ship_image = pygame.Surface((40, 50))
    ship_image.fill((20, 10, 100))
    ship_rect = ship_image.get_rect()
    ship_rect.bottomright = screen_rect.bottomright

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        screen.fill(bg_color)
        screen.blit(ship_image, ship_rect)

        pygame.display.flip()

main()

pygame.Rects have a lot of other attributes that you can use as well, but keep in mind that only the topleft coords are used as the blit position:

x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h