可调整大小的文本,在可调整大小的窗口中

时间:2019-03-25 14:58:44

标签: 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")
FPS = pygame.time.Clock()

red_ = (255,0,0)

def background_image_settings(back_end_image):
    back_end_image_set = pygame.image.load(back_end_image)
    screen = pygame.display.set_mode((1200,700), HWSURFACE|DOUBLEBUF|RESIZABLE)
    screen.blit(pygame.transform.scale(back_end_image_set, (1200,700)), (0,0))
    pygame.display.flip()
    try:                                                    #This function is responsible for making the window resizable and creating the background (in my case it is picture)
        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)
                screen.blit(pygame.transform.scale(back_end_image_set,event.dict['size']), (0,0))
                pygame.display.flip()
    except:
        pass

def text_objects(text, font):
    textSurface = font.render(text, True, red_check)    #This function is responsible for making the text colourful and font.   #Have seen it working with TextSurf, TextRect = text_objects(text, largeText)
    return textSurface, textSurface.get_rect()        


background_image_settings(r'/Users/user1/Desktop/Project work/images/backgroundimage2.jpg')

1 个答案:

答案 0 :(得分:0)

screen.blit()的位置在某些公式乘以屏幕的宽度和高度上:

例如,在高尔夫模拟器中,您可以使用如下代码:

window.blit(stroke_label, (SCREEN_WIDTH - (.21+.02*math.floor(math.log10(strokes))) * SCREEN_WIDTH, SCREEN_HEIGHT - .985 * SCREEN_HEIGHT))