miercuri, 29 august 2012

pygame - test class show text "Hello World"

I wrote in my journal: Today I received an email in which I asked about python and how to display a text message. I hate this approach, especially on the internet there are hundreds of materials on this subject.

This made ​​me to remember my blog and I write a new tutorial on pygame.

I use the old class from last pygame tutorials , see here.

The source code it's pretty simple.

The news is function show_text().

To do this i use pygame functions, see pygame documentation.

Let's see the final source code.

import pygame
from pygame.locals import *

screen_mode = (640, 480)
color_black = 0, 0, 0

class Game:
 def __init__(self):
  self.screen = pygame.display.set_mode(screen_mode)
  pygame.display.set_caption("Pygame text window")

  self.quit = False
 def update(self):
  print "update function"
 def draw(self):

 def show_text(self):
  font = pygame.font.Font(None, 36)
  text = font.render("Hello World!", 1, (100, 100, 100))
  textpos = text.get_rect()
  textpos.centerx = self.screen.get_rect().centerx
  self.screen.blit(text, textpos)

 def mainLoop(self):

  while not self.quit:
   for event in pygame.event.get():
    if event.type == pygame.QUIT:
     self.quit = True
    elif  event.type  == pygame.MOUSEBUTTONDOWN:
     print pygame.mouse.get_pressed()
if __name__ == '__main__':
 game = Game()

The result is this: