Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

python - Scale Everything On Pygame Display Surface

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...