Page 1 of 1

Pygame script in Ren'Py

Posted: Tue Aug 05, 2014 1:49 pm
by Darkmoonfire
My brother is trying to get something he made in Pygame to work in Ren'Py and he wanted to ask some questions.

--- So I would like to take advantage of some of Ren'Py's features for a project however I'm running into a couple basic issues that I'll need to understand before I even know how viable it is to take this approach.

I have been able to figure out how to get pygame to take over the display (that's no trick really) but I'm having a much harder time figuring out how to hand the display back to the original settings afterward. I could redo much of the code I already have to use Ren'Py type scripting but that would nullify some of the purpose of this. I have tried to experiment with having the pygame script not take over the screen but just draw on it but I run into the second problem. (as a note: what I've tried was using a pygame.surface.fill( ) which didn't work because the Ren'Py display is OpenGl by default and pygame.surface.blit( ) which is where the second problem came in.

The second problem involves renderables. For some reason I'm having a hard time getting the pygame script to load images through Ren'Py. I can define the image in the Ren'Py script and it knows how to work with it but whether I try to pass it to the normal Python script or try to load the image in the Pygame script with pygame.image.load( ) it can't seem to handle it. I've seen some examples of things which use some of the guts of the rendering system and I could probably work my way through that but I'd rather not if there's a simpler way to do things.

I rather get the idea that Ren'Py was not meant to be used the way I'm trying to use it but I'm wondering if more knowledgeable people could either confirm that or point me in a direction that would make it seem more viable. ---

Re: Pygame script in Ren'Py

Posted: Tue Aug 05, 2014 6:38 pm
by nintendotoad
Read up on Renpygame here.

Get Renpygame here.

Although your brother is going to want to add the following lines to the top of script.rpy:

Code: Select all

init python hide:
    import pygame
    pygame.font.init()
After doing that, he's going to want to scan through both script.rpy and aliens.py. Even if he'd rather not use Renpygame, at least he will be able to see how "going back to Ren'Py from Pygame" works.

Re: Pygame script in Ren'Py

Posted: Thu Aug 07, 2014 12:16 pm
by Darkmoonfire
Thanks for your help. My brother was able to get things working, although he had to use GIFs instead of PNGs because it was having trouble loading those in. Apparently they required some extended images module that he couldn't find.

Re: Pygame script in Ren'Py

Posted: Fri Aug 15, 2014 4:58 pm
by Darkmoonfire
So how do you get PNGs to work in renpygame? It seems odd that they wouldn't since they work both renpy and pygame.

Re: Pygame script in Ren'Py

Posted: Fri Aug 15, 2014 5:30 pm
by xela
I generally avoid giving advice on using pygame in Ren'Py because in 99.9% of all cases you'd be better off taking python logic from pygame code and using Ren'Py's screen language/udds to handle displaying stuff... + we'd have more basecode for Ren'Py if it's something interesting if you're willing to share :)

Otherwise you just load pngs with pygame.image.load(os.path to the image). No libraries should be missing.

Re: Pygame script in Ren'Py

Posted: Sun Aug 17, 2014 10:02 pm
by Spiky Caterpillar
Darkmoonfire's brother wrote:I have been able to figure out how to get pygame to take over the display (that's no trick really) but I'm having a much harder time figuring out how to hand the display back to the original settings afterward. I could redo much of the code I already have to use Ren'Py type scripting but that would nullify some of the purpose of this. I have tried to experiment with having the pygame script not take over the screen but just draw on it but I run into the second problem. (as a note: what I've tried was using a pygame.surface.fill( ) which didn't work because the Ren'Py display is OpenGl by default and pygame.surface.blit( ) which is where the second problem came in.
There are two approaches to this - you can set your game to always run in software mode (by setting the renderer preference in an init block or by modifying Ren'Py, for instance) and then you'll be able to mess with the screen in software mode. Alternatively, you can have the pygame code draw to a Displayable - something like

Code: Select all

init python:
    class Forcefield617(renpy.display.core.Displayable):
        def render(self, width, height, st, at):
            ret = renpy.display.render.Render(width, height)
            surf = renpy.display.pgrender.surface((800,600), True)
            # do all your pygame  manipulations to surf.
            ret.blit(surf,(0,0))
            renpy.redraw(self, 0.02) # Only redraw if your displayable's animated
            return ret
image whatever = Forcefield617()
label start:
    show whatever
should work, and displays in both GL and software. (Useful, because Macs glitched like hell in software mode last I checked.) IIRC, any surface that you're directly blitting into a Render needs to be generated using renpy.display.pgrender.surface (otherwise, it won't have padding, and will segfault often.). You can blit normal pygame surfaces onto surf to your heart's content, though.
Darkmoonfire's brother wrote:The second problem involves renderables. For some reason I'm having a hard time getting the pygame script to load images through Ren'Py. I can define the image in the Ren'Py script and it knows how to work with it but whether I try to pass it to the normal Python script or try to load the image in the Pygame script with pygame.image.load( ) it can't seem to handle it. I've seen some examples of things which use some of the guts of the rendering system and I could probably work my way through that but I'd rather not if there's a simpler way to do things.
You probably want something like renpy.display.im.cache.get(im.Image('fnords.png')) - which returns a Pygame surface and, AFAIK, takes advantage of the image cache.