问题
So I'm making a 2D pixel art game in pygame and as you could assume, all my sprite textures appear very small. 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 or messing up the coordinates. Every sprite will move on a grid: one unit is 16x16 pixels and when my player sprite moves, for example, it will just move over in a direction 16 pixels.
Here's my main script:
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 do have more sprites I am going to load in, but I would like to resolve the scaling issue first.
回答1:
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 [...]"
No there is no way. You have to scale each coordinate, each size and each surface individually. PyGame is made for images (Surfaces) and shapes in pixel units. Anyway up scaling an image will result in either a fuzzy, blurred or jagged (Minecraft) appearance.
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?
Yes of course.
Create a Surface to draw on (win
). Use pygame.transform.scale() or pygame.transform.smoothscale() to scale it to the size of the window and blit it to the actual display 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()
Minimal example: repl.it/@Rabbid76/PyGame-UpScaleDisplay
来源:https://stackoverflow.com/questions/64341589/scale-everything-on-pygame-display-surface