Fix Code?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
otomeido
Newbie
Posts: 6
Joined: Fri May 06, 2016 5:32 pm
Projects: Event Rising, Cadaver Clicker
Organization: Otomeido Studios
Tumblr: eventrising
Location: Arizona/Georgia
Contact:

Fix Code?

#1 Post by otomeido »

Can someone take a look at this code and let me know if there's a cleaner way of running this code...
Thank you!

Code: Select all

label meetgreet:
    scene courtyard_day with fade
    n "When we stepped out into the courtyard, I expected it to be empty as it had been before, but instead I was pleasantly surprised to see several small crowds of students."
    kaoru "Wow, they sure got here quick. I guess I must've missed them on my way to school?"
    kaoru "But...I don't really see anyone that looks like a teacher? Maybe they're all preparing for that announcement...?"
    show asuka_normal at right
    asuka "Oh! Hey! I think I see some people from my school!"

label groups:
    window hide
    hide asuka_normal
    $ group1 = False
    $ group2 = False
    $ group3 = False
    $ group4 = False
    $ group5 = 0

label group5check:
    if group5 == 4:
        jump Group5v1
    else:
        jump groups2

label groups2:
    $ result = renpy.imagemap("/BGs/courtyardmap.png", "/BGs/courtyardmap_hover.png", [
                        (0, 385, 307, 719, "Group 1"),
                        (320, 276, 607, 627, "Group 2"),
                        (622, 216, 893, 497, "Group 3"),
                        (901, 366, 1279, 713, "Group 4")
                        ])
        
    if result == "Group 1" and group1 == False:
        jump Group1v1
    elif result == "Group 1" and group1 == True:
        jump Group1v2
        
    if result == "Group 2" and group2 == False:
        jump Group2v1
    elif result == "Group 2" and group2 ==  True:
        jump Group2v2
        
    if result == "Group 3" and group3 == False:
        jump Group3v1
    elif result == "Group 3" and group3 == True:
        jump Group3v2
        
    if result == "Group 4" and group4 == False:
        jump Group4v1
    elif result == "Group 4" and group4 == True:
        jump Group4v2



        
label Group1v1:
    $ group1 = True
    $ group5 += 1
    n "Asuka and I make our way over to the other students from Minami. As we get close, I'm able to catch part of their conversation..."
    show juun_normal at left
    juun "...so what d'ya think, Deej? Sounds like it's gonna be the party of the summer, right?"
    juun "I can see it now- the lights, the music, the dancing..."
    n "Matsuda Juun is a 1st year so I've only known him a little  while, but he's a pretty cool guy. He's the kind of guy that's always in the know; despite my only ever having seen him hanging out with DJ or Arina..."
    n "Honestly, I wouldn't be surprised if he came to summer school just to hang out with people."
    n "Must be nice to have that kind of choice in the matter."
    show manaka_mad at right with moveinright 
    manaka "The police, when I tell them there are delinquents making a mess of the lighthouse."
    manaka "You know that place is off limits! It's dangerous! This party is an accident waiting to happen."
    n "That's Horie Manaka, a year above myself. She's very smart and always willing to help people out."
    n "Unless you're playing a game against her - then she's relentless."
    n "She works as a teacher's aid every year."
    show dj_normal with moveinleft
    chiyo "Aw, come on Manaka- don't be like that! Everyone has been looking forward to it for months. And besides, the principal probably already knows about..."
    chiyo "And may have asked me to perform."
    n "Takada Chiyo may also be a 1st year, but they don't call her 'DJ' for nothing. She's a super talented DJ after all, with extensive knowledge of sound systems and 'sick beats'."
    n "Her headphones are always playing music, and I don't I've ever heard the same song play twice from them, either. If she has them on, don't bother trying to talk to her; she's already gone."
    hide juun_normal
    show arina_normal at left
    arina "Yeah, the student couoncil has been discussing making it a yearly celebration. Kind of like a summer prom? I'm surprised you didn't hear about it!"
    n "That' Uetake Arina, she's in he same class as me. She's a good person and pretty smart, but kind of intense... She has a bit of a competitive streak, especially when it comes to table tennis! Despite that, she's never a sore loser."
                                                                                                        
    
    jump group5check
    
