gpt4 book ai didi

python - 缩放 Pygame 显示表面上的所有内容

转载 作者:行者123 更新时间:2023-12-05 07:10:34 25 4
gpt4 key购买 nike

所以我在 pygame 中制作了一个 2D 像素艺术游戏,正如你所想的那样,我所有的 Sprite 纹理看起来都非常小。我想知道是否有一种方法可以全局放大游戏中的所有内容,而不必单独放大每个 Sprite 或弄乱坐标。每个 Sprite 都会在网格上移动:一个单位是 16x16 像素,例如,当我的玩家 Sprite 移动时,它只会在 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()

我确实有更多 Sprite 要加载,但我想先解决缩放问题。

最佳答案

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()pygame.transform.smoothscale()将其缩放到窗口的大小和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()

最小示例: repl.it/@Rabbid76/PyGame-UpScaleDisplay

关于python - 缩放 Pygame 显示表面上的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61181196/

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