Python Pygame-缩放图像

Python Pygame-缩放图像,python,image,pygame,transform,scale,Python,Image,Pygame,Transform,Scale,我正在制作一个程序,它将2D数组作为2D贴图拼接。self.tile_size变量用于缩放将成为blit的平铺图像。 我希望Q使它变小,E使它变大。问题是,pygame窗口中的输出不是我想要的。链接中的图像显示了当我运行一个程序并试图放大图像时发生的情况 这是我的密码: import pygame from pygame.locals import * pygame.init() import math, sys, os, random, time from a_star import *

我正在制作一个程序,它将2D数组作为2D贴图拼接。self.tile_size变量用于缩放将成为blit的平铺图像。 我希望Q使它变小,E使它变大。问题是,pygame窗口中的输出不是我想要的。链接中的图像显示了当我运行一个程序并试图放大图像时发生的情况

这是我的密码:

import pygame
from pygame.locals import *
pygame.init()

import math, sys, os, random, time

from a_star import *

tiles_list = [
    pygame.image.load('Floor.png'),
    pygame.image.load('Wall.png'),
    ]

PLAN_W = 20
PLAN_H = 20

PLAN_STATUS = [[True for i in range(PLAN_W)]for j in range(PLAN_H)]

OBSTACLES = [
    [0,0],
    [0,1],
    [0,2]]

obsta(OBSTACLES, PLAN_STATUS) # function imported from a_star that change PLAN_STATUS cells from OBSTACLES to False


class World():
    def __init__(self):
        self.screen_width = 800
        self.screen_height = 600

        self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
        self.clock = pygame.time.Clock()

        self.bg_color = (255,255,255)

        self.tile_size = 48

        self.map_status = PLAN_STATUS

        self.tiles_list = tiles_list

        self.scale_change = 0

        self.run()

    def scale_tiles(self):
        print(self.tile_size)
        self.new_list = []
        for i in self.tiles_list:
            i = pygame.transform.scale(i, (self.tile_size, self.tile_size))
            self.new_list.append(i)
        self.tiles_list = self.new_list

    def render_map(self):
        print(self.tile_size)
        '''
        self.screen.fill(self.bg_color)
        for x in range(PLAN_H):
            for y in range(PLAN_W):
                if self.map_status[y][x]:
                    tile = self.tiles_list[0]
                elif not self.map_status[y][x]:
                    tile = self.tiles_list[1]

                self.screen.blit(tile, (x*self.tile_size, y*self.tile_size))
'''

        self.screen.blit(self.tiles_list[0], (400, 300))

    def run(self):
        while True:


            self.events = pygame.event.get()
            self.keys = pygame.key.get_pressed()
            self.mouse_pos = pygame.mouse.get_pos()
            self.mouse_but = pygame.mouse.get_pressed()

            #UPDATE
            self.screen.fill(self.bg_color)

            #self.scale_tiles()
            self.render_map()

            pygame.display.flip()

            #EVENTS
            if self.keys[ord('e')] == 1:
                self.tile_size += 1
                self.scale_tiles()
            if self.keys[ord('q')] == 1:
                self.tile_size -= 1
                self.scale_tiles()


            '''for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        print('q')
                        self.scale_change = -1
                    if event.key == pygame.K_e:
                        print('e')
                        self.scale_change = 1
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_q or event.key == pygame.K_e:
                        print('stopped key')
                        self.scale_change = 0'''



            self.clock.tick(50)

if __name__=="__main__":
    main = World()
我的代码中哪里有问题? 谢谢你的帮助


使用原始全局
瓷砖列表中的图像/曲面创建
新的瓷砖列表
而不是
self.tiles列表
中已缩放的图像,否则每次缩放都会增加不精确性


对于tiles\u列表中的i:
而不是
对于self中的i。tiles\u列表:
使用原始全局
tiles\u列表中的图像/曲面
来创建
新列表
而不是
self.tiles\u列表
中已缩放的图像,否则每次缩放都会增加不精确性


对于tiles中的i\u列表:
而不是
对于self中的i。tiles\u列表:

什么是星?请阅读。有些东西,如
obsta
PLAN\u STATUS
,与示例无关。另外,不要使用星号导入(带有星号
*
),因为它们会使代码更难阅读。您可以保留pygame.locals import*
中的
。@skrx谢谢,下次我会记住的;)@CristiFati a_star是我的模块,它使用*算法在2D阵列中找到两点之间的最短路径。我想象。这就是问题不好的原因,关于。不管怎样,很高兴有人回答!什么是星星?请看下面的句子。有些东西,如
obsta
PLAN\u STATUS
,与示例无关。另外,不要使用星号导入(带有星号
*
),因为它们会使代码更难阅读。您可以保留pygame.locals import*
中的
。@skrx谢谢,下次我会记住的;)@CristiFati a_star是我的模块,它使用*算法在2D阵列中找到两点之间的最短路径。我想象。这就是问题不好的原因,关于。不管怎样,很高兴有人回答!我想不出来,像那样简单的事情会导致它。非常感谢muchI不会有主意的,像那样简单的事情可能会导致它。非常感谢你