PyGame中的问题通过缩放背景图片

时间:2017-08-03 09:16:31

标签: image python-3.x pygame zoom

我试图制作一个在背景上有世界地图的游戏。在这个游戏中,用户可以通过单击鼠标并移动它来缩放图片或拖动图片。首先,我尝试添加缩放功能,但是当用户单击鼠标以缩放图片时会发生奇怪的事情。图片分为正方形。这是图片和我的代码。 这是图片:[1]:https://i.stack.imgur.com/dQhfX.jpg [1]:

这就是:https://i.stack.imgur.com/qdlqt.jpg

import pygame
import time
from pygame.locals import *
clock = pygame.time.Clock()
pygame.init()
WHITE = (255, 255, 255)
WIDTH = 1280
HEIGHT = 800
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
img = pygame.image.load("/Users/Esat/Desktop/map.jpg")
img = pygame.transform.scale(img,(WIDTH,HEIGHT))

while True:
        events = pygame.event.get()
        button_down = pygame.mouse.get_pressed()
        if button_down == (1,0,0):
            print("Clicked")
            WIDTH = WIDTH+10
            HEIGHT = HEIGHT+10
            img = pygame.transform.scale(img, (WIDTH, HEIGHT))
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        windowSurface.fill(WHITE)
        windowSurface.blit(img, (0, 0)) 
        clock.tick(60)
        pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

当你不断改变图像时,事情可以很快得到时髦。我发现最好保留原件的副本,并根据该原件进行所有比例更改。以下是您的代码修复:

import pygame
import time
from pygame.locals import *
clock = pygame.time.Clock()
pygame.init()
WHITE = (255, 255, 255)
WIDTH = 1280
HEIGHT = 800
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
originalImg = pygame.image.load("map.jpg")
img = pygame.transform.scale(originalImg,(WIDTH,HEIGHT))

while True:
        events = pygame.event.get()
        button_down = pygame.mouse.get_pressed()
        if button_down == (1,0,0):
            #print("Clicked")
            WIDTH = WIDTH+10
            HEIGHT = HEIGHT+10
            img = pygame.transform.scale(originalImg, (WIDTH, HEIGHT))
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        windowSurface.fill(WHITE)
        windowSurface.blit(img, (0, 0)) 
        clock.tick(60)
        pygame.display.flip()