【问题标题】:How to change the number displayed when we press button如何更改按下按钮时显示的数字
【发布时间】:2021-11-27 18:02:28
【问题描述】:

我正在尝试制作一个可以倒计时的时钟,但由于我不知道如何更改时钟上的数字而卡住了(例如,当我按下“+ 1 分钟”按钮时)时钟从0:0变为1:0),我是python和python的新手,所以我希望能得到一些我无法理解的信息。 这是我的代码:

import pygame

pygame.init()

screen = pygame.display.set_mode((500,600))

GREEN 
YELLOW
BLACJ
RED
WHITE
KIARA

Text_1 = font_1.render('+',True,KIARA)
Text_2 = font_1.render('+',True,KIARA)
Text_3 = font_1.render('-',True,KIARA)
Text_4 = font_1.render('-',True,KIARA)
Text_5 = font_1.render('START',True,BLACK)
Text_6 = font_1.render('RESET',True,BLACK)

total_secs = 0
mins = int(total_secs/60)
secs = int(total_secs%60)
time = f"{mins}:{secs}"

Text_time = font_2.render(time,True,BLACK)
TextRect_time = Text_time.get_rect()
TextRect_time.center = (248,425)


running = True
while running:
    screen.fill(GREEN)

    mouse_x, mouse_y = pygame.mouse.get_pos()

    screen.blit(Text_1,TextRect_1)
    screen.blit(Text_2,TextRect_2)
    screen.blit(Text_3,TextRect_3)
    screen.blit(Text_4,TextRect_4)
    screen.blit(Text_5,TextRect_5)
    screen.blit(Text_6,TextRect_6)
    screen.blit(Text_time,TextRect_time)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if (70 < mouse_x < 130) and (70 < mouse_y < 130):

                if (170 < mouse_x < 230) and (70 < mouse_y < 130):

                if (70 < mouse_x < 130) and (220 < mouse_y < 280):

                if (170 < mouse_x < 230) and (220 < mouse_y < 280):

    pygame.display.flip()

pygame.quit()

【问题讨论】:

  • 您尚未声明任何这些颜色(绿色、黄色等)。请同时包含 font_1,否则很难重现。
  • 您的颜色名称不是变量。 BLACK = (0, 0, 0) ... 会起作用,因为您将 RGB 值分配给变量。在您的代码末尾,您有大量的 if 语句,但它们没有附加任何操作。 ... if (condition) ... then do ... + 如果您有更多条件,则应在第一个 if 语句之后使用 elif 语句。

标签: python time pygame clock


【解决方案1】:

每次按键时,都需要更改文本并重新渲染文本表面。例如:

if (70 < mouse_x < 130) and (70 < mouse_y < 130):
    mins = (mins+1) % 60
    time = f"{mins}:{secs}"
    Text_time = font_2.render(time, True, BLACK)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多