0

I am creating a class which shall return me a surface with a square and the text as displayed below:

surface

The square (currently) shows the user's colour for the game and text is player name. I'll blit these surfaces on the screen after calculating the positions from an array of similar surfaces.

I'm able to generate the text surface easily. I don't want to pass the screen surface to the class for pygame.draw.rect; but rather, create the rectangle separately; merge the rectangle and text surface and then return this grouped surface. My current class definition is simply:

from pygame.locals import *
from pygame import font as pyfont, event as pyevent, display as pydisplay
from sys import exit

class Info:
    GRAY = ( 214, 171, 127 )
    def __init__( self, name, colour ):
        pyfont.init()
        self.name = name
        self.colour = colour
        self.font = pyfont.Font( None, 36 )

    def generate( self ):
        text = self.font.render( self.name, True, self.GRAY )
        # The rectangle surface to be drawn here
        return text
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • @jonrsharpe Nothing. I can create the rectangles if I pass the screen surface as a parameter to the class, but as I mentioned in the question; I don't want to do that. I want to leave all the positional calculations for later. I've also looked at the [`subsurface`](http://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface) function; but again, I'd have to calculate my placement positions before initialising the class. – hjpotter92 Feb 16 '14 at 10:20

1 Answers1

1

After a bit of more reading of pygame docs, I've updated the class to be something like:

import pygame
from pygame.locals import *
from pygame import font as pyfont, event as pyevent, display as pydisplay
from sys import exit

class Info:
    GRAY = ( 214, 171, 127 )
    def __init__( self, surface, name, colour ):
        pyfont.init()
        self.surface = surface
        self.name = name
        self.colour = colour
        self.font = pyfont.Font( None, 36 )

    def generate( self ):
        t = pygame.Rect( 5, 5, 32, 32 )
        pygame.draw.rect( self.surface, self.colour, t, 0 )
        text = self.font.render( self.name, True, self.GRAY )
        return self.surface.blit( text, (50, 5) )

and it can be used as follows:

if __name__ == "__main__":
    x = pygame.Rect( 50, 50, 250, 100 )
    screen = pydisplay.set_mode( (1024, 576) )
    pydisplay.set_caption( "Title goes" )
    srf = screen.subsurface( x )
    x = Info( srf, "hjpotter92", (230, 150, 51) )
    c = pygame.time.Clock()
    screen.fill( (255, 255, 255) )
    while True:
        pydisplay.update()
        c.tick( 1 )
        print x.generate()
        for event in pyevent.get():
            if event.type == QUIT:
                exit( 0 )
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit( 0 )

But it is a dirty/foul solution to the problem, and generates something like:

current-output

I'm open to changes to the current design. Otherwise I'd have to keep calculating the positions for further instances of Info class beforehand.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183