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!
Urbandude23
 
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
pg.init()

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)
    screen.fill((0,0,0))
    screen.blit(label, screenrect.center)
    pg.display.update()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 5 guests