NOTE: Sometime around 2015 there was an update to Google Forms that broke the ability to quickly/easy store high scores for Twine Games in Google Drive. Sorry!

Screen Shot 2014-05-31 at 6.18.51 PMThe first thing that you will obviously need is a Twine game that stores the player’s points as a variable. To do this, write a bit of code like this in the Start passage: <<set $points = 100>>. Any time that you need to change the player’s points during the game, use some code like this: <<set $points = $points + 5>> or this: <<set $points = $points - 7>> As a side note, it's possible to use shorthand like this <<set $points += 5>> to add or subtract points, but I prefer to teach the former code to my elementary and middle school students because I believe that <<set $points = $points + 5>> is easier to comprehend.

Your Twine game will also need to store the player’s name. An easy way to ask the player their name is to use this code:

<<set $name = prompt("What’s your name?")>>

Screen Shot 2014-06-01 at 7.16.59 PMCreate a Google Form with two questions, each with Text input fields. Make the first question “Name: ” and “Score: ” for the second question. Don’t worry. We’re not just going to ask the player to type in their score, we’re going to use a script to enter their score automatically. Uncheck “Show link to submit another response” on the Confirmation Page.

Click “View live form” to see your form. Right-click and choose “View page source.” Copy all of the HTML code from...
Screen Shot 2014-05-31 at 6.02.18 PMb
to...
Screen Shot 2014-05-31 at 6.02.18 PM
Screen Shot 2014-06-01 at 7.12.09 PMb
...and paste all of this into the last passage of your Twine game.

Type <html> before the HTML code that you just pasted and type </html> after it.

Create a new Script passage, then copy and paste the following code:

/*Places the variables (given as parameters) into the Google Forms
text input field.*/
macros['googleForms'] =
{
handler: function (place, macroName, params, parser)
{
/*Enter the ID of the first Google Forms text input field here.*/
a = place.querySelector("#entry_111111111");
a.value = eval(Wikifier.parse(params[0]));
/*Enter the ID of the second Google Forms text input field here.*/
b = place.querySelector("#entry_222222222");
b.value = eval(Wikifier.parse(params[1]));
}
};

In the Script, replace entry_111111111 with the ID of your first Google Forms text input field and replace entry_222222222 with the ID of your second text input field. Make sure that you leave the # before the IDs.

After </html> type <<googleForms $name $points>>. Replace $name with whatever variable you used to store the player’s name and replace $points with whatever variable you used to store the player’s score.

You can test it out at this point by playing your Twine game. When you get to the end of your game, you should see something like this:

Screen Shot 2014-05-31 at 5.49.31 PM

Once you’ve tested it and it’s working, we’re going change the HTML code so that all of this business is hidden. Change <input type="text" to <input type="hidden" for both of the text input boxes. Change value="Submit" to value="High Scores" or something like that. Delete the following text from the HTML code: Name:, This is a required question, Score:, This is a required question, Never submit passwords through Google Forms.

Next we’re going to set up the Google Spreadsheet that will store the high scores. A spreadsheet should have been automatically created when you made the Google Form. If you named your form “Twine Game High Scores,” then the spreadsheet will be named “Twine Game High Scores (Responses).” Open it and insert a new sheet.

Add this code to cell A1 of the new sheet: =SORT('Form Responses'!B:C,2,0). If your game works so that low scores are better than high scores, then you would use this formula instead: =SORT('Form Responses'!B:C,2,1).

At the bottom-left, you will see “Form Responses” and “Sheet 2.” Drag and drop them so that “Sheet 2” comes before “Form Responses.”

Click the Share button at the top-right. Copy the “Link to share” and then share the file so that anyone with the link can view (but not edit).

Open your Google Form. Type “Your score has been saved. Click here to view the high scores.” in the box for the Confirmation Page and then paste the “Link to share.”

Here is a demo: ohiofi.github.io/twinehighscoredemo.html

I used this in my newest Twine game, Sundown.
You can check out Sundown here: http://ohiofi.github.io/sundown

mario


catdance2

NOTE: Material in this tutorial does NOT work with Twine 2's default format, Harlowe.

Scripts allow you to add new features to your Twine game. You can find these scripts posted on blogs and in Twine forums. For example, Leon Arnott has created many scripts and has posted them to his blog, Glorious Trainwrecks.

To add a script to your game, click the menu in the bottom-left, then click Edit Story JavaScript. Copy and paste the script from the blog into the Edit Story JavaScript passage. Here is a simple script that I created that will allow you to change the background image within passages:

//changes the background image
macros['setback'] =
{
handler: function (place, macroName, params, parser) {
document.body.style.backgroundImage = 'url('+params[0]+')';
}
};


Screenshot 2014-05-13 11.07.31
Screenshot 2014-05-13 15.21.17After adding that, you can use the macro <<setback>> on any page in which you would like to change the background image. Be sure to include the URL for the image. Like this:

<<setback http://ohiofi.com/img/oh.png>>

<<setback http://ohiofi.com/img/oh.png>>

Finally, you might want to include some CSS like this:

body {
background-repeat:no-repeat; background-size:100%;
}


Here is a DEMO that uses my setback macro.
Screenshot 2014-05-13 16.10.03
mario
This is part of a larger "How To" guide, click here for my full How To Use Twine guide.

CONTINUE TO THE NEXT POST: How to use Google Drive to store High Scores for Twine games


Twine style

May 12, 2014

RANDOM NUMBERS

tumblr_maikmkhJud1qzl9pho1_75sqGames are more interesting if they are different each time you play. In board games, rolled dice and shuffled cards are typical. In video games, random numbers are used. You can use the code random to "roll dice" and get a number between 1 and 6. The code below says to the computer, “Generate a number between 1 and 6. If you roll a 1, the player has found a dollar.”

(set: $diceroll to (random: 1, 6))
(if: $diceroll is 1)[
  You found a dollar! (set: $money to $money + 1)
]

The code below say to the computer, “Generate a number of coins (between 1 and 100) to be found in a treasure chest and tell the player how many coins they found.”

(set: $randomtreasure to (random: 1, 100))
You found $randomtreasure coins in the treasure chest!
(set: $coins to $coins + $randomtreasure)


treasure

PICTURES AND ANIMATED GIFS

You can use <img src=" "> to add pictures and animated gif files to your game. Make sure that you only use one angle bracket on each side, not two. The web address of the file needs to go in between the "quotation marks," like this:

<img src="http://ohiofi.github.io/img/catdance1.gif">

STYLE

nyan

To customize the way your game looks, click the menu in the bottom-left, then Edit Story Stylesheet. In the stylesheet, you can write CSS code that will allow you to change the background color, the text color, the maximum size for pictures, and much more.

If you put the code background:lightblue; in the section after body { , it changes the background to light blue. Putting the code color:purple; in the section after body { changes the text color to purple. The code img { max-width:600px; } says to the computer, "All images (pictures, animated gifs, etc.) should be 600 pixels wide or less."

Screenshot 2015-10-28 12.43.21
Here is some basic CSS that can be used with Harlowe, the default Twine 2 theme.

@import url(https://fonts.googleapis.com/css?family=Press+Start+2P); .mars { /* A background named 'mars' */   background-image: url("http://ohiofi.github.io/img/mars.jpg");   background-size: cover; } tw-story {   /* The following changes the text */   color: darkgreen; /* Set text to green or other color */   text-shadow: 1px 1px lightgreen; /* Green text shadow */   font-size: 20px;   font-family: "Press Start 2P","Helvetica","Arial",sans-serif;   /* Set background to fade lightblue to blue */   background: lightblue; /* For old browsers */   background: -webkit-linear-gradient(lightblue, blue);   background: -o-linear-gradient(lightblue, blue);   background: -moz-linear-gradient(lightblue, blue);   background: linear-gradient(lightblue, blue); } tw-passage {   /* The following changes the text box */   border: 5px groove gray; /* Gray border around the box */   border-radius: 25px; /* Rounded corners on the box */   background: rgba(255,255,255,0.7); /* 30% transparent box */   padding: 25px; } tw-link {   color:darkblue; /* Set the link color to darkblue */   transition: color .5s ease-in-out; } tw-link:hover { /* Mouse over a link & it turns orange */   color: orange; } img { /* Images won't get larger than 600px */   max-width: 600px;   max-height: 600px; }


mario




RECENT POSTS