简体   繁体   English

如何更新Pygame中显示的文字?

[英]How to update displayed text in Pygame?

I have a couple of text rects succesfully displayed.我成功显示了几个文本矩形。 However the message that is to be written is assigned to the variable msg .但是,要写入的消息已分配给变量msg If I set my script to change the value of msg after a couple of seconds it is changed but it does not change what my message change.如果我将我的脚本设置为在几秒钟后更改msg的值,它会更改但不会更改我的消息更改的内容。 I have a pygame.display.update() but it still doesnt work.我有一个pygame.display.update()但它仍然不起作用。

HORL.py HORL.py

import pygame, sys, time, random, math
from functions import horl, number, starting, msg
from pygame.locals import QUIT
pygame.init()

clock = pygame.time.Clock()
screenWidth = 800
screenHeight = 600
screen = pygame.display.set_mode((screenWidth , screenHeight))
pygame.display.set_caption('Higher or Lower')
pygame.display.set_icon(pygame.image.load('sprites/icons/icon.ico'))

# fonts
pixel_serif = pygame.font.Font('fonts/pixelserif.ttf', 32)

# name
name = pixel_serif.render('Higher or Lower', True, 0)
namerect = name.get_rect()
namerect.center = (400, 75)

# backgrounds
eight_to_six = pygame.image.load('sprites/background/800x600.png')

# buttons 
higher = pygame.image.load('sprites/icons/highButton.png')
lower = pygame.image.load('sprites/icons/lowButton.png')

# text
message = pixel_serif.render(msg, True, 0)
msgrect = message.get_rect()
msgrect.center = (400, 300)
num = pixel_serif.render(str(starting), True, 0)
numrect = num.get_rect()
numrect.center = (400, 350)

hpressed = False
lpressed = False
running = True
while running:
    clock.tick(60)
    mouse_pos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1 and hButton.collidepoint(mouse_pos) and hpressed == False:
                hpressed = True
                higher = pygame.image.load('sprites/icons/highPressed.png')
                horl('H')
            elif event.button == 1 and lButton.collidepoint(mouse_pos) and lpressed == False:
                lpressed = True
                lower = pygame.image.load('sprites/icons/lowPressed.png')
                horl('L')
        if event.type == pygame.MOUSEBUTTONUP:
            time.sleep(1)
            higher = pygame.image.load('sprites/icons/highButton.png')
            lower = pygame.image.load('sprites/icons/lowButton.png')
            hpressed = False
            lpressed = False

    screen.blit((eight_to_six), (0, 0))
    screen.blit(name, namerect)
    screen.blit(message, msgrect)
    screen.blit(num, numrect)

    hButton = screen.blit(higher,(200,400))
    lButton = screen.blit(lower,(500,400))

    pygame.display.update()

functions.py函数.py

import pygame, time, random
from random import randint, choice

class color:
    white = '255, 255, 255'
    black = '0, 0, 0'
    red = '255, 0, 0'
    green = '0, 255, 0'

wins = 0
loss = 0
rounds = 0

starting = randint(0,13)
number = choice([i for i in range(0,13) if i not in [starting]])
msg = 'The starting number is'

def correct(yn):
    if yn == 'y':
        starting = number
        print('msg y')
        msg = 'Correct! The number is',number

    elif yn == 'n':
        starting = number
        print('msg n')
        msg = 'Incorrect! The Number is',number

def horl(opt):
    global number
    global wins
    global loss
    global msg
    if opt == 'H':
        if number > int(starting):
            correct('y')
            wins = wins + 1
        elif number < int(starting):
            correct('n')
            loss = loss + 1
    elif opt == 'L':
        if number < int(starting):
            correct('y')
            wins = wins + 1
        elif number > int(starting):
            correct('n')
            loss = loss + 1
        else:
            print(color.error + '\nSYNTAX ERROR' + color.normal)```

If you change the msg variable, the Surface that is rendered from that text does not magically update.如果更改msg变量,从该文本呈现的Surface不会神奇地更新。 You have to render the text again.您必须再次渲染文本。 And you must return the new message text from the function:并且您必须从 function 返回新消息文本:

def correct(yn):
    if yn == 'y':
        starting = number
        print('msg y')
        msg = f'Correct! The number is {number}'

    elif yn == 'n':
        starting = number
        print('msg n')
        msg = f'Incorrect! The Number is {number}'
    return msg

def horl(opt):
    global number
    global wins
    global loss
    global msg

    new_msg = ""
    if opt == 'H':
        if number > int(starting):
            new_msg = correct('y')
            wins = wins + 1
        elif number < int(starting):
            new_msg = correct('n')
            loss = loss + 1
    elif opt == 'L':
        if number < int(starting):
            new_msg = correct('y')
            wins = wins + 1
        elif number > int(starting):
            new_msg = correct('n')
            loss = loss + 1
        else:
            print(color.error + '\nSYNTAX ERROR' + color.normal)```

    return new_msg
while running:
    clock.tick(60)
    mouse_pos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1 and hButton.collidepoint(mouse_pos) and hpressed == False:
                hpressed = True
                higher = pygame.image.load('sprites/icons/highPressed.png')
                msg = horl('H')
                message = pixel_serif.render(msg, True, 0)  
            elif event.button == 1 and lButton.collidepoint(mouse_pos) and lpressed == False:
                lpressed = True
                lower = pygame.image.load('sprites/icons/lowPressed.png')
                msg = horl('L')
                message = pixel_serif.render(msg, True, 0)

        # [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM