I’m using pygame to show two images: I’ve maneged to resize the images according to the screen size of my user. But I’d like to standardized also the x and the y coordination position of my images. The images should always be in the middle according to the y axis and have the a little bit of pad to stay close to the border of the monitor like this no matter what resolution my user has:
This are the values that I’d like to standardized
JavaScript
x
4
1
WIN.blit(FIRST_IMG, (100, 280))
2
3
WIN.blit(SECOND_IMAGE, (1350, 280))
4
This is my code right now:
JavaScript
1
52
52
1
import pygame
2
import os
3
4
5
WIN = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
6
7
WHITE = (255, 255, 255)
8
9
FPS = 60
10
11
SCREEN_INFO = pygame.display.Info()
12
13
IMAGE_WIDTH, IMG_HEIGHT = SCREEN_INFO.current_w // 5, SCREEN_INFO.current_h // 3
14
15
FIRST_IMG = pygame.image.load(os.path.join("Assets", "7.png"))
16
FIRST_IMG = pygame.transform.scale(FIRST_IMG, (IMAGE_WIDTH, IMG_HEIGHT))
17
18
SECOND_IMAGE = pygame.image.load(os.path.join("Assets", "8.png"))
19
SECOND_IMAGE = pygame.transform.scale(SECOND_IMAGE, (IMAGE_WIDTH, IMG_HEIGHT))
20
21
22
def draw_window():
23
WIN.fill(WHITE)
24
25
WIN.blit(FIRST_IMG, (100, 280))
26
27
WIN.blit(SECOND_IMAGE, (1350, 280))
28
29
pygame.display.update()
30
31
32
def main():
33
clock = pygame.time.Clock()
34
run = True
35
36
while run:
37
clock.tick(FPS)
38
for event in pygame.event.get():
39
if event.type == pygame.QUIT:
40
run = False
41
elif event.type == pygame.KEYDOWN:
42
if event.key == pygame.K_ESCAPE:
43
run = False
44
45
draw_window()
46
47
pygame.quit()
48
49
50
if __name__ == "__main__":
51
main()
52
Advertisement
Answer
Get the bounding rectangle of the image and set the centery
and left
or right of the rectangle depending on the size of WIN
. Use the rectangles to blit
the images:
JavaScript
1
13
13
1
def draw_window():
2
width, height = WIN.get_size()
3
4
WIN.fill(WHITE)
5
6
first_rect = FIRST_IMG.get_rect(midleft = (100, height // 2))
7
WIN.blit(FIRST_IMG, first_rect)
8
9
second_rect = FIRST_IMG.get_rect(midright = (width - 100, height // 2))
10
WIN.blit(SECOND_IMAGE, second_rect)
11
12
pygame.display.update()
13