How to scale images to screen size in Pygame

后端 未结 2 1954
无人及你
无人及你 2020-12-24 06:15

I was wondering how I would go about scaling the size of images in pygame projects to the resolution of the screen. For example, envisage the following scenario

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 06:49

    If you scale 1600x900 to 1280x720 you have

    scale_x = 1280.0/1600
    scale_y = 720.0/900
    

    Then you can use it to find button size, and button position

    button_width  = 300 * scale_x
    button_height = 300 * scale_y
    
    button_x = 1440 * scale_x
    button_y = 860  * scale_y
    

    If you scale 1280x720 to 1600x900 you have

    scale_x = 1600.0/1280
    scale_y = 900.0/720
    

    and rest is the same.


    I add .0 to value to make float - otherwise scale_x, scale_y will be rounded to integer - in this example to 0 (zero) (Python 2.x)

提交回复
热议问题