【问题标题】:How to update displayed text in Pygame?如何更新 Pygame 中显示的文本?
【发布时间】:2023-01-09 23:55:10
【问题描述】:

我成功显示了几个文本矩形。但是,要写入的消息已分配给变量msg。如果我将我的脚本设置为在几秒钟后更改 msg 的值,它会更改但不会更改我的消息更改的内容。我有一个pygame.display.update(),但它仍然不起作用。

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

函数.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)```

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    如果更改 msg 变量,则该变量呈现的 Surface 文本不会神奇地更新。您必须再次呈现文本。并且您必须从函数返回新的消息文本:

    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 + '
    SYNTAX 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)
    
            # [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2020-02-29
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多