Pygame - “ display.set_mode() ”创建比请求更小的 window(表面在 window 之外)

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

我想在屏幕右下角画一艘船,但它没有出现在 window 上!坐标似乎在 X、Y 上偏离了大约 50 点。无论通过pygame.display.set_mode()设置什么样的分辨率,window总是小于定义的尺寸(50)。

外接FULL HD屏幕通过HDMI连接到笔记本电脑,但断开连接没有任何效果。使用 Windows 10、Python 3.6.2 和 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()

尝试搜索答案,但其中 none 有效/明确提到了这个问题。使用代码的教程没有减去 X/Y 坐标以获得精确定位的图像。 “0, 0”作为矩形绘图位置完美无缺。右下角存在上述问题

Pygame blit 图像,使其左上角位于您传递的坐标处。因此,通过将屏幕底部作为 y 坐标传递,您告诉 pygame 在屏幕下方绘制飞船。要解决此问题,您可以将 screen_rectbottomright 属性分配给 ship_rectbottomright,然后只在 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 还有很多其他属性可以使用,但请记住只有 topleft 坐标用作 blit 位置:

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