Pygame根据矩形大小调整表面和窗口的大小

时间:2018-08-26 01:44:52

标签: python pygame

我有一个显示区域和一个在显示器上发白的表面。表面上是图像,在这种情况下为矩形。将来可能要记住这点。在表面上绘制多条矩形或线条。

我正在尝试(按x键)放大SpriteSpriteSurface上名为SpriteSurface的Rect以及整个显示窗口。尽管调整了大小,但SpriteSurface图像应居中。当前,窗口将放大并且图像保持居中,但是如果取消注释spritesizeX和Y线,则图像会变大,但过大会过快,窗口似乎不会放大到足够大。降低这些值表明,在第一次调整大小之后,居中的偏移量被丢弃了。我觉得解决方案应该相对容易一些,但是很麻烦。任何帮助将不胜感激。

Settings.py

spriteSizeX = 30
spriteSizeY = 30
SpHalfX = int(round(spriteSizeX / 2))
SpHalfY = int(round(spriteSizeY / 2))
multiplyer = 3
windowSizeX = int(round(spriteSizeX * multiplyer))
windowSizeY = int(round(spriteSizeY * multiplyer))
HalfWinX = int(round((windowSizeX / 2) - SpHalfX))
HalfWinY = int(round((windowSizeY / 2) - SpHalfY))

Orange = (238,154,0)
Gold = (255,215,0)
Black = (0,0,0)
Blue = (0,0,255)
Gray = (128,128,128)
DarkGray = (100,100,100)
Green = (0,128,0)
Lime = (0,255,0)
Purple = (128,0,128)
Red = (255,0,0)
Teal = (0,200, 128)
Yellow = (255,255,0)
White = (255,255,255)

run = True

SpriteCapture.py

#!/usr/local/bin/python3.6
import sys, pygame
from pygame.locals import *
from settings import *

pygame.init()
pygame.display.set_caption("Sprite Capture")
Screen = pygame.display.set_mode((windowSizeX, windowSizeY),RESIZABLE)
SpriteSurface = pygame.Surface((spriteSizeX,spriteSizeY))
Sprite = Rect(0,0,spriteSizeX,spriteSizeY)

while run == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if pygame.key.get_pressed()[pygame.K_s]:
            pygame.image.save(SpriteSurface, 'img1.png')
            run = False

        if pygame.key.get_pressed()[pygame.K_q]:
            run = False

        if pygame.key.get_pressed()[pygame.K_z]:
            #spriteSizeX += 10
            #spriteSizeY += 10
            windowSizeX += -10
            windowSizeY += -10
            HalfWinX = int(round(windowSizeX / 2 - SpHalfX))
            HalfWinY = int(round(windowSizeY / 2 - SpHalfY))
            Screen = pygame.display.set_mode((windowSizeX, windowSizeY),RESIZABLE)
            SpriteSurface = pygame.Surface((spriteSizeX,spriteSizeY))

        if pygame.key.get_pressed()[pygame.K_x]:
            #spriteSizeX += 10
            #spriteSizeY += 10
            windowSizeX += 10
            windowSizeY += 10
            HalfWinX = int(round(windowSizeX / 2 - SpHalfX))
            HalfWinY = int(round(windowSizeY / 2 - SpHalfY))
            Screen = pygame.display.set_mode((windowSizeX, windowSizeY),RESIZABLE)
            SpriteSurface = pygame.Surface((spriteSizeX,spriteSizeY))

    Sprite = Sprite = Rect(0,0,spriteSizeX,spriteSizeY)
    Screen.fill(Black)
    pygame.draw.rect(SpriteSurface,Orange,Sprite)
    Screen.blit(SpriteSurface, (HalfWinX,HalfWinY))
    pygame.display.flip()

1 个答案:

答案 0 :(得分:2)

如果要根据屏幕尺寸缩放表面或矩形,可以定义一个zoom_factor变量,可以在按下键时增加该变量,然后使用它来缩放窗口和表面。将其乘以原始屏幕的宽度和高度即可缩放窗口,还可以使用pygame.transform.rotozoom缩放表面,并将zoom_factor作为scale参数传递。

import sys
import pygame
from pygame.locals import *


width = 30
height = 30
multiplyer = 3
window_width = round(width * multiplyer)
window_height = round(height * multiplyer)
zoom_factor = 1
ORANGE = (238,154,0)
BLACK = (0,0,0)

pygame.init()
screen = pygame.display.set_mode((window_width, window_height), RESIZABLE)
screen_rect = screen.get_rect()  # A rect with the size of the screen.
clock = pygame.time.Clock()
# Keep a reference to the original image to preserve the quality.
ORIG_SURFACE = pygame.Surface((width, height))
ORIG_SURFACE.fill(ORANGE)
surface = ORIG_SURFACE
# Center the rect on the screen's center.
rect = surface.get_rect(center=screen_rect.center)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                run = False
            elif event.key == pygame.K_z:
                zoom_factor = round(zoom_factor-.1, 1)
                # Scale the screen.
                w, h = int(window_width*zoom_factor), int(window_height*zoom_factor)
                screen = pygame.display.set_mode((w, h), RESIZABLE)
                screen_rect = screen.get_rect()  # Get a new rect.
                # Scale the ORIG_SURFACE (the original won't be modified).
                surface = pygame.transform.rotozoom(ORIG_SURFACE, 0, zoom_factor)
                rect = surface.get_rect(center=screen_rect.center)  # Get a new rect.
            elif event.key == pygame.K_x:
                zoom_factor = round(zoom_factor+.1, 1)
                w, h = int(window_width*zoom_factor), int(window_height*zoom_factor)
                screen = pygame.display.set_mode((w, h), RESIZABLE)
                screen_rect = screen.get_rect()
                surface = pygame.transform.rotozoom(ORIG_SURFACE, 0, zoom_factor)
                rect = surface.get_rect(center=screen_rect.center)
            # Note that the rect.w/screen_rect.w ratio is not perfectly constant.
            print(zoom_factor, screen_rect.w, rect.w, rect.w/screen_rect.w)

    screen.fill(BLACK)
    screen.blit(surface, rect)  # Blit the surface at the rect.topleft coords.
    pygame.display.flip()
    clock.tick(60)

或者,您可以将所有表面都涂成背景,然后每帧用pygame.transform.rotozoom缩放背景并将其涂成screen。但是,每帧缩放较大的背景表面将不利于性能。