gpt4 book ai didi

python - Pygame:重新缩放像素大小

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:18 25 4
gpt4 key购买 nike

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

最佳答案

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

然后将新的 Surface 缩放到屏幕的大小,并将其 blit 到真实的屏幕表面上。

这是一个例子:

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

关于python - Pygame:重新缩放像素大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com