0

I have a class that represents a bit of text that can be drawn to the screen. This class is intended to be used in relation to a Window object that, when it is drawn, passes a subsurface of the surface it was given to the draw() function of the text element.

The window draws, but the text does not. Nor does it draw when I invoke the TextElement's draw() function directly. I've examined the code with a debugger, and the blit is definitely being done. I've also tried switching the font to "Arial" instead of letting Pygame grab the default.

import pygame

#Background and edge color of windows
global bgcolor
global bordercolor
global txtcolor
#Pygame font object that most text will be drawn in
global mainfont

#Defaults; code that imports us can change these.
#Black background with white borders & text
bgcolor=(0,0,0)
txtcolor=(255,255,255)
bordercolor=(255,255,255)
#This will give us whatever font Pygame is set up to use as default
if not __name__== "__main__":
    #Font module needs to be initialized for this to work; it should be if we're imported, but won't if
    #we're being executed directly.
    mainfont=pygame.font.SysFont(None,12)
else:
    mainfont=None


class Window:
    """The base UI class. Is more-or-less a way to draw an empty square onto the screen. Other things are derived from this.
    Also usable in its own right as a way to manage groups of more complex elements by defining them as children of
    a basic window."""

    def __init__(self, rect):

        self.rect=rect

        #'children' of a window are drawn whenever it is drawn, unless their positions are outside the area of the window.
        self.children=[]

    def draw(self, surface):
        "Draw this window to the given surface."

        pygame.draw.rect(surface, bgcolor, self.rect)
        #Draw non-filled rect with line width 4 as the border
        pygame.draw.rect(surface, bordercolor, self.rect, 4)

        self._draw_children(surface)

    def _draw_children(self,surface):
        "Call draw() on each of the children of this window. Intended to be called as part of an element's draw() function."
        for thechild in self.children:
            #We use a subsurface because we only want our child elements to be able to access the area inside this window.
            thechild.draw(surface.subsurface(self.rect))

class TextElement(str):
    """A bit of static text that can be drawn to the screen. Intended to be used inside a Window, but can be drawn 
    straight to a surface. Immutable; use DynamicTextElement for text that can change and move."""

    def __new__(cls,text,font,rect=pygame.Rect((0,0),(0,0))):
        self=super().__new__(cls,text)
        self.image=font.render(text,True,txtcolor)
        self.rect=rect
        return self

    def __repr__(self):
        return str.join("",("TextElement('",self,"')"))

    def draw(self,surface):
        self.image.blit(surface,self.rect.topleft)

class DynamicTextElement():
    """A bit of text whose text can be changed and that can be moved. Slower than TextElement."""

    def __init__(self,text,font,rect):
        self.text, self.font, self.rect = text, font, rect

    def __repr__(self):
        return str.join("",("DynamicTextElement('",self.text,"')"))

    def draw(self,surface):
        image=self.font.render(self.text,True,txtcolor)
        image.blit(surface,self.rect.topleft)

if __name__ == '__main__':
    pygame.init()
    mainfont=pygame.font.SysFont(None,12)
    screen=pygame.display.set_mode((800,600))
    mywindow=Window(pygame.Rect(150,150,600,300))
    mytext=TextElement("Hello world! This is static, immutable text!",mainfont,pygame.Rect((200,200),(100,100)))
    mydyntext=DynamicTextElement("And this is dnyamic, movable text!",mainfont,pygame.Rect((200,230),(100,100)))
    print(mytext.image)
    mywindow.children.append(mytext)

    clock=pygame.time.Clock()

    while True:
        pygame.event.pump()
        clock.tick(60)
        screen.fill((55,55,55))
        mywindow.draw(screen)
        mytext.draw(screen)
        pygame.display.flip()

Any ideas?

2
  • BTW: in blit() you can use self.rect in place of self.rect.topleft
    – furas
    Jul 17, 2014 at 23:30
  • have you try to run it in console/terminal/cmd.exe to see errors ? I got error in __new__ in TextElement().
    – furas
    Jul 17, 2014 at 23:32

1 Answer 1

1

You use

    self.image.blit(surface, self.rect.topleft)

but should be

    surface.blit(self.image, self.rect.topleft)

surface and self.image in wrong places.

You tried to draw surface on text.

You have the same problem in DynamicTextElement()


BTW: I use Python 2 so I had to use str

self = str.__new__(cls,text)

in place of super()

self = super().__new__(cls,text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.