如何将文字放在特定位置?

时间:2019-03-28 17:00:42

标签: python pygame

我坚持要如何将文本放置在特定坐标上(包括可调整大小的窗口和背景图片)。这是我的代码:

import pygame
from pygame.locals import *
import numpy as np
import matplotlib.pyplot as plt
import argparse
import threading, os, sys, time

pygame.init()
pygame.display.set_caption("AI Battlehip Game")

original_size = (1200,700)

FPS = pygame.time.Clock()

red = (255,0,0)

screen = pygame.display.set_mode(original_size, HWSURFACE|DOUBLEBUF|RESIZABLE)
back_end_image_set = pygame.image.load(r'/Users/User1/Desktop/Project work/images/backgroundimage1.jpg') 
screen.blit(pygame.transform.scale(back_end_image_set, original_size), (0,0))
pygame.display.flip()


myFont = pygame.font.SysFont('freesansbold.ttf', 60)
label = myFont.render('Check 1', 1, (red))

pos_W = (1200 - label.get_width())//2
pos_H = (700 - label.get_height())//2

FPS.tick(60)

try:                                                   
    while True:
        pygame.event.pump()
        event = pygame.event.wait()
        if event.type == QUIT:
            pygame.display.quit()
        elif event.type == VIDEORESIZE:
            screen = pygame.display.set_mode(event.dict['size'], HWSURFACE|DOUBLEBUF|RESIZABLE)
            new_size = event.dict['size']
            screen.blit(pygame.transform.scale(back_end_image_set, new_size), (0,0))
            label_W = pos_W * new_size[0] // original_size[0]
            label_H = pos_H * new_size[1] // original_size[1] 
            screen.blit(pygame.transform.scale(label, (label_W, label_H)), (0,0))
            pygame.display.flip()
except:
    raise

任何帮助将不胜感激。非常感谢你!

1 个答案:

答案 0 :(得分:1)

您必须缩放文本表面(Observable)的位置和大小:

doOnSubscribe

请注意,调整线性大小会沿x和y轴缩放窗口。可以将x和y轴的比例因子应用于曲面上的每个点或向量。矩形的位置以与其大小相同的比例变化:

label