微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么文本会缩放?

如何解决为什么文本会缩放?

我试图在玩家角色旁边移动一个文本,但它的缩放比例很奇怪...... 这是玩家角色的图片,旁边有文字“test”:

Text in game

它应该是某种抗锯齿,因为它在移动文本的代码中是关闭的:

import pygame,sys
from pygame.locals import *


class MovingText(): 
 
    def __init__(self,text,font_size,color,surface,target,off_1,off_2): 
        self.font       = pygame.font.SysFont(None,font_size)
        self.textobj    = self.font.render(text,color) 
        self.textrect   = self.textobj.get_rect()
        self.surface    = surface  
        self.target     = target 
        self.offset     = (off_1,off_2)
        self.textrect.center = self.target_pos() 
         
    def update(self): 
        self.textrect.center = self.target_pos() 
        self.surface.blit(self.textobj,self.textrect.center)
         
    def target_pos(self): 
        pos = self.target.rect.center 
        return pos[0] + self.offset[1],pos[1] + self.offset[0]

我认为导致问题的原因是文本被创建时,这里:

self.follow_text = movingtext.MovingText('test',10,(255,255,255),self.display,self.player,5)

如果你认为这是其他的东西,你可以自由地检查其余的代码,不要真的这么认为,但我已经被证明错了一两次:

import pygame,sys
from pygame.locals import *
from pygame.mouse import get_pos

import time

from utils import button,constants,movingtext
from entities import player,entity
import game


class Game():

    def __init__(self,map_number):
            pygame.init()
            self.clock = pygame.time.Clock()
            self.screen = pygame.display.set_mode((pygame.display.Info().current_w,pygame.display.Info().current_h),pygame.RESIZABLE)
            self.display = pygame.Surface((300,300))
            self.font_small = pygame.font.SysFont(None,20)
            self.font_medium = pygame.font.SysFont(None,32)
            self.test_bg = pygame.image.load('images/wp.png')
            self.pause = False

            self.timer = 0
            self.game_called = time.time()

            self.flag_mover = False

            self.map_number = map_number

            f = open('maps/map'+self.map_number+'.txt')
            self.map_data = [[int(c) for c in row] for row in f.read().split('\n')]
            f.close()

            #Tile list -----
            self.spawn_img = pygame.image.load('images/spawn.png').convert()
            self.spawn_img.set_colorkey((0,0))

            self.goal_img = pygame.image.load('images/goal.png').convert()
            self.goal_img.set_colorkey((0,0))


            self.key_img = pygame.image.load('images/key.png').convert()
            self.key_img.set_colorkey((0,0))

            self.lava_img = pygame.image.load('images/lava.png').convert()
            self.lava_img.set_colorkey((0,0))

            self.grass_img = pygame.image.load('images/grass2.png').convert()
            self.grass_img.set_colorkey((0,0))
            
            #Player
            for y,row in enumerate(self.map_data):
                for x,tile in enumerate(row):
                    if tile == 1:
                        self.player = player.Player(self.display,(150 + (x+1) * 10 - y * 10,100 + x * 5 + (y-0.5) * 5),self.map_data)

            #goal flag
            for y,tile in enumerate(row):
                    if tile == 2:
                        self.goal_flag = entity.Entity(self.display,100 + x * 5 + (y-1) * 5),'images/goal_flag.png')

            #points
            self.point_list = []
            for y,tile in enumerate(row):
                    if tile == 3:
                        self.points = entity.Entity(self.display,'images/point.png')
                        self.point_list.append(self.points)

            self.running = True
            self.click = False

    
    def drawText(self,font,x,y):
        textobj = font.render(text,1,color)
        textrect = textobj.get_rect()
        textrect.topleft = (x,y)
        surface.blit(textobj,textrect)


    def gameLoop(self):
        
        while self.running:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                    
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        if self.pause == False:
                            self.pause = True
                        else:
                            self.pause = False

            if self.pause == False:
        
                self.screen.blit(pygame.transform.scale(self.display,self.screen.get_size()),(0,0))
                self.display.fill(0) #clears the scree
                self.drawText('game',self.font_small,self.screen,20,20)
                # self.drawText('time: '+str(int(self.timer/1000)),self.player.rect[0],self.player.rect[1]) 

                self.follow_text = movingtext.MovingText('test',5)
                # self.follow_text = movingtext.MovingText()

                #Draws the map
                for y,row in enumerate(self.map_data):
                    for x,tile in enumerate(row):
                        if tile == 0:
                            self.display.blit(self.lava_img,(150 + x * 10 - y * 10,100 + x * 5 + y * 5))
                        if tile == 1:
                            self.display.blit(self.spawn_img,100 + x * 5 + y * 5))
                        if tile == 2:
                            self.display.blit(self.goal_img,100 + x * 5 + y * 5))
                        if tile == 3:
                            self.display.blit(self.key_img,100 + x * 5 + y * 5))    
                        if tile == 4:
                            self.display.blit(self.grass_img,100 + x * 5 + y * 5))

                #collision detection between entities
                if self.goal_flag.rect[0] == self.player.rect[0] and self.goal_flag.rect[1] == self.player.rect[1] - 2:
                    self.flag_mover = True
                if self.flag_mover == True:
                    self.goal_flag.rect[1] += -0.1
                

                #point collision
                for point_collision in self.point_list:
                    if point_collision.rect[0] == self.player.rect[0] and point_collision.rect[1] == self.player.rect[1]:
                        self.point_list.remove(point_collision)
                        
        
                #update
                for points in self.point_list:
                    points.update()
                self.goal_flag.update()
                self.player.update()
                self.follow_text.update()

            else:
                self.screen.blit(pygame.transform.scale(self.display,0))
                self.drawText('game',20)
                self.drawText('PAUSED',self.font_medium,pygame.display.Info().current_w/2-50,pygame.display.Info().current_h/2)
                
                for y,100 + x * 5 + y * 5))       
                        if tile == 4:
                            self.display.blit(self.grass_img,100 + x * 5 + y * 5))
              

            pygame.display.update()
            self.clock.tick(60)


解决方法

当您在表面上绘制文本并将表面缩放到窗口大小时,文本会被缩放。您必须直接在显示面上绘制文本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。