Pygame - “display.set_mode()”创建一个比请求更小的窗口(Surfaces在窗口之外)

时间:2017-09-02 15:56:59

标签: python pygame pygame-surface

我试图在屏幕的右下方画一艘船,但它没有出现在窗户上!坐标似乎在X,Y上偏离约50点。无论通过pygame.display.set_mode()设置何种分辨率,窗口总是小于定义的尺寸(50)。

外部全高清屏幕通过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()

尝试搜索答案,但他们都没有工作/明确提到这个问题。使用代码的教程没有减去X / Y坐标以获得精确定位的图像。直线绘图位置“0,0”完美无缺。右下角遇到了上述问题。

1 个答案:

答案 0 :(得分:0)

Pygame使图像闪烁,使其左上角位于您传递的坐标处。因此,通过将屏幕底部作为y-coord传递,您可以告诉pygame在屏幕下方绘制船只。要解决此问题,您可以将bottomright的{​​{1}}属性分配给screen_rect的{​​{1}},然后只需在bottomright处显示图像。

ship_rect

pygame.Rects还有许多其他属性也可以使用,但请记住,只有ship_rect坐标用作blit位置:

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()