Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

python - Is there a way to resize properly game's screen?

Every time I try to resize the PyGame window, there's no resizing of the game, it keeps like before, and there's just more background.

Any idea about how to resize the game's output with the window (and keeping the ratio, too ), is there a method or do I just need to hand code it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to code it yourself.

Draw your game not directly on the display surface, but on an intermediate one that you can resize to the display surface's size.

Here's a simple example:

import pygame
from pygame.locals import *

def main():
    pygame.init()
    screen = pygame.display.set_mode((400, 300),HWSURFACE|DOUBLEBUF|RESIZABLE)
    game_screen = screen.copy()
    pic = pygame.surface.Surface((50, 50))
    pic.fill('dodgerblue')

    while True:
        for event in pygame.event.get():
            if event.type == QUIT: 
                return
            elif event.type == VIDEORESIZE:
                ratio = game_screen.get_rect().width / game_screen.get_rect().height
                new_size = event.size[0], int(event.size[0] / ratio)
                screen = pygame.display.set_mode(new_size, HWSURFACE|DOUBLEBUF|RESIZABLE)

        game_screen.fill('black')
        game_screen.blit(pic, (100, 100))
        screen.blit(pygame.transform.scale(game_screen, screen.get_rect().size), (0, 0))
        pygame.display.flip()
    
main()    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...