Pygame script in Ren'Py

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Darkmoonfire
Regular
Posts: 103
Joined: Mon Apr 25, 2011 6:41 am
Completed: Christmas Project
Organization: Lunarescent Wings
Tumblr: darkmoonfire
Contact:

Pygame script in Ren'Py

#1 Post by Darkmoonfire » Tue Aug 05, 2014 1:49 pm

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. ---

nintendotoad
Regular
Posts: 42
Joined: Sat Mar 31, 2012 2:56 pm
Location: projectexist.net
Contact:

Re: Pygame script in Ren'Py

#2 Post by nintendotoad » Tue Aug 05, 2014 6:38 pm

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.

User avatar
Darkmoonfire
Regular
Posts: 103
Joined: Mon Apr 25, 2011 6:41 am
Completed: Christmas Project
Organization: Lunarescent Wings
Tumblr: darkmoonfire
Contact:

Re: Pygame script in Ren'Py

#3 Post by Darkmoonfire » Thu Aug 07, 2014 12:16 pm

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.

User avatar
Darkmoonfire
Regular
Posts: 103
Joined: Mon Apr 25, 2011 6:41 am
Completed: Christmas Project
Organization: Lunarescent Wings
Tumblr: darkmoonfire
Contact:

Re: Pygame script in Ren'Py

#4 Post by Darkmoonfire » Fri Aug 15, 2014 4:58 pm

So how do you get PNGs to work in renpygame? It seems odd that they wouldn't since they work both renpy and pygame.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Pygame script in Ren'Py

#5 Post by xela » Fri Aug 15, 2014 5:30 pm

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.
Like what we're doing? Support us at:
Image

User avatar
Spiky Caterpillar
Veteran
Posts: 252
Joined: Fri Nov 14, 2008 7:59 pm
Completed: Lots.
Projects: Black Closet
Organization: Slipshod
Location: Behind you.
Contact:

Re: Pygame script in Ren'Py

#6 Post by Spiky Caterpillar » Sun Aug 17, 2014 10:02 pm

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.
Nom nom nom nom nom LEAVES.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]