Pygame. How do I resize a surface and keep all objects within proportionate to the new window size?

前端 未结 3 804
忘了有多久
忘了有多久 2021-01-13 08:14

If I set a pygame window to resizable and then click and drag on the border of the window the window will get larger but nothing blit onto the surface will get larger with i

相关标签:
3条回答
  • 2021-01-13 08:56

    Don't draw on the screen directly, but on another surface. Then scale that other surface to size of the screen and blit it on the screen.

    Here's a simple example:

    import pygame
    from pygame.locals import *
    
    def main():
        pygame.init()
        screen = pygame.display.set_mode((200, 200),HWSURFACE|DOUBLEBUF|RESIZABLE)
        fake_screen = screen.copy()
        pic = pygame.surface.Surface((50, 50))
        pic.fill((255, 100, 200))
    
        while True:
            for event in pygame.event.get():
                if event.type == QUIT: 
                    pygame.display.quit()
                elif event.type == VIDEORESIZE:
                    screen = pygame.display.set_mode(event.size, HWSURFACE|DOUBLEBUF|RESIZABLE)
    
            fake_screen.fill('black')
            fake_screen.blit(pic, (100, 100))
            screen.blit(pygame.transform.scale(fake_screen, screen.get_rect().size), (0, 0))
            pygame.display.flip()
        
    main()    
    
    0 讨论(0)
  • 2021-01-13 08:58

    Recently with pygame2.0 You can use the SCALED flag

    0 讨论(0)
  • 2021-01-13 09:05

    so without the gui initialization (pygame init, and setmode comands ) in the section where you have existing pygame event get()(which your only allowed one time (or just put in ''for inkey in pygame.event.get(VIDEORESIZE):''( if you want to be redundant))(note you can only use ''for inkey in pygame.event.get(VIDEORESIZE):'' one time per loop because it is really a stack that unstacks when you read the event list so you should really use ''for inkey in pygame.event.get():'' snd put all your key recognission statements after this one occurrance of ''for inkey in pygame.event.get():'':

    for inkey in pygame.event.get(VIDEORESIZE)
        if inkey.type == pygame.VIDEORESIZE:
            winwidth,winhight  = inkey.size  # or event.w, event.h
            Window1copy = Window1.copy()# make copy of existing window
            Window1=pygame.display.set_mode((winwidth,winhight),pygame.RESIZABLE) 
            Window1.blit(Window1copy, (0, 0))
            pygame.display.update()
    
    0 讨论(0)
提交回复
热议问题