在pygame中更新数字

问题描述 投票:3回答:1

我有一个相当简单的问题。我想每秒更新一次值,而我的代码执行该操作。尽管顺序应该正确,但更新的数字确实显示在表面的后面。

如何显示在顶部?

((如果我不进行screen.fill,我画了一个圆以查看它是在圆的后面还是在圆的前面)

此外,如果不将这些数字相互叠加,我将不胜感激,但是一旦解决了其他问题,我就能解决这个问题。

代码:

import pygame
from pygame.locals import *

pygame.init()
display_width = 1600
display_height = 800

color = 23,52,85
color2 = 43,200,8

screen = pygame.display.set_mode((display_width,display_height))

wood = 0

myFont = pygame.font.SysFont('Arial', 25)
someEvent = pygame.key.get_pressed()

MOVEEVENT = pygame.USEREVENT+1
t = 1000
pygame.time.set_timer(MOVEEVENT,t)

def increaseWood():
  global wood
  wood+=3
  print("The wood was increased")
  return wood

def drawEverySecond():
  text = "Wood: " + str(increaseWood())
  label = myFont.render(text,1,color2)
  screen.blit(label, (1000,300))
  print('Current Wood:', wood)

while True:
  pygame.display.update()
  screen.fill(color)
  pygame.draw.circle(screen, (220,40,100),(1000,300), 20,4 )
  #drawEverySecond()

  for event in pygame.event.get():
      #print(event)
      if event.type == pygame.QUIT:
          pygame.display.quit()
          quit()
      if event.type == MOUSEBUTTONDOWN:
          print("Mouse was pressed!")
      if event.type == MOVEEVENT:
        drawEverySecond()
python events time pygame screen
1个回答
3
投票

[您必须增加数量并每秒渲染一次label表面(每次发生MOVEEVENT时,都::

label = label = myFont.render("Wood: 0", 1, color2) 
def drawEverySecond():
    global label
    text = "Wood: " + str(increaseWood())
    label = myFont.render(text,1,color2)  
    print('Current Wood:', wood)

但是您必须在任何帧中绘制label曲面:

while True:
    pygame.display.update()
    screen.fill(color)
    pygame.draw.circle(screen, (220,40,100),(1000,300), 20,4 )
    screen.blit(label, (1000,300))

    # [...]
© www.soinside.com 2019 - 2024. All rights reserved.