我需要用pygame制作一个秒表

时间:2014-05-18 01:48:03

标签: python python-2.7 pygame

我制作了秒表,但是它出现在IDLE中,我有我的pygame窗口,但是如何让秒表出现在pygame窗口?我尝试了不同的东西,但我不知道如何在窗口中显示它。

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


background_colour = (255,255,255)
(width, height) = (720, 480)

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
myfont = pygame.font.SysFont("monospace", 25)

label = myfont.render("countdown", 1, (0,0,0))
screen.blit(label, (100, 100))

pygame.display.flip()

def cronos():
    clock = pygame.time.Clock()
    minutes = 0
    seconds = 0
    milliseconds = 0

    while True: #game loop
        #do stuff here
        if milliseconds > 1000:
            seconds += 1
            milliseconds -= 1000
        if seconds > 60:
            minutes += 1
            seconds -= 60

        print ("{}:{}".format(minutes, seconds))

        milliseconds += clock.tick_busy_loop(60)

2 个答案:

答案 0 :(得分:0)

为防止数字重叠,您可以添加以下内容:

clock = pygame.time.Clock()
minutes = 0
seconds = 0
milliseconds = 0

cover = pygame.surface.Surface((160,40)).convert()
cover.fill((220, 220, 220))
while True:
    if milliseconds > 1000:
        seconds += 1
        milliseconds -= 1000
        screen.blit(cover, (0,0))
        pygame.display.update()

    if seconds > 60:
        minutes += 1
        seconds -= 60
    milliseconds += clock.tick_busy_loop(60)
    timelabel = myfont.render("{}:{}".format(minutes, seconds), True, (0,0,0))
    screen.blit(timelabel,(0, 0))


    pygame.display.update()

答案 1 :(得分:-1)

只需添加标签,然后在while True

中进行blit即可
#...
while True:
    timelabel = myfont.render("{}:{}".format(minutes, seconds), 1, (0,0,0))
    screen.blit(timelabel, (200, 100))
    #Rest of your code