- Jul 3, 2021
- 4,528
- 22,164
As I have learned to code RenPY, there have been a few useful things I have learned about it. Some are probably well known, others maybe not so much. I have added to this text file as I went and just thought I would share it here...
Python:
#Useful Ren'PY code
# Getting a main character's name while providing a visible default and ensuring it isn't an empty string
# length = 15 limits the length of the name to 15, adjust this as needed. The "exclude" does not allow the characters listed.
$ name = renpy.input("What's your name?", "Mark", length=10, exclude='{0123456789/*-+.`~!@#$%^&*()_+-=[];\,<>?:|/}')
$ name = name.strip() or "Mark" #note that if the name is blank the strip will return false and it will assign "Mark" to $name instead
# Ensuring a value is not subtracted lower than zero.
# max() will return the highest value, which would be zero if the value drops below zero.
$ player_health = max(player_health - damage, 0)
# You can also ensure the value doesn't go higher than a specific range.
# min() in this case returns the lowest number so if player_health is higher than 100, it will return 100
# which will cap the maximum you can have at 100.
$ player_health = min(player_health + boost, 100)
# Dialog name and text black outline around font. Values are: [(outline, colour, x, y)]
define gui.name_text_outlines = [(2, "#000", 0, 0)]
define gui.dialogue_text_outlines = [(2, "#000", 0, 0)]
# To pause output in the middle of a sentence use: {w}
"Add in a {w} to pause output in the middle of this sentence."
# You can also add in a pause timer to it like {w=1.5} which forces it to continue on
"Add in a {{w=1.5}{w=1.5} to pause output in the middle of this sentence for 1.5 seconds."
# To add italics use: {i}text{/i}, good for private thoughts.
# To add an underline use: {u}text{/u}, good for emphasizing a word like "I {u}really{/u} need to go!"
# To add bold use: {b}bold text{/b}
# To change text colour use: {color=#0F0}text{/color} Note, use #RGB or #RRGGBB, HTML hex colour codes.
#To output double quotes within a text printout, most people will use \" like...
"Test \"quotes\""
# This will produce: Test "quotes". But this looks ugly to program and there is a simpler way. Python doesn't care about single or double
# quotes. If you want to embed double quotes, just use single quotes for the entire statement like this...
'Test "quotes"'
#...that will produce the same results. You can do the same if you want single quotes with...
"Test 'quotes'"
#Outputting a string with variables embedded.
OutputStr = "Backgrounds/{}_{}_{}.jpg".format(location, chapter, sequence)
# Adding a pause between lines of code. (see also "$renpy.pause()")
pause #pause until key pressed
pause 1.0 #pause for 1 seconds or until key pressed
pause .5 #pause for 1/2 second or until key pressed
# Show text slowly printing out at center of screen using a font and color.
# Note: Use a background image with scene like "scene black", this will print over it.
# This will print "Slowly printing text." on the center of your screen using MyFont.ttf at 10 characters
# per second, with the color #F66 (light red) and a font size of 60.
centered "{size=60}{font=MyFont.ttf}{cps=10}{color=#F66}Slowly printing text.{/color}{/cps}{/font}"
# RenPY arrays and random numbers
# Example code which picks a name at random and displays it.
default Jessica = ["Jessica", "Jess", "Mam", "Mum"] # create an array of four names, add to this if you wish.
$ name = renpy.random.choice(Jessica)
"random name = [name]"
# Adding a progress bar is really simple (https://www.renpy.org/doc/html/splashscreen_presplash.html#adding-a-progress-bar)
You need two images, presplash_background.png (can also be a JPG) for the background, and presplash_foreground.png which will be drawn from left to right as the game loads. This should probably be transparents with a bar or whatever.
### Showing a tall vertical image that pans up and down the length of it.
#First set up a pan_view early in code, with your other defines...
transform pan_view:
yalign 0.0
ease 6.0 yalign 1.0
ease 6.0 yalign 0.0
repeat 2
# Then when you want to pan an image, do this...
show image_name at pan_view
Last edited: