Pygame: Rescale pixel size

后端 未结 1 1876
忘了有多久
忘了有多久 2021-01-13 18:58

With pygame, I created a 20x20 pixel window and added a 2x2 pixel rectangle. When I run the program, the window size is super small and I can barely see the rectangle. How c

相关标签:
1条回答
  • 2021-01-13 19:21

    Don't draw directly to the screen, but to another Surface.

    Then scale that new Surface to the size of the screen and blit it onto the real screen surface.

    Here's an example:

    import pygame
    screen_width, screen_height = 20, 20
    
    scaling_factor = 6
    
    x, y = 10, 10
    rect_width, rect_height = 2, 2
    vel = 2
    black = (0, 0, 0)
    white = (255, 255, 255)
    pygame.init()
    win = pygame.display.set_mode((screen_width*scaling_factor, screen_height*scaling_factor))
    
    screen = pygame.Surface((screen_width, screen_height))
    
    run = True
    while run:
        pygame.time.delay(100)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        screen.fill(black)
        pygame.draw.rect(screen, white, (x, y, rect_width, rect_height))
    
        win.blit(pygame.transform.scale(screen, win.get_rect().size), (0, 0))
        pygame.display.update()
    pygame.quit()
    
    0 讨论(0)
提交回复
热议问题