问题描述
使用 pygame,我创建了一个 20x20 像素的窗口并添加了一个 2x2 像素的矩形.当我运行程序时,窗口大小非常小,几乎看不到矩形.如何在保持像素数不变的同时增加窗口大小,即增加像素大小?我知道这个类似的问题,但讨论了一个更复杂的案例.>
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 can I increase the window size whilst keeping the number of pixels constant, i.e. increase the pixel size? I am aware of this similar question, but there a somewhat more complicated case is discussed.
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()
推荐答案
不要直接绘制到屏幕上,而是绘制到另一个Surface
.
Don't draw directly to the screen, but to another Surface
.
然后将新的 Surface
缩放到屏幕的大小并将其 blit 到真实的屏幕表面上.
Then scale that new Surface
to the size of the screen and blit it onto the real screen 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()
这篇关于Pygame:重新调整像素大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!