首页 > 解决方案 > 你如何在 Pygame 中缩放图像?

问题描述

我有一张饼干的照片,它对窗户来说太大了。我尝试过使用,pygame.transform.scale但无论我尝试哪种尺寸,它都不会按比例放大。这是代码:

import pygame

pygame.init()

window_size = (600, 400)
stage = 0
running = True

font32 = pygame.font.Font('freesansbold.ttf', 32)
font48 = pygame.font.Font('freesansbold.ttf', 48)

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)
cookieColor = (157, 126, 103)

Window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Cookie Clicker")
windowCenter = (Window.get_rect().centerx, Window.get_rect().centery)

cookieImage = pygame.image.load(r'C:\Users\rogne\Cookie Clicker\Cookie.png')
pygame.transform.scale(cookieImage, (400, 400))

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if playRect.collidepoint(event.pos) and stage == 0:
                stage = 1
            
    if stage == 0:
        Window.fill(red)

        playbuttontext = font32.render('Play', True, white, red)
        playRect = playbuttontext.get_rect()
        playRect.centerx = Window.get_rect().centerx
        playRect.centery = Window.get_rect().centery + 100

        title_text = font48.render('Cookie Clicker', True, white, red)
        titleRect = title_text.get_rect()
        titleRect.centerx = Window.get_rect().centerx
        titleRect.centery = Window.get_rect().centery - 100

        Window.blit(playbuttontext, playRect)
        Window.blit(title_text, titleRect)

    if stage == 1:
        Window.fill(black)
        Window.blit(cookieImage, (200, 200))

    pygame.display.update()

pygame.quit()

标签: pythonpygame

解决方案


推荐阅读