Я скопировал и вставил свой код ниже для начала головоломки в Pygame, и я пытался изменить размер окна. Проблема в том, что когда я делаю окно меньше или больше, плитки не изменяют размеры, чтобы масштабироваться. Вместо этого они просто либо отсекаются (или, если я увеличу окно, они просто будут окружены пробелами). Я не пытался масштабировать его, так как понятия не имею, как мне поступить.
import pygame
from PIL import Image
import sys
#Constants
Black = (0,0,0)
def CreatePuzzle():
#Loading the image and calculating the tile sizes
ImageFilename = "dduck800_600.jpg"
PuzzleImage = Image.open(ImageFilename)
ImageSize = PuzzleImage.size
NoRows = 5
NoCols = 5
TileWidth = int(PuzzleImage.width/ NoRows)
TileHeight = int(PuzzleImage.height/ NoCols)
BlankTile = (NoCols-1,NoRows-1)
#Creating borders for the tiles
HorBorder = pygame.Surface((TileWidth,1))
VertBorder = pygame.Surface((1, TileHeight))
HorBorder.fill(Black)
VertBorder.fill(Black)
#Dividing image into tiles
Tiles = {}
PuzzleImage = pygame.image.load(ImageFilename)
for Col in range(NoCols):
for Row in range(NoRows):
Tile = PuzzleImage.subsurface(Col*TileWidth, Row*TileHeight, TileWidth, TileHeight)
Tiles[(Col,Row)] = Tile
if (Col,Row) != BlankTile:
Tile.blit(HorBorder, (0,0))
Tile.blit(HorBorder, (0,TileHeight-1))
Tile.blit(VertBorder, (0,0))
Tile.blit(VertBorder,(0,TileWidth-1))
Tiles[BlankTile].fill(Black)
#Display puzzle board to the user
pygame.init()
Display = pygame.display.set_mode(ImageSize,pygame.RESIZABLE)
pygame.display.set_caption("Picture Puzzle V1")
Display.blit (PuzzleImage, (0, 0))
pygame.display.flip()
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.K_SPACE:
print("Shuffling")
#shuffle
elif event.type == pygame.VIDEORESIZE:
OldDisplay = Display
Display = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
# On the next line, if only part of the window
# needs to be copied, there's some other options.
Display.blit(OldDisplay, (0,0))
del OldDisplay
#Main
CreatePuzzle()
Раздел кода, который позволяет изменять размер окна, равен
elif event.type == pygame.VIDEORESIZE:
OldDisplay = Display
Display = pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
# On the next line, if only part of the window
# needs to be copied, there's some other options.
Display.blit(OldDisplay, (0,0))
del OldDisplay