#function: renpy.random.randint()
Explore tagged Tumblr posts
Note
Hey!! Thanks for your time. So I was wondering how I would write the code for something where I want the chances of success to be based on a percentage. I'm trying to implement lottery tickets, and I need it to have a 0.09% chance of success. And also, how would I make it so you get a different response for each outcome? Like if you failed to get one, I want your buddy to say "aw that's too bad, maybe next time!" And if not I want everyone to freak out, plus the earnings to be added. Thank you!!
I return from the void now that everyone is migrating back from the land of the South African Muskrat! Yours is a rather involved answer to see all of it below the cut
The answer to your question, at least the percentage part, is a matter of the randint() function in the Python library. In this case, you want something to happen only 1 in so many times. For example, 1 in 10 is a 10% chance. To simulate this in code, you can have the interpreter pick a random(note this isnât true randomness, but itâs good enough for a video game we arenât making a cyber security system!) number between 1 and 10. If it comes out to one number and one number only, then you pass the check. This makes it a 10% chance, just like if you rolled a 10 sided die.Â
For your .09% chance(very rare!), it would be 1 in ~1,100 chance.(.090909...%). So if you have Python pick a number between 1 and 1,100 and check to see if itâs one specific number, then youâll have your lottery win.
The easiest way to implement this in game is to record the results of the 1 in 1,100 chance inside a function wrapper so that you can call it multiple times and they can check lots of tickets.
init python:Â Â def Lottery_Ticket():Â Â Â Â ticket_num = renpy.random.randint(1, 1100)Â Â Â Â return ticket_num
In order to use the randint() function from the random library, you just need to invoke it! Itâs a part of Renpyâs library. So youâd type renpy.random.randint(1,1100) like above. :)
Once that is done in your game code itself youâll put the logic for the winning number. Letâs say itâs 42, for meme reasons.Â
You could write in your game file:
label check_ticket:  $ ticket = Lottery_Ticket()  if ticket == 42:    jump you_win  else:    jump you_lose
This simple logic sets the ticket equal to the result of the Lottery_Ticket function. It then checks if the number is exactly the winning number you chose. If so, it takes you to the winning section of your game code, if not, it falls through and says you lose.
Every time they buy a ticket, you can send them to this label, it will re-run the Lottery_Ticket function anew, picking a new random number, and see if they won.
Under your winning code, since you asked, this is where youâd put your characters reacting positively! Note the thing in brackets is the player characterâs name as a variable being interpolated by the engine. If you let the player pick their own name, thatâs how youâd get it into the script. If they have a set name, just type that instead.Â
label you_win:  e âOh my God! [PC_Name] you did it!  e âItâs like a dream come true!â  $ Inv.add(lottery_winnings)  $ renpy.notify(âYou addedâ + str(lottery_winnings) + â to your wallet!â)
As you see, Iâm using to functions. One that comes with RenâPy calls in the notify screen context through the function. It needs to be a string inside which is why Iâve turned the int representing the lottery winnings into a string here.(This assumes you have a variable called âlottery_winningsâ that has a specific integer value! Please initialize one before the start of your game!) (example: $ lottery_winnings = 10000)
The second is me assuming that you have an Inventory class instance with a method called âaddâ that can add money into a wallet variable. To make something like that:
init python:  class Inventory(python_object):    def__init__(self, wallet = 0):      self.wallet = wallet      self.pocket = []       def add(num):      self.wallet += num      return self.wallet
Then you just need an instance of it:$ Inv = Inventory() (I wrote it with defaults, so you donât need to pass any arguments to the instance when you make it, though you are welcome to pass an INTEGER to it when you instantiate it so that your character starts with a certain amount of money. :D )
Then you can call the method on the variable to add the earnings to their wallet. :)Â
Thereâs also an empty pockets list inside of the inventory you can use to store the string names of objects they also own. For something like that:
$ Inv.pocket.append(âappleâ)
This will add a string called âappleâ to the pocket list inside the inventory. Which you can use to check if they âownâ an object by checking if it is in the list.
if âappleâ in Inv.pocket:  .......
But thatâs more detail for another time.Â
You need to put the class in an init python block before the game starts so that you can make an instance of it to manipulate later.
For not winning, just have the âyou_loseâ label have the dialogue you wanted.
label you_lose:    e âAw, thatâs too bad. Maybe next time!â
And then continue on with the rest of the game.
I hope everything here is clear and this helps. I know this ask came in ages ago, but I still think answering it will help RenâPy users in the now. Remember: If your code wonât compile, and you donât what to do; who you gonna call? The RenâPy Help Desk!
#RenPy#Ren'Py#thehelpdesk#Renpyhelpdesk#keyword: python#keyword: init python#Python#Python Classes#Inventory Code#function: renpy.notify()#function: renpy.random.randint()#keyword: jump#keyword: label
14 notes
¡
View notes