pygame.error: Surfaces must not be locked during blit?

pygame.error: Surfaces must not be locked during blit?

Postby ChristianCareaga » Thu Apr 24, 2014 10:40 pm

I'm trying to create a couple of simple GUI classes for Pygame

So far my idea is to make a Frame class, then make that frame class have widgets that it blits to itself, then i realized the widgets coordinates would be relative to the screen even though I'm blitting them to the frame, So I thought subsurfaces might work

So I'm now trying to make the Frame a subsurface of the screen surface, and make the widgets subsurfaces to there parent frame, but I'm having trouble doing this properly

Here is my frame class:

Code: Select all
class Frame(object):
    def __init__(self, screen, size, pos, **kwargs):

        self.screen = screen
        self.size = size
        self.pos = pos
       

        self.image = pygame.Surface(self.size)
        self.rect = self.image.get_rect(topleft=self.pos)

        self.image = self.screen.subsurface(self.rect)
        print self.image.get_parent()

        self.process_kwargs(kwargs)

        self.widgets = []

    def process_kwargs(self, kwargs):
        defaults = {
                    "color" : pygame.Color("green")
                    }

        for kwarg in kwargs:
            if kwarg in defaults:
                defaults[kwarg] = kwargs[kwarg]
            else:
                raise KeyError("Button accepts no keyword {}.".format(kwarg))
        self.__dict__.update(defaults)

    def update(self):
        self.screen.blit(self.image, (self.rect))
        self.image.fill(self.color)

        for w in self.widgets:
            w.update()
            w.draw(self.image)

    def check_events(self, event):
        for w in self.widgets:
            w.check_events(event)


here is a button as an example of a widget:

Code: Select all
class Button():
    def __init__(self, frame, text, command, **kwargs):

        self.frame = frame
        self.text = text
        self.command = command
        self.clicked = False

        self.process_kwargs(kwargs)

        self.image = pygame.Surface(self.size)
        self.rect = self.image.get_rect(topleft=self.position)
        self.image = frame.image.subsurface(self.rect)
        self.image.fill(self.color)
        self.image.unlock()
       
        self.create_text()
        self.frame.widgets.append(self)

    def process_kwargs(self, kwargs):
        defaults = { "font" : pygame.font.Font(None,16),
                    "color" : pygame.Color("white"),
                    "highlighted" : pygame.Color("blue"),
                    "font_color" : pygame.Color("black"),
                    "position" : (5, 0),
                    "size" : (100, 50)
                    }

        for kwarg in kwargs:
            if kwarg in defaults:
                defaults[kwarg] = kwargs[kwarg]
            else:
                raise KeyError("InputBox accepts no keyword {}.".format(kwarg))
        self.__dict__.update(defaults)

    def create_text(self):
        self.text = self.font.render(self.text,True,self.font_color)
        self.text_rect = self.text.get_rect(center=self.rect.center)

    def check_events(self, event):
        self.check_click(event)

    def check_click(self, event):
        '''pass events from frame to check for click'''
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            self.on_click(event)

        elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
            self.on_release(event)


    def on_click(self, event):
        if self.rect.collidepoint(event.pos):
            self.clicked = True
            self.command()
        else:
            self.clicked = False

    def on_release(self, event):
        self.clicked = False


    def update(self):
        self.image.fill(self.color)

        if self.clicked:
            self.image.fill(self.highlighted, (self.rect.inflate(-4,-4)))

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


currently when i run this I'm getting an error that says:

Code: Select all
Traceback (most recent call last):()

  File "C:\Documents and Settings\User\Desktop\GUI\example.py", line 62, in <module>
    Game()
  File "C:\Documents and Settings\User\Desktop\GUI\example.py", line 28, in __init__
    self.Loop()
  File "C:\Documents and Settings\User\Desktop\GUI\example.py", line 37, in Loop
    self.Draw()
  File "C:\Documents and Settings\User\Desktop\GUI\example.py", line 58, in Draw
    self.GUI.update()
  File "C:\Documents and Settings\User\Desktop\GUI\frame.py", line 39, in update
    self.screen.blit(self.image, (self.rect))
pygame.error: Surfaces must not be locked during blit


but when I check the surfaces it tells me they are unlocked? Is there a better way to do this or a right way to do what I'm trying?

-Chris
Last edited by Yoriz on Sun Apr 27, 2014 5:56 pm, edited 1 time in total.
Reason: Changed Title.
ChristianCareaga
 
Posts: 52
Joined: Sat Jun 22, 2013 9:54 am

Re: Pygame GUI problems

Postby Mekire » Fri Apr 25, 2014 8:15 am

I don't really think subsurfaces are what you want here. Just make a couple of simple functions; one that takes a coordinate relative to a frame and returns a coordinate relative to the screen; and another that takes a coordinate relative to the screen and returns a coordinate relative to a given frame.

-Mek
User avatar
Mekire
 
Posts: 1033
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Pygame GUI problems

Postby ChristianCareaga » Fri Apr 25, 2014 9:58 pm

Mekire wrote:I don't really think subsurfaces are what you want here. Just make a couple of simple functions; one that takes a coordinate relative to a frame and returns a coordinate relative to the screen; and another that takes a coordinate relative to the screen and returns a coordinate relative to a given frame.

-Mek


That's what i originally tried, I tried again and sort of got it just need to fix a couple kinks

btw, that was the 100 post in the Game Dev topic :P
ChristianCareaga
 
Posts: 52
Joined: Sat Jun 22, 2013 9:54 am

Re: Pygame GUI problems

Postby metulburr » Fri Apr 25, 2014 11:19 pm

btw, that was the 100 post in the Game Dev topic

you mean 1000th?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1513
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest