Coding
-
Creating Interactive Fiction - from Twine to Python
Follow these instructions to install Python 3, then write your interactive fiction in Python Setting up Python In Chrome, go to the website: python.org/downloads Download and install the latest version of Python 3. As of today, that's 3.6.2 but it might be newer for you, depending on how far in the future you are reading this. Greetings from the past! Installing Python Click the Continue button on three installation screens screens (Introduction, Read Me, and License). Agree to the terms of the software licensing. Type in your administrator password. When the installation is successful, click on the Close button Click on Move to Trash when asked. Open IDLE IDLE is where we'll write and run our Python program. Go to your Applications folder and open IDLE in the Python 3 folder. The Shell When you open Python, you'll be greeted by the Shell. This is the place where the output of our program will be seen. For now, check that the first line says Python 3.X.X. We want to be sure we are using Python 3, not 2. Open a new file We write our program in a new file, not the shell. Open a new file for our use Creating our python program Ready to get programming? Let's get started. One of the most important parts of any programming language is comments. This allows us to write notes to ourselves or others who will read our program. These won't be run by Python - they are just for the humans who look at the code! Comments are really helpful in explaining your code, and helping you work through your thinking. A comment is made by typing a #, followed by your comment. Notice how the comment is red? IDLE colors the code to keep it easy to follow. We'll add comments as many places as possible. Starting your program At the top of your program, it's a good idea to start with a few lines of comments. Start your program with a comment for the name of the program. Follow that with your name, then the date, then any notes you might want to add. Twine Remember our story from Twine? Let's get that open. We'll start by making the starting passage from our Twine story in Python. Double click on the opening passage. Viewing the passage Here we can see the text and the choices from this passage. Keep this open while we hop over to Twine Turning Passages into Functions Let's turn this passage into a function. A function is a chunk of code that does one specific task. In this case, we'll make a function that shows the text, waits a second, shows the options and then goes off to the next passage depending on what the user chooses. A function is made, or defined, by using the define command. Enter def followed by the name of the function you are making, then an open and close parentheses, then a colon. Like this: def intro(): We could call our starting passage whatever we like, so you might have def start(): or def beginning(): or def whateveryouwanttocallit(): depending on your style. After we define the function, let's go down to the next line and indent by pressing the tab key once. Here we'll make text appear on the screen with the print command. The print command works as follows print ("TEXT YOU WANT TO SHOW"). In this case we want to show the text from the passage. Notice how we made the print command go over 3 lines? That makes it a bit easier to read. To do that, just end the line with a ", then return to the next line and start with another ". Don't forget to end the print with the closing ). That's a classic programming error, or bug. Remember, if you open something you must close it! Just wait a second The next thing we want to do is pause 1 second before displaying the choices. To do this, we'll use the line time.sleep(1). See how we've added a comment in the same line as this command? That's called an in-line comment and it's a super good idea. Use comments as many places as possible to make it easy to understand your code. time.sleep() is a special function that requires what's called a module. In order to use a module, we have to import it which we'll do in the next step... Import time Above the function, import the time module by typing import time. Behind the scenes this brings in a bunch of complicated code so Python knows what a second is. We won't worry too much about that, but give a quiet thank you to whomever created the time module and just saved you a ton of work. The important thing (get it) is that if we use a function from a module, we must import it at the beginning of our program. Give me options Now that we've shown the text and waited a second, let's show the options. To do this, use a print command. Notice how this print command uses a different option for spanning multiple lines? If you surround your print text, which is called a string, in tripple quotes ("""), you can span multiple lines. It's another way to span lines like we did before. Choose whichever feels right to you. Listen to your heart. Don't forget to use inline comments - they are super helpful! Input please Now we need to get input from the user. To do this, we'll create a variable, which is a bucket in which to save something. We'll call it choice, and that's where the user's choice will be saved. We'll set that equal to a function called input(). Input gives the user a chance to type something in. So here, we are asking the user to type something, and we are saving it as choice. Choices Now'll we'll look at what the user said. If it's A, we'll go to the function that matches that choice (which we'll make later). To do this we'll use the if statement. An if statement looks to see if something is true, and if it's true it'll do something. Here we setup the statement like so: if choice in answer_a: Remember, choice is where we are saving the answer from the user, and answer_A is a list of acceptable answers that'll match as A. We'll set that list up soon. After the if line, we indent (use tab) and then enter the name of a function we'll make for that choice. In this case, if they select A they will be stopping the colonists, so we named the function option_stop_colonists(). You can name the functions as you see fit. Adding other choices Now that we've checked if the user chooses A, we'll setup the next if statement for B. Here we'll use an elif - this is Else IF, which means if it wasn't A, let's check if it's something else. The line will be elif choice in answer_B: answer_B is a list of acceptable choices that'll match for B. Then we name a function we'll make later that will be the passage for that option. In this case B matches with waiting, so we named the function option_wait(). You can name the function as you see fit. What if they didn't choose A or B Finally we need to check if they didn't choose A or B. To do this we'll use an else: which says anything other than what we looked for above will be matched as true. So if it's anything else, we'll want to print some instructions on the options. we will save those instructions as required, so for now we'll add else: and then indented on the next line, print (required). Finally we'll want to give them the passage again, so we'll run this function again - intro() Possible Answers At the top of our program, after the import, we'll want to make a few lists of what answers match as A, B, C, etc. Here we create a variable named answer_A, answer_B, etc, and set them equal to what the user could type to indicate that choice. A list starts with a bracket [, has each item in "quotations", separated by a comma ,. Required Text Next we'll create a variable that has the instructions if they choose something we don't expect. To do this we'll add required = (\nUse only A, B, or C\n") Notice how the start and end of the string have \n in them? That starts a new line and can be helpful in adding some space to your text. Creating another Passage/Function Now we'll create another function for stopping the colonists. From before we decided to call this option_stop_colonists(). Open the passage in Twine. Use the same pattern to create the function Use the same pattern as before to create this function. Remember, use comments to help make the code easier to understand. We: Name the function Print the passage text Wait 1 second Print the options Ask the user for their choice Check if it's A Check if it's B Tell them to try again if they entered anything else. Creating another passage/function Let's do this again for each passage. Here's the function for option_wait() Here's what option_wait() would look like Running the program After we create all of our functions, we will run the starting function to start it all. Here that's just intro(). Running the Program To run the program, in IDLE click Run then Run Module. Save if you haven't yet Save your code if you haven't yet by clicking OK and entering a name and choosing a location Your program runs in the shell Test your program by running it and choosing every option. Does it behave as you'd expect? Full example code Here's the full example code for your reference.
-
Creating Interactive Fiction Using Twine
Follow these instructions to make an interactive story using twine. Download and Install Twine Go to twinery.org and download and install Twine 2 for your operating system. Giving Permission for Twine to Open in System Preferences. The first time you click on the Twine app, it may display the below error. If so: 1. Click OK 2. Under the Apple Menu, Open System Preferences 3. Open the icon for Security & Privacy 4. Click the Open Anyway Button to the right of the statement that says: "Twine was blocked from opening because it is not from an identified developer." Open Twine When you open twine, it will walk you through an introduction to twine if you click on "tell me more". It's worth doing if you have time. When finished, click "Go to the Story List". You'll have no stories. Let's create one by clickign the + Story button Name your story In this case we'll create a story named Boston Tea Party Starting passage Every stort starts somewhere. In Twine, it starts with Untitled Passge. Double click the starting passage, or click the edit pencil icon, to open the text of the passage. Editing a Passage 1 - Enter a name for your passage. In this case, we are starting in the Boston Harbor, so we've named the passage Boston Harbor. 2- Enter the text that describes the situation. 3- In twine, any decisions are surrounded in square brackets. Write your option surrounded by double brackets, [[like this]]. 4 - Repeat for any other choices you will offer. 5 - Close the passage Story Map Now you'll notice your story has two new passages. Double click a passage to edit it. In this case we'll edit the choice for stopping and arresting the colonists. Edit the passage Edit the passage, indicating choices by surrounding them in [[double brackets]]. Editing the other passage Let's edit the other choice - do nothing. Double click the passage or click the pencil icon. Continue editing Continue editing your story and creating choices as needed. Here's an example of a story with 3 levels and 7 passages. Testing To test your story, click the Play button in the bottom right. Viewing the story Now you can read the story - choose your options to test the story. Exporting your story if you'd like to export your story, click Home in Twine, then the gear icon and publish to file to create a html file.
-
How do I get started with microbit and the bitboard kit?
Kit Contents The bitboard kits contain: 1 microbit 1 microbit bitboard 1 USB cable with USBc adapter 5 alligator clips 4 LEDs (red, green, blue, white) 1 rgb LED 1 piezo speaker 1 potentiometer 2 push button switches 1 slide switch 2 360° motors 1 servo 3 LEGO half beams Connecting the Microbit Look at the USB micro end of the cord and the USB micro port on the microbit. Be sure to fit the cable into the port correctly. Plug the USB A or C end of the cable into your computer. The Tech Training lab computers have both A and C ports available. Click "Allow" on the pop up window asking if the microbit can connect. A previous program may be running on the microbit. Don't worry about that. Coding the Microbit Go to https://makecode.microbit.org/ You may be able to log in using your Meadowbrook Google Account. Start with the tutorials You can watch the tutorial video (remember to use headphones) or go right to the blocks tutorial. The Coding Interface Microbit simulation. You can click on the buttons, click on the shake, and more to see what your program will do. Instructions. Be sure to read these carefully. Code blocks. Click on each section to see what blocks you can use. Code area. This is where you drag your code blocks to build your program. Next Button to go to the next direction. Download button. You have to connect to the microbit first by clicking on the three dots. Connecting the Microbit within the Coding Interface Click on the three dots next to the Download button Click on Connect Device Make sure your microbit is connected to your computer with the USB cable and click Next Click on the Pair button on the next screen On the pop up window, select the microbit and click on the Connect button On the next window, click on Done. Don't forget to click on the three dots at the end and select disconnect. Running your Code Once you have connected your microbit, you can at any time click on the download button, and your current code will be downloaded to the microbit.
-
How do I use the bitboard with a microbit?
If this is your first experience with the microbit, or you need a refresher on the basics, please see this set of instructions. The bitboard allows many inputs and outputs to be easily connected to the microbit. This article will show you the basic setup of the bitboard as well as how to connect and do basic programming for some of the components included in the bitboard kit. Connecting the microbit to the bitboard: Make sure the microbit is not plugged in. Gently press the microbit into the bitboard with the LED facing away from the center of the board. Connect your microbit as you did using the previous instructions. Once connected, the microbit will run the last program downloaded until you replace it with a new program. Connecting a simple LED to the bitboard: Most components have one side that is colored white. The exceptions are the slide switch with two white sides and the potentiometer with no white sections. In addition, the RGB LED has more options on the non-white side. Let's start with an LED. Pick one of the four LEDs included in your kit. Use alligator clips to connect the white side (either hole) to the ground (gnd) on either end of the bitboard. Make sure the alligator clips are going through the hole rather than across it. Connect the other side to pin 2. Open makecode.microbit.org and start a new project. Click on Advanced to reveal the pins options Click on Pins Drag the digital write pin block into your Forever command. Change the pin number to 2 Change the value to 1 Download your program (and connect your microbit) Your light should now be on. Digital vs Analog Every pin on the bitboard can be used to digital read and write. That means it can get information and send information to the pin as a 0 or 1. Think on or off. In our LED example, the digital write value of 1 turned the light on. Analog read and write allows for a range of values. Only pins 0-4 and 10 can analog read, but all can analog write. That means you can send analog information to any pin but only receive it from pins 0-4 and 10. Dimming the LED with programming Let's use analog write to dim our LED Remove the Digital Write block from your forever code and replace it with the Analog Write block located in the Pins section of the Advanced code. Set the Pin value to 2 and the write value to any number between 0 and 1023. Remember to download your new code in order for the microbit to run it. Change the number and re-download. What makes the LED brighter and dimmer? Dimming the LED with a Potentiometer Set the Analog write value to 1023 in your code and download it. This should return your LED to full brightness. Disconnect the alligator attached to pin 2 of the bitboard and instead connect it to one of the holes in the first row of contacts on the potentiometer. Take another alligator clip and attach one clip to a hole in the second row of holes on the potentiometer and the other clip to pin 2 of the bitboard. Rotate the potentiometer. All the other components Try connecting other components to the bitboard. The switches can be used to turn things on and off, and the piezo speaker can make sounds. We will get into the RGB LED and motors next session, but feel free to try to make them work if you want to.