954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pygame text.render problem - printing variables

Okay everyone first heres a source page for this module that im having trouble with...

http://www.pygame.org/docs/ref/font.html


Anyways my problem is finding out how to use this module to be able to print a variable with font.render....... anyone know how? Heres the code...

#! usr/bin/env python

import pygame, sys, os
from pygame.locals import *
pygame.font.init

pygame.init()

screen = pygame.display.set_mode((150, 150))
pygame.display.set_caption('Help')
pygame.mouse.set_visible(0)
pygame.display.init()

lives = 3

def main():
global lives
font = pygame.font.Font(None, 36)
text = font.render("Lives =" <strong><em>[somehow but number of lives here]</em></strong>", 1, (250, 250, 250))
screen.blit(text, (0, 0))
pygame.display.flip()

main()


So can anyone figure out how i can do this without an error message popping up? THANKS!!!

iamoldest
Newbie Poster
9 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Something like this will do:

#! usr/bin/env python

import pygame
from pygame.locals import *

pygame.font.init

pygame.init()

screen = pygame.display.set_mode((150, 150))
pygame.display.set_caption('Help')
pygame.mouse.set_visible(0)
pygame.display.init()

lives = 3

def main():
    global lives
    font = pygame.font.Font(None, 36)
    s = "Lives =" + str(lives)
    text = font.render(s, 1, (250, 250, 250))
    screen.blit(text, (0, 0))
    pygame.display.flip()
    # run event loop and provide exit conditions
    while True:
        for event in pygame.event.get():
            # window title 'x' click
            if event.type == pygame.QUIT:
                raise SystemExit

main()
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You