Tuesday 9 December 2014

Day 9 - Variables in Ren'Py - dice roll d20

The plan is to have some stats which is a more RPG-like thing, and I love the idea of having these decided by a dice roll as with pen and paper games like d&d.

I had to read a little of the Ren'Py documentation:

At the bottom of that page I spotted:
# return a random integer between 1 and 20
$ d20roll = renpy.random.randint(1, 20)
Which is just what I needed. My code now includes:

    $ vitality = renpy.random.randint(1, 20)
    $ skill = renpy.random.randint(1, 20)
    $ luck = renpy.random.randint(1, 20)    
    
    "Your vitality is [vitality]"
    "Your skill is [skill]"
    "Your luck is [luck]"

Which I have tested and that seems to work as desired. When the skill checks come a bit later on in the game I'll need to add some logic, so I'll be consulting the documentation again at that point to make sure I get my syntax correct.

--

Update 18/10/2015:

I should've updated this post a long time ago! So here is how you would do a skill check in Ren'Py, using an example from my (ever) unfinished pizza game:

    $ skill += renpy.random.randint(1, 20)

#The above line takes the variable named skill and adds another random d20 number to it and saves that as the new value for skill
 
    "[skill]"

#Displays that new number to the player

    if skill >= 30:
        jump wall
    else:
        jump walk

#That last bit here is an else/if statement that checks if the skill roll is equal to or above above 30. If it is above 30 then the scene jumps to "wall" or if it's below then the scene will be "walk"

This example is quite specific in that the game only checks against skill once, so I didn't bother saving the additional "skill check" roll as a new variable, but the logic and syntax should be pretty similar.

In fact I just tried a quick tester and it seems to work!

    $ skill = renpy.random.randint(1, 20)
    "Your skill is [skill]"
 
    $ skillRollOne = renpy.random.randint(1, 20)
 
    $ skillRollOne = skill + skillRollOne
 
    "[skillRollOne]"
 
    if skillRollOne >= 30:
        jump wall
    else:
        jump walk

That is similar to the above, only your original skill value isn't overwritten when you do the skill check.

I hope this is helpful to some people out there and yes I am quite a novice at programming, so if any programmers out there read this and notice bad practice please let me know so that I can update the post!






No comments:

Post a Comment