PyGame - How to get an image in the center of the screen

后端 未结 2 540
深忆病人
深忆病人 2021-01-29 00:25
# MENU
def gameMenu():
intro = True
while(intro == True):
    win.fill((0,0,0))
    iPATH = f\"{PATH}/assets/textures/titleScreen/buttons\"
    win.blit(pygame.image.loa         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 01:00

    You made a simple mathematical mistake. If your screen size is 800 and you divide it by 2, you obtain 400. Therefore, with an image width of, say, 100 pixels, you'll be covering pixels 400 to 500, thus having an skew towards the right.

    If you want to truly center your image, you need to account for half its width. Therefore, you calculation to obtain the top left x coordinate should look like:

    x_centered = screen_width / 2 - image_width / 2
    y_centered = screen_height / 2 - image_height / 2 #similarly..
    

    Adapting to the specifics of your current project should be rather straightforward.

提交回复
热议问题