Including a Timer in Pygame

This is the place for queries that don't fit in any of the other categories.

Including a Timer in Pygame

Postby Urbandude23 » Fri Aug 01, 2014 2:53 am

Hi everyone!

I have been struggling when it comes to displaying a timer on the screen that counts up in pygame. I've researched it and nothing seems to work! Here's what I have so far:

texta = font.render("Time:"+str(time), True, black)
screen.blit(texta, [500,100])

My plan was to add 1 every second but it didn't work! Can anyone solve it?

Thank you!
Posts: 1
Joined: Fri Aug 01, 2014 2:52 am

Re: Including a Timer in Pygame

Postby metulburr » Fri Aug 01, 2014 3:14 am

try this:
Code: Select all
import pygame as pg

def update_font(msg, font):
    return font.render(str(msg), 1, (255,255,255))

screen = pg.display.set_mode((600,400))
screenrect = screen.get_rect()
font = pg.font.Font(None, 48)
done = False
display = 0
timer = 0

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    if pg.time.get_ticks()-timer > 1000:
        timer = pg.time.get_ticks()
        display += 1
    label = update_font(display, font)
we will be moving to on October 1 2016
more details here
User avatar
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests