On 9/2/07, <b class="gmail_sendername">David Millar</b> <<a href="mailto:davmillar@gmail.com">davmillar@gmail.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello. I'm working on a text adventure game right now, and I seem to be kind of stuck. There's a huge chunk of code called moreaction() that pulls scripted events for certain locations. It's ever-changing so I'm not looking for specifics, but can anyone suggest good ways to clean up or condense the code without sacrificing clarity? The latest version of the source is found at
<a href="http://thegriddle.net/python/v006.txt" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://thegriddle.net/python/v006.txt</a> Any help is much appreciated :) - Dave M.</blockquote><div>
<br>I just took a look at the code, it seems that you are in if-then hell. I think that I have a solution for you that improve the readability of the code. It's using multiple dictionaries to get you out of if-then hell. Let's say we have:
<br><br>sceneDict = {} <br># We populate these with the difference scene numbers. In the sceneDict, we have eventDict if there are any events.<br></div><br>Lets say we take your first piece of code:<br><pre> if thescene == 3:
<br> if event[1] == 0:<br> global choins<br> choins = choins - 10<br> add_item("BEAN",1)<br> map[2].add_opt("NORTH",4)<br> map[2].set_desc("You are just outside a nasty looking movie theatre. Shady Latin gang members have a shell game set up nearby, and from previous experiences you know to avoid gambling like the plague.")
<br> event[1] = 1<br> elif event[1] == 1:<br> map[3].set_desc("You walk up to the gangsters and the boss guy says 'Get lost, fool!'")<br> event[1] = 2<br> elif event[1] == 3:
<br> map[3].set_desc("You walk up to the gangsters but they tell you to get lost.")<br> if (inventory.has_key("ARMONDO'S NOTE") == 0):<br> print "\nYou walk up to the gangsters and flash a picture of Candy in front of them. 'Woah, is that Candy?' the boss guy asks. I ain't seen her since high school!' He scribbles something on the back of a receipt for frozen wonton burrito meals, and you do the math and realize that he wants you to give candy the number."
<br> add_item("ARMONDO'S NOTE",1)<br> elif event[1] == 4:<br> print "\nYou see Candy with Armondo, and they wave you over. 'Hey, thanks for hooking us up again! And sorry Armondo took your choins in his little game, teehee!' She hands you 5 choins. 'Uhh, he took 10 choins from me, not fi-' 'SHUT UP RUBE!' Candy laughs at Armondo and kisses him on the cheek. 'We're going to the back seat of Armondo's car for coffee. See ya! They walk away and get into Armondo's car, which starts bucking around a bit. Then it suddenly starts up and leaves, opening the street to the south."
<br> choins += 5<br> map[2].rem_opt("TALK")<br> map[2].add_opt("SOUTH",15)<br> map[1].add_opt("ORDER",16)<br> elif thescene == 6:<br> map[5].rem_opt("EAST")
<br> add_item('MAIN MAP',1)<br> add_recipe('ICED COFFEE','COFFEE','ICE',1)<br> add_recipe('ESPRESSO','COFFEE','BEAN',2)<br> add_recipe('CAPPUCCINO','ESPRESSO','MILK',2)
<br> add_recipe('CREAMY COFFEE','COFFEE','MILK',1)<br> elif thescene == 8:<br> if event[1] >= 3 and (book.has_key("SYRUP") == 0):<br> print "\n'PROTIP!' 'Huh?' you respond. 'PROTIP! CANDY and WATER make various sugary SYRUPS to add to your drinks!' Wow. Interesting."
<br> add_recipe('SYRUP','CANDY','WATER',1)<br> disp_menu(0)</pre><br>sceneDict = { 3:"" , 6:"",8:""}<br>sceneDict[3] = { 0:"",1:"",2:"",4:""} # The key 3 points to a dictionary of event dictionary.
<br><br>I have to research how to get the values of these dictionary to be executeable code :). Tell me what you think (by the way, you will need one or two if statements and perhaps an try-except block to make this code work). More tonight or if Kent, Alan or Bob want to step in. :)
<br><br>-Tino<br></div>