Pygame:重新调整像素大小

时间:2019-01-04 13:59:01

标签: python pygame

使用pygame,我创建了一个20x20像素的窗口并添加了一个2x2像素的矩形。 当我运行该程序时,窗口大小非常小,几乎看不到矩形。如何在保持像素数不变的同时增加窗口大小,即增加像素大小?我知道this similar question,但这里讨论的情况有些复杂。

import pygame
screen_width, screen_height = 20, 20
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, screen_height))
run = True
while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    win.fill(black)
    pygame.draw.rect(win, white, (x, y, rect_width, rect_height))
    pygame.display.update()
pygame.quit()

1 个答案:

答案 0 :(得分:3)

不要直接绘制到屏幕上,而是直接绘制到另一个Surface上。

然后将新的Surface缩放到屏幕尺寸,然后将其放大到实际屏幕表面上。

这是一个例子:

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()