pygame.font.Font

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 382
Reputation: tomtetlaw is an unknown quantity at this point 
Solved Threads: 4
tomtetlaw's Avatar
tomtetlaw tomtetlaw is offline Offline
Posting Whiz

pygame.font.Font

 
0
  #1
Jun 5th, 2009
How do you render a font so that it changes when the variable changes?

This is my code:
  1. ##############################
  2. # Copyright lololol (c) 2009 #
  3. ##############################
  4.  
  5. import pygame
  6. from pygame.locals import *
  7. pygame.init()
  8.  
  9. GRAVITY = 0.07
  10. FRICTION = None
  11.  
  12. def getNumKeysPressed():
  13. temp = 0
  14. for item in pygame.key.get_pressed():
  15. if item:
  16. temp += 1
  17. return temp
  18.  
  19. class Player:
  20. def __init__(self, speed, image, health):
  21. self.move_speed = speed
  22. self.x = 0
  23. self.y = 0
  24. self.screen_width = 0
  25. self.screen_height = 0
  26. self.speed = [0, 0]
  27. self.image = pygame.image.load(image + '.png')
  28. self.image_alt = pygame.image.load(image + '_alt.png')
  29. self.blit_image = self.image
  30. self.health = health
  31.  
  32. def update(self):
  33. self.x += self.speed[0]
  34. self.speed[1] += GRAVITY
  35. self.y += self.speed[1]
  36. if self.x > self.screen_width:
  37. self.x = self.screen_width
  38. if self.x < 0:
  39. self.x = 0
  40. if self.y > self.screen_height:
  41. self.y = self.screen_height
  42. if self.y < 0:
  43. self.y = 0
  44.  
  45. def switch_image(self):
  46. temp = [self.image, self.image_alt]
  47. if self.blit_image == temp[1]:
  48. self.blit_image = temp[0]
  49. else:
  50. self.blit_image = temp[1]
  51.  
  52. def walk_left(self):
  53. self.speed[0] = -self.move_speed
  54.  
  55. def walk_right(self):
  56. self.speed[0] = self.move_speed
  57.  
  58. def walk_up(self):
  59. self.speed[1] = -self.move_speed
  60.  
  61. def walk_down(self):
  62. self.speed[1] = self.move_speed
  63.  
  64. def main():
  65. clock = pygame.time.Clock()
  66. p = Player(2, "rifleman_blu", 100)
  67. screen = pygame.display.set_mode((640,480))
  68. pygame.display.set_caption("TF2 - 16 Bit - Demake")
  69. background = pygame.Surface(screen.get_size())
  70. background = background.convert()
  71. background.fill((250, 250, 250))
  72. p.screen_width = (screen.get_width() - p.image.get_width())
  73. p.screen_height = (screen.get_height() - p.image.get_height())
  74. font = pygame.font.Font(None, 70)
  75.  
  76. while True:
  77. clock.tick(60)
  78. FRICTION = p.speed[0] / 2
  79. for event in pygame.event.get():
  80. if event.type == QUIT:
  81. pygame.quit()
  82. return
  83. if event.type == KEYDOWN:
  84. if event.key == K_ESCAPE:
  85. g_exit()
  86.  
  87. key = pygame.key.get_pressed()
  88. if key[K_LEFT]:
  89. p.switch_image()
  90. p.walk_left()
  91. if p.speed[0] != 0:
  92. p.speed[0] -= FRICTION
  93.  
  94. elif key[K_RIGHT]:
  95. p.switch_image()
  96. p.walk_right()
  97. if p.speed[0] != 0:
  98. p.speed[0] -= FRICTION
  99.  
  100. elif key[K_UP]:
  101. p.walk_up()
  102.  
  103. elif key[K_DOWN]:
  104. p.walk_down()
  105.  
  106. p.update()
  107.  
  108. print p.speed[0]
  109.  
  110. background.fill((250, 250, 250))
  111. background.blit(p.blit_image, (p.x, p.y))
  112. background.blit(pygame.image.load("health_bar.png"), (0,0))
  113. text = font.render(str(p.health), 1, (255, 255, 255)) #this didnt work
  114. background.blit(text, (0,0))
  115. screen.blit(background, (0,0))
  116. pygame.display.flip()
  117.  
  118. if __name__ == '__main__': main()

Any help would be appreciated
...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: pygame.font.Font

 
0
  #2
Jun 6th, 2009
How do you mean it doesn't work? What sort of behavior are you seeing?
Last edited by scru; Jun 6th, 2009 at 11:35 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC