making the weekdays loop

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
saskuto
Regular
Posts: 64
Joined: Fri Jan 15, 2010 2:56 pm
Contact:

making the weekdays loop

#1 Post by saskuto »

Ok so i'm making a basic dating sim type and have the first few days written out as an introduction to get to know characters and such, then I have the 'generic day' which starts from Monday and loops to give the choices in the morning, -go through morning events- another choice menu for the afternoon -go through afternoon events- another choice menu -go through evening events- then the day loops. But at the moment all I have is :

Code: Select all

init:
    
    $ date = "Monday"

    python hide:
        def date_overlay():
            if date:
                ui.image(date + ".png",
                         xpos=1.0, xanchor="right",
                         ypos=0.0, yanchor="top")

        config.overlay_functions.append(date_overlay
Which shows the day Monday and does nothing else ^^;

here's a tiny example of how the script is written:

Code: Select all

label generic_day:

    ##Generic day script

label day_end:

    think "I log on to Ume's chatroom."
    think "After a while I figure she's not coming online and head to bed."
    
    jump generic_day
At the moment the day Monday shows up but never changes. I'm wondering how to get the days to loop along with the generic day.

Do I have to do seven generic days with a different overlay for each one and have the Sunday jump to the Monday so it loops?

shahab96
Veteran
Posts: 228
Joined: Mon May 24, 2010 5:40 am
Location: Lahore, Pakistan
Contact:

Re: making the weekdays loop

#2 Post by shahab96 »

try making a list in which you put all the days. after each day has passed acces the next thing in the list and when it is sunday set the list to go back to its beginning. I have seen code for that but I cant remember where it is.
The true measure of a man is what he does with his power.

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: making the weekdays loop

#3 Post by Jake »

shahab96 wrote:try making a list in which you put all the days. after each day has passed acces the next thing in the list and when it is sunday set the list to go back to its beginning. I have seen code for that but I cant remember where it is.
Making a list of days is simple enough:

Code: Select all

  week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', Saturday', 'Sunday']
If you want to just iterate through a list, you can do something like this:

Code: Select all

  for day in week:
    # This code will run once for every item in the 'week' list, with 'day' set to each item in turn
    if day == 'Monday':
        # This code will only run if it's Monday...
    if day in ['Saturday', 'Sunday']:
        # This code will only run if it's a weekend...
- however, the problem with a 'for' loop is that it'll run through every item in the list once, and then move on - it won't keep going forever... so you'll only see one Monday, one Wednesday and so on.


If you want something which keeps going forever (or at least until some condition is met), you want a while loop:

Code: Select all

  done = False
  while (done == False):
    # This code will be run over and over and over until the 'done' flag is set to true, then it'll stop looping.
    ...
    if (won_girl and saved_world):
        done = True
Of course, this doesn't help you get through your week. One way you could do it is with list manipulation. For example, the split operator gives you a sub-section of a list, and it's done like this:

Code: Select all

  a = myList[2:] # Get a sub-list which contains every item after the second one
  b = myList[:6] # Get a sub-list which contains every item up to the sixth one
So you could loop through your week like this:

Code: Select all

# Copy-and-Pasted from the Python interpreter to demonstrate the approach:
>>> week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
>>> week
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
>>> week = week[1:] + week[:1]
>>> week
['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday']
>>> week = week[1:] + week[:1]
>>> week
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
Which you could implement something like this:

Code: Select all

  week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  done = False
  while (done == False):

    day = week[0] # Get the first item in the 'week' list
    # Do stuff for that day
    ...
    # shuffle first item in list off to the end of the list.
    week = week[1:] + week[:1]
    if (won_girl and saved_world):
        done = True



However, there's probably a much easier way to do it. The modulus operator (%), when given two numbers, will give you the remainder of dividing the first number by the second:

Code: Select all

>>> 5 % 2
1
>>> 10 % 2
0
>>> 3 % 2
1
>>> 4 % 7
4
>>> 13 % 7
6
>>> 7 % 7
0
So if you just keep incrementing the day by 1 (as is done in the DSE demo, for example) then you can just take the modulus of the day by 7, and you'll get a number between 0 and 6 - which you can then use as the index into the list of days:

Code: Select all

  week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  done = False
  day = 0

  while (done == False):

    dayNumber = day % 7
    dayName = week[dayNumber]

    # Do stuff for that day
    ...
    # Advance week on one day
    day += 1

    if (won_girl and saved_world):
        done = True
Server error: user 'Jake' not found

Headache
Regular
Posts: 101
Joined: Sat Mar 20, 2010 9:29 am
Contact:

Re: making the weekdays loop

#4 Post by Headache »

Jake, how do you use implement this code in DSE?

Glazed Donuts
Regular
Posts: 121
Joined: Thu Aug 12, 2010 11:47 am
Contact:

Re: making the weekdays loop

#5 Post by Glazed Donuts »

I'm not sure if this is the right thread to ask it in, but I would like to put some custom text at the top of the screen that lets the user know how many points they currently have. How do I display this? Would I need to create images of each individual number? or is there a better way to do this? I have a background overlay (the UI) that says "Your points are: " and I want the blank space to be filled in with the text-based numbers. If text-based numbers aren't possible, then how do you do it with graphical numbers?

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: making the weekdays loop

#6 Post by Aleema »

Text based numbers are definitely possible ... just use the variable you're storing the numbers in like this:

ui.text("Your points are: %d" % yournumvar, xpos=0, blahblah...)

%d will represent the variable in the text, and you have to define it with "% whatever-your-variable-is" after the closing quote mark.

Glazed Donuts
Regular
Posts: 121
Joined: Thu Aug 12, 2010 11:47 am
Contact:

Re: making the weekdays loop

#7 Post by Glazed Donuts »

Thank you!

Post Reply

Who is online

Users browsing this forum: No registered users