I'm hitting a wall since forever now and it starts to get on my nerves :p. I'm a beginner in the art of coding, so I picked things here and there, but my results are ... slim to say the least.
So, let's say I have 24 hours and I can eat one fruit every hour, randomly picked in a list divided per hour.
At the start, I have only one choice of fruit per hour, then, I add some other fruits and change the odds
I'm using this function to randomize the odds (viewtopic.php?f=8&t=48265&p=478399&hili ... er#p478399):
Code: Select all
def weighted_random(pairs):
total_weight = sum(n for _, n in pairs)
rvalue = renpy.random.randint(1, total_weight)
for (value, weight) in pairs:
rvalue -= weight
if rvalue <= 0:
return value
Code: Select all
default basic_list = {1:[["apple",1],["cranberry",2]],2:[["biscuit",3],["chocolat",4]]}
So, I need to make a class because there will be more than one list and these will have to be often modified during the game :
Code: Select all
init python:
class Food( object ):
global hour
def __init__( self ):
self._dict = {}
self._cur_hour = -1
def new (self, it , val ):
for k, v in self._dict.items():
if k==hour:
test = False
for elem in v:
if elem[0] == it:
test= True
if test==False:
v += [[it , val]]
return True
return False
def new_key(self , key):
test = False
for k,v in self._dict.items():
if k == key:
test = True
break
if test == False:
self._dict[key] = []
def modify (self, it , val ):
for k, v in self._dict.items():
if k==hour:
for elem in v:
if elem[0] == it:
elem[1]+= val
return True
def change (self, it , val ):
for k, v in self._dict.items():
if k==hour:
for elem in v:
if elem[0] == it:
elem[1] = val
return True
return False
Code: Select all
default class_list = Food()
default hour = 1
default choices = ["apple","biscuit","kiwi","cranberry","banana", "strawberry"]
default item_chosen = ""
screen fridge:
frame:
xalign 1.0
yalign 0.5
has vbox
for item in class_list._dict:
if class_list._dict[item] > 0:
$ amount = class_list._dict[item]
text ("[item] : [amount]")
screen controls:
frame:
has hbox
vbox:
for i in range(0,24):
textbutton "[i]" action SetVariable("hour",i)
vbox:
for item in choices:
textbutton ("[item]") action SetVariable("item_chosen",item)
vbox:
textbutton "new hour" action Function( class_list.new_key, hour )
textbutton "add item" action Function( class_list.new , item_chosen , 10 )
textbutton "set item to 1" action Function( class_list.change, item_chosen, 1 )
textbutton "increase item by 1" action Function( class_list.modify, item_chosen, 1 )
label start:
show screen controls
show screen fridge
"hello"
jump start
- How can I create directly a full set off fruits ?
Code: Select all
def start_fruits(fruits_class, start_list):
temp_hour = 0
for it in start_list:
fruits_class.new_key(temp_hour)
for k, v in fruits_class._dict.items():
if k==temp_hour:
test = False
for elem in v:
if elem[0] == it:
test= True
if test==False:
v += [[it , 1]]
return True
temp_hour += 1
return False
# add a start list
default apples_start_fruits =["apple" * 24] #24 x apples, one apple per hour to start
# add a textbutton ... who doesn't work ... obviously :p
textbutton "start fruits" action Function( start_fruits, class_list, apples_start_fruits )