0

How do you render a font so that it changes when the variable changes?

This is my code:

##############################
# Copyright lololol (c) 2009 #
##############################

import pygame
from pygame.locals import *
pygame.init()

GRAVITY = 0.07
FRICTION = None

def getNumKeysPressed():
    temp = 0
    for item in pygame.key.get_pressed():
        if item:
            temp += 1
    return temp

class Player:
    def __init__(self, speed, image, health):
        self.move_speed = speed
        self.x = 0
        self.y = 0
        self.screen_width = 0
        self.screen_height = 0
        self.speed = [0, 0]
        self.image = pygame.image.load(image + '.png')
        self.image_alt = pygame.image.load(image + '_alt.png')
        self.blit_image = self.image
        self.health = health

    def update(self):
        self.x += self.speed[0]
        self.speed[1] += GRAVITY
        self.y += self.speed[1]
        if self.x > self.screen_width:
            self.x = self.screen_width
        if self.x < 0:
            self.x = 0
        if self.y > self.screen_height:
            self.y = self.screen_height
        if self.y < 0:
            self.y = 0

    def switch_image(self):
        temp = [self.image, self.image_alt]
        if self.blit_image == temp[1]:
            self.blit_image = temp[0]
        else:
            self.blit_image = temp[1]

    def walk_left(self):
        self.speed[0] = -self.move_speed

    def walk_right(self):
        self.speed[0] =  self.move_speed

    def walk_up(self):
        self.speed[1] = -self.move_speed

    def walk_down(self):
        self.speed[1] = self.move_speed    

def main():
    clock = pygame.time.Clock()
    p = Player(2, "rifleman_blu", 100)
    screen = pygame.display.set_mode((640,480))
    pygame.display.set_caption("TF2 - 16 Bit - Demake")
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))
    p.screen_width = (screen.get_width() - p.image.get_width())
    p.screen_height = (screen.get_height() - p.image.get_height())
    font = pygame.font.Font(None, 70)
    
    while True:
        clock.tick(60)
        FRICTION = p.speed[0] / 2
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                return
            if  event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    g_exit()
                    
        key = pygame.key.get_pressed()
        if key[K_LEFT]:
            p.switch_image()
            p.walk_left()
            if p.speed[0] != 0:
                p.speed[0] -= FRICTION
                
        elif key[K_RIGHT]:
            p.switch_image()
            p.walk_right()
            if p.speed[0] != 0:
                p.speed[0] -= FRICTION
                
        elif key[K_UP]:
            p.walk_up()
            
        elif key[K_DOWN]:
            p.walk_down()
            
        p.update()

        print p.speed[0]

        background.fill((250, 250, 250))
        background.blit(p.blit_image, (p.x, p.y))
        background.blit(pygame.image.load("health_bar.png"), (0,0))
        text = font.render(str(p.health), 1, (255, 255, 255)) #this didnt work
        background.blit(text, (0,0))
        screen.blit(background, (0,0))
        pygame.display.flip()

if __name__ == '__main__': main()

Any help would be appreciated

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by scru
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.