在 Pygame 显示表面上缩放所有内容
Scale Everything On Pygame Display Surface
所以我正在 pygame 中制作一个 2D 像素艺术游戏,正如您所假设的那样,我所有的精灵纹理看起来都非常小。我想知道是否有一种方法可以全局放大游戏中的所有内容,而不必单独放大每个精灵或弄乱坐标。每个精灵都会在一个网格上移动:一个单位是 16x16 像素,当我的玩家精灵移动时,例如,它只会在 16 像素的方向上移动。
这是我的主要脚本:
import sys
from pygame.locals import *
import pygame
from game.sprites import Ghost
pygame.init()
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 640
DES_WIDTH = 64
DES_HEIGHT = 64
COL_BG = (46, 48, 55)
COL_FG = (235, 229, 206)
X = 1000
Y = 1000
win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Through The Doors")
running = True
paused = False
# INITIALIZE SPRITES
player = Ghost()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
clock = pygame.time.Clock()
while running:
clock.tick(30)
if not paused:
win.fill(COL_BG)
all_sprites.update()
all_sprites.draw(win)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
player.go_right()
elif keys[pygame.K_LEFT]:
player.go_left()
elif keys[pygame.K_UP]:
player.go_up()
elif keys[pygame.K_DOWN]:
player.go_down()
pygame.display.flip()
pygame.quit()
我确实有更多精灵要加载,但我想先解决缩放问题。
I'm wondering if there's a way I can globally scale everything up in my game without either having to scale each sprite up individually [...]"
不,没有办法。您必须单独缩放每个坐标、每个尺寸和每个表面。 PyGame 适用于以像素为单位的图像(表面)和形状。无论如何,放大图像将导致模糊、模糊或锯齿状 (Minecraft) 外观。
Is there a way I could make a separate surface and just put that on top of the base window surface, and just scale that?
当然可以。
创建一个 Surface 来绘制 (win
)。使用pygame.transform.scale()
or pygame.transform.smoothscale()
to scale it to the size of the window and blit
它来实际显示Surface(display_win
):
display_win = pygame.display.set_mode((WINDOW_WIDTH*2, WINDOW_HEIGHT*2))
win = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT))
while running:
# [...]
if not paused:
win.fill(COL_BG)
all_sprites.update()
all_sprites.draw(win)
# [...]
scaled_win = pygame.transform.smoothscale(win, display_win.get_size())
# or scaled_win = pygame.transform.scale(win, display_win.get_size())
display_win.blit(scaled_win, (0, 0))
pygame.display.flip()
所以我正在 pygame 中制作一个 2D 像素艺术游戏,正如您所假设的那样,我所有的精灵纹理看起来都非常小。我想知道是否有一种方法可以全局放大游戏中的所有内容,而不必单独放大每个精灵或弄乱坐标。每个精灵都会在一个网格上移动:一个单位是 16x16 像素,当我的玩家精灵移动时,例如,它只会在 16 像素的方向上移动。
这是我的主要脚本:
import sys
from pygame.locals import *
import pygame
from game.sprites import Ghost
pygame.init()
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 640
DES_WIDTH = 64
DES_HEIGHT = 64
COL_BG = (46, 48, 55)
COL_FG = (235, 229, 206)
X = 1000
Y = 1000
win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Through The Doors")
running = True
paused = False
# INITIALIZE SPRITES
player = Ghost()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
clock = pygame.time.Clock()
while running:
clock.tick(30)
if not paused:
win.fill(COL_BG)
all_sprites.update()
all_sprites.draw(win)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
player.go_right()
elif keys[pygame.K_LEFT]:
player.go_left()
elif keys[pygame.K_UP]:
player.go_up()
elif keys[pygame.K_DOWN]:
player.go_down()
pygame.display.flip()
pygame.quit()
我确实有更多精灵要加载,但我想先解决缩放问题。
I'm wondering if there's a way I can globally scale everything up in my game without either having to scale each sprite up individually [...]"
不,没有办法。您必须单独缩放每个坐标、每个尺寸和每个表面。 PyGame 适用于以像素为单位的图像(表面)和形状。无论如何,放大图像将导致模糊、模糊或锯齿状 (Minecraft) 外观。
Is there a way I could make a separate surface and just put that on top of the base window surface, and just scale that?
当然可以。
创建一个 Surface 来绘制 (win
)。使用pygame.transform.scale()
or pygame.transform.smoothscale()
to scale it to the size of the window and blit
它来实际显示Surface(display_win
):
display_win = pygame.display.set_mode((WINDOW_WIDTH*2, WINDOW_HEIGHT*2))
win = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT))
while running:
# [...]
if not paused:
win.fill(COL_BG)
all_sprites.update()
all_sprites.draw(win)
# [...]
scaled_win = pygame.transform.smoothscale(win, display_win.get_size())
# or scaled_win = pygame.transform.scale(win, display_win.get_size())
display_win.blit(scaled_win, (0, 0))
pygame.display.flip()