简体   繁体   English

如何在pygame窗口上显示值?

[英]How to display value on pygame window?

I'm trying to get my game to open a separate window that displays the players score after the quit out of the main game. 我试图让我的游戏打开一个单独的窗口,显示退出主游戏后的玩家得分。 I can't figure out how to have the text display the value of s + s2. 我无法弄清楚如何让文本显示s + s2的值。 So far i have: 到目前为止,我有:

import pygame, sys
from pygame.locals import *
pygame.init()
import time

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
darkBlue = (0,0,128)
white = (255,255,255)
black = (0,0,0)
pink = (255,200,200)


background = pygame.image.load('background.jpg')
screen = pygame.display.set_mode((640,521),0,32)
x,y = 285, 430
m_x, m_y = 0,0
t = 0
u = 0
q = 0
e = 0
pygame.display.set_caption('Game')
s = 0
s2 = 0
while 1:
    screen.blit(background, (0,0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.display.set_mode((200,200),0,32)
            pygame.display.set_caption('Score')
            font = pygame.font.Font(None, 100)
            text = font.render('s + s2'), 1, white)
            screen.blit(text, (0,0))
            pygame.display.update()
            time.sleep(5)
            pygame.quit()
            sys.exit()


        if event.type == KEYDOWN:
            if event.key == K_a:
                m_x = -3
                s+=1
                print (s)
            elif event.key == K_d:
                m_x = +3
                s2+=1
                print (s2)
        if event.type == KEYUP:
            if event.key == K_a:
                m_x = 0
            elif event.key == K_d:
                m_x = 0

    x+= m_x
    y+= m_y      

    ball = pygame.sprite.Sprite()
    ball.image = pygame.image.load('red_ball.png').convert()
    ball.rect = ball.image.get_rect()
    ball.image.set_colorkey((white))
    screen.blit(ball.image,(x,y))
    if x > 640:
        x = 0
    if x < 0:
        x = 640


    g_ball = pygame.sprite.Sprite()
    g_ball.image = pygame.image.load('green_ball.png').convert()
    g_ball.rect = g_ball.image.get_rect()
    g_ball.image.set_colorkey(white)
    screen.blit(g_ball.image,(50,t))
    t+=5
    if t > 521:
        t = 0

    g_ball2 = pygame.sprite.Sprite()
    g_ball2.image = pygame.image.load('green_ball.png').convert()
    g_ball.rect = g_ball.image.get_rect()
    g_ball2.image.set_colorkey(white)
    screen.blit(g_ball2.image,(350,u))
    u+=5
    if u > 521:
        u = -50

    g_ball3 = pygame.sprite.Sprite()
    g_ball3.image = pygame.image.load('green_ball.png').convert()
    g_ball3.rect = g_ball3.image.get_rect()
    g_ball3.image.set_colorkey(white)
    screen.blit(g_ball3.image,(500,q))
    q+=5
    if q > 521:
        q = -75

    g_ball4 = pygame.sprite.Sprite()
    g_ball4.image = pygame.image.load('green_ball.png').convert()
    g_ball4.rect = g_ball4.image.get_rect()
    g_ball4.image.set_colorkey(white)
    screen.blit(g_ball4.image,(200,e))
    e+=5
    if e > 521:
        e = -100


    pygame.display.update()

Pygame cannot open multiple windows at the same time. Pygame无法同时打开多个窗口。 This is because of the design of the underlying SDL library. 这是因为底层SDL库的设计。

Your best bet is to dedicate some corner of the main window as the "HUD" which displays this information. 您最好的选择是将主窗口的某个角落作为显示此信息的“HUD”。

Development versions of SDL do support multiple windows (SDL 1.3) and there are development versions of Pygame you could look into, if multiple windows is still the ideal solution for you. SDL的开发版本支持多个窗口(SDL 1.3),如果多个窗口仍然是您的理想解决方案,您可以查看Pygame的开发版本。

You also have a problem with the code for drawing text. 您还有绘制文本代码的问题。 (Aside from the mismatched parenthesis.) (除了不匹配的括号外。)

font.render('s + s2'), 1, white)

This will show a literal "s + s2", what you need to do is add these two values and convert that to a string. 这将显示文字“s + s2”,您需要做的是添加这两个值并将其转换为字符串。 Use this instead. 请改用它。

font.render(str(s + s2), 1, white)

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

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