简体   繁体   中英

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. 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. This is because of the design of the underlying SDL library.

Your best bet is to dedicate some corner of the main window as the "HUD" which displays this information.

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.

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. Use this instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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