label Group1v2:
    kaoru "I don't think they have any more information for us..."
    jump group5check

label Group2v1:
    $ group2 = True
    $ group5 += 1
    kaoru "Hideo, Kaito, Eiko"
    jump group5check
    
label Group2v2:
    kaoru "They seem a little busy now..."
    jump group5check

label Group3v1:
    $ group3 = True
    $ group5 += 1
    kaoru "Arata, Yuuta, Momoko, Tatsuya."
    jump group5check
    
label Group3v2:
    kaoru "I think we should leave them alone for now."
    jump group5check

label Group4v1:
    $ group4 = True
    $ group5 += 1
    kaoru "Kotone, Boton, Nagisa, Miho."
    jump group5check
    
label Group4v2:
    kaoru "I doubt they can help us."
    jump group5check

label Group5v1:
    kaoru "Yuki mamoru"

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: Fix Code?

#2 Post by theCodeCat »

A simple improvement would be to change your If/else statements a little bit.

Code: Select all

# Instead of
if result == "Group 1" and group1 == False:
    jump Group1v1
elif result == "Group 1" and group1 == True:
    jump Group1v2

# I would write
if result == "Group 1":
    if group1 == False:
        jump Group1v1
    else:
        jump Group1v2

# Because it makes it clearer that when result=="Group 1" control either jumps to "Group1v1" or "Group1v2"
Also, when you are dealing with boolean (True/False) values in conditionals, you can write "if x:" instead of "if x == True:" and "if not x:" instead of "if x == False".

The biggest improvement would just be making the logic of your code clearer though. It took me a few minutes just to figure out the flow of your code, which as I understand it is just:
The player has to pick each of the 4 options once in order to proceed. The "group[1-4]" variables track whether an option has been clicked, and the "group5" variable tracks how many different options have been clicked, so when it is 4 you know each button has been selected. The "group[1-4]v1" labels contain what happens the first time an option is selected while the "group[1-4]v2" labels contain what happens when an option is selected after the first time.

Give your labels and variables more descriptive names. Maybe "group1WasSelected" instead of "group1", "uniqueGroupsSelected" instead of "group5", "group1Main" instead of "group1v1", and "group1Secondary" instead of "group1v2". These are by no means perfect but do a much better job of hinting at the purpose of the variable/label.
You could also add a comment to the beginning of the section that briefly explains what happens, like "The player needs to select each option once before the game proceeds to label Group5v1"

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Fix Code?

#3 Post by trooper6 »

I did something similar to what you are aiming to do (though I used Imagebuttons rather than imagemaps). As soon as I get finished with this thing I'm doing right this moment, I'll work up some code to show you how I'd do it.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Fix Code?

#4 Post by trooper6 »

So this is a different take on what you had. Note: I'm using imagebuttons, not imagemaps:

Code: Select all

# Declare characters used by this game.
define n = Character(None, color="#c8ffc8")
define kaoru = Character("Kaoru", color="#c8ffc8")
define asuka = Character("Asuka", color="#c8ffc8")
define juun = Character("Juun", color="#c8ffc8")
define manaka = Character("Manaka", color="#c8ffc8")
define chiyo = Character("Chiyo", color="#c8ffc8")
define arina = Character("Arina", color="#c8ffc8")

default group1_visits = 0
default group2_visits = 0
default group3_visits = 0
default group4_visits = 0

screen group_visit_screen():
    add "meet_base" xalign 0.5
    imagebutton:
        hover "group1_hover"
        idle "group1_idle"
        xpos 155 ypos 55
        focus_mask True
        activate_sound "click.wav"
        action [SetVariable("group1_visits", group1_visits +1), Jump("group1_talk")]
    imagebutton:
        hover "group2_hover"
        idle "group2_idle"
        xpos 170 ypos 250
        focus_mask True
        activate_sound "click.wav"
        action [SetVariable("group2_visits", group2_visits +1), Jump("group2_talk")]
    imagebutton:
        hover "group3_hover"
        idle "group3_idle"
        xpos 400 ypos 100
        focus_mask True
        activate_sound "click.wav"
        action [SetVariable("group3_visits", group3_visits +1), Jump("group3_talk")]
    imagebutton:
        hover "group4_hover"
        idle "group4_idle"
        xpos 450 ypos 200
        focus_mask True
        activate_sound "click.wav"
        action [SetVariable("group4_visits", group4_visits +1), Jump("group4_talk")]

