Pygame - 缩放图像

时间:2017-08-31 17:11:00

标签: python image pygame transform scale

我正在制作程序,将2D数组作为2D地图拼贴。 self.tile_size 变量用于缩放将为blit的图块图像。 我希望 Q 使其更小,而 E 使其更大。问题是,pygame窗口中的输出不是我想要的。链接中的图像显示了发生了什么,当我运行程序并试图使图像更大。 Image of output

这是我的代码:

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()

我的代码中的问题在哪里? 谢谢你的帮助

Floor.png

1 个答案:

答案 0 :(得分:0)

使用原始全局tiles_list中的图像/曲面创建new_list,而不是self.tiles_list中已缩放的图像,否则每次缩放都会增加不精确度。< / p>

for i in tiles_list:不是for i in self.tiles_list: