缩放 Pygame 显示表面上的所有内容 [英] Scale Everything On Pygame Display Surface

查看:80
本文介绍了缩放 Pygame 显示表面上的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在 pygame 中制作一个 2D 像素艺术游戏,正如您所假设的,我所有的精灵纹理看起来都非常小.我想知道是否有一种方法可以在我的游戏中全局缩放所有内容,而不必单独缩放每个精灵或弄乱坐标.每个精灵都会在一个网格上移动:一个单位是 16x16 像素,例如,当我的玩家精灵移动时,它只会向 16 像素的方向移动.

这是我的主要脚本:

导入系统从 pygame.locals 导入 *导入pygame从 game.sprites 导入 Ghostpygame.init()WINDOW_WIDTH = 640WINDOW_HEIGHT = 640DES_WIDTH = 64DES_HEIGHT = 64COL_BG = (46, 48, 55)COL_FG = (235, 229, 206)X = 1000Y = 1000win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))pygame.display.set_caption(穿过门")运行 = 真暂停 = 假# 初始化精灵玩家 = 幽灵()all_sprites = pygame.sprite.Group()all_sprites.add(玩家)时钟 = pygame.time.Clock()在跑步的时候:时钟滴答(30)如果没有暂停:win.fill(COL_BG)all_sprites.update()all_sprites.draw(win)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:系统退出()键 = pygame.key.get_pressed()如果键[pygame.K_RIGHT]:player.go_right()elif 键[pygame.K_LEFT]:player.go_left()elif 键 [pygame.K_UP]:player.go_up()elif 键[pygame.K_DOWN]:player.go_down()pygame.display.flip()pygame.quit()

我确实要加载更多精灵,但我想先解决缩放问题.

解决方案

我想知道是否有一种方法可以在我的游戏中全局扩展所有内容,而不必单独扩展每个精灵 [...]"

不,没有办法.您必须单独缩放每个坐标、每个尺寸和每个表面.PyGame 用于以像素为单位的图像(表面)和形状.无论如何,放大图像会导致模糊、模糊或锯齿状 (Minecraft) 外观.

<块引用>

有没有办法制作一个单独的表面,然后将其放在基础窗口表面的顶部,然后缩放它?

当然可以.

创建一个表面来绘制(win).使用

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.

解决方案

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:

这篇关于缩放 Pygame 显示表面上的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