我正试图让我的游戏打开一个单独的窗口,显示退出主游戏后的玩家得分。我无法弄清楚如何让文本显示s + s2的值。到目前为止我有:
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()
答案 0 :(得分:2)
Pygame无法同时打开多个窗口。这是因为底层SDL库的设计。
您最好的选择是将主窗口的某个角落用作显示此信息的“HUD”。
SDL的开发版本支持多个窗口(SDL 1.3),如果多个窗口仍然是您理想的解决方案,您可以查看Pygame的开发版本。
绘图文字的代码也有问题。 (除了不匹配的括号外。)
font.render('s + s2'), 1, white)
这将显示文字“s + s2”,您需要做的是添加这两个值并将其转换为字符串。请改用它。
font.render(str(s + s2), 1, white)