label start:
    "Your game starts, la la la."
    jump meetgreet
    
label meetgreet:
    scene courtyard_day with fade
    n "When we stepped out into the courtyard, I expected it to be empty as it had 
       been before, but instead I was pleasantly surprised to see several small crowds of students."
    kaoru "Wow, they sure got here quick. I guess I must've missed them on my way to school?"
    kaoru "But...I don't really see anyone that looks like a teacher? Maybe they're 
           all preparing for that announcement...?"
    show asuka_normal at right
    asuka "Oh! Hey! I think I see some people from my school!"
    
    call greetloop
    
    kaoru "Yuki mamoru"
    "The Meet and Greet is over."
    jump end
    
  
label greetloop:
    if group1_visits==0 or group2_visits==0 or group3_visits==0 or group4_visits==0:
        call screen group_visit_screen()
    else:
        return
    
label group1_talk:
    scene courtyard_day with fade
    if group1_visits == 1:
        n "Asuka and I make our way over to the other students from Minami. As we 
           get close, I'm able to catch part of their conversation..."
        show juun_normal at left
        juun "...so what d'ya think, Deej? Sounds like it's gonna be the party of the summer, right?"
        juun "I can see it now- the lights, the music, the dancing..."
        n "Matsuda Juun is a 1st year so I've only known him a little  while, 
           but he's a pretty cool guy. He's the kind of guy that's always in the 
           know; despite my only ever having seen him hanging out with DJ or Arina..."
        n "Honestly, I wouldn't be surprised if he came to summer school just to hang out with people."
        n "Must be nice to have that kind of choice in the matter."
        show manaka_mad at right with moveinright 
        manaka "The police, when I tell them there are delinquents making a mess of the lighthouse."
        manaka "You know that place is off limits! It's dangerous! This party is an accident waiting to happen."
        n "That's Horie Manaka, a year above myself. She's very smart and always willing to help people out."
        n "Unless you're playing a game against her - then she's relentless."
        n "She works as a teacher's aid every year."
        show dj_normal with moveinleft
        chiyo "Aw, come on Manaka- don't be like that! Everyone has been looking 
               forward to it for months. And besides, the principal probably already knows about..."
        chiyo "And may have asked me to perform."
        n "Takada Chiyo may also be a 1st year, but they don't call her 'DJ' for 
           nothing. She's a super talented DJ after all, with extensive knowledge 
           of sound systems and 'sick beats'."
        n "Her headphones are always playing music, and I don't I've ever heard 
           the same song play twice from them, either. If she has them on, don't 
           bother trying to talk to her; she's already gone."
        hide juun_normal
        show arina_normal at left
        arina "Yeah, the student couoncil has been discussing making it a yearly 
               celebration. Kind of like a summer prom? I'm surprised you didn't hear about it!"
        n "That' Uetake Arina, she's in he same class as me. She's a good person 
           and pretty smart, but kind of intense... She has a bit of a competitive 
           streak, especially when it comes to table tennis! Despite that, she's never a sore loser."
    else:
        kaoru "I don't think they have any more information for us..."
    jump greetloop
    
label group2_talk:
    scene courtyard_day with fade
    if group2_visits == 1:
        kaoru "Hideo, Kaito, Eiko"
    else:
        kaoru "They seem a little busy now..."
        
    jump greetloop
    
label group3_talk:
    scene courtyard_day with fade
    if group3_visits == 1:
        kaoru "Arata, Yuuta, Momoko, Tatsuya."
    else:
        kaoru "I think we should leave them alone for now."
        
    jump greetloop
    
label group4_talk:
    scene courtyard_day with fade
    if group4_visits == 1:
        kaoru "Kotone, Boton, Nagisa, Miho."
    else:
        kaoru "I doubt they can help us."
        
    jump greetloop    
    
label end:
    "The game is over."
    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Ocelot