Skip to main content

test

Page 1

Chapter 2: A Vivid Description Printing Out a Scene Description Open up your tool of choice (see Chapter 1 for download assistance) and stretch your fingers, it’s time to embark on a journey of creation. First thing you’ll want to do, in order to make sure your environment is properly set up is type this out:

>>> print("Hello, World!") Figure 2.01

If everything is in order, this should display to the screen (wherever your output is going) the words

Hello, World! If it doesn’t, you’ll want to figure out what’s wrong before moving forward. What, you might be asking, are we going to do with the ability to display these very specific words to the screen? We can actually write any characters we want, arranged in any order. Such is the benefit of programming: almost complete freedom. Freedom to, say, print out a description of a scene for the player to read. In order to display something else, we’ll have to see within Figure 2.01 what to change. Taking the print statement, we are able to replace those words encased by “” with what we want to print. For instance:

>>> print("You wake up in a cave, to your right is a door. It’s dark brown wood, moldy and surrounded by a rusted metal outline.") Figure 2.02

If you type this out, the interpreter will respond to you by displaying that exact description from “you wake up...” to “...outline”. So you’ve probably guessed the format here, you can use print("_") to display whatever you desire, just typing the characters inside of the quotations. The print() is what we call a function, it’s named “print” and it performs the specific task of displaying whatever we need to the screen. The quotations around the words simply means “these are characters” and not, say, a variable. We call this printing a string, or a grouping of characters. We’ll talk more about these concepts later in Chapter 3. Main Ideas:


● You can print out any words you’d like with print(""). Practice: ● 2.1 - Print out a text description of a scene, maybe something with a forest or a door. Remember your quotes (“”).

Using Timing for Better Gameplay

Taking a Breath Between Lines When you open a game, is the dialogue all spat out in a single, scary paragraph? No! You’re treated to a nice and user-friendly display of sentences or phrases, one after another, in a digestible duration. We can do this in our game as well, telling our code to wait in between messages so that the player has time to read. Let’s see an example in a greeting:

1 2 3 4

from time import sleep print("Hello,") sleep(1) print("how is your day?") Figure 2.03

This code greets you, waits a second, and then asks how your day is. How nice, but how’s it work? First, in line 1, we made the “sleep” function usable to us. In Python, and in other coding languages, not all the commands are automatically available for use instantly. If you specifically want to make a visual game, or generate a random number, or wait some time in between lines of code, you’ll have to download that functionality into your program before being able to use it. Line 2 is a print statement, just like we went over in the previous section, that this time prints out “Hello,”. Line 3 uses our downloaded command, which is a function just like print that takes in some argument and performs an operation. This function, instead of displaying some given text to the screen, waits a particular amount of time before executing the next line of code. I wrote in ‘1’ and therefore waited one second. If I had written “sleep(2)”, the code would wait 2 seconds before going to line 4, where another print statement is used.


Character-by-character Printing When games display text, they’ll sometimes have time between both paragraphs and individual characters. In Python, this is possible to do but requires some more knowledge to fully be comfortable using. I recommend you see this exercise as a measurement of your learning. Flip to Bonus Section 1 when you think you’re ready. Main Ideas: ● First, by importing the time library with from time import sleep, you can then use

sleep(_) with any number of seconds inside the function. Your program will, reaching that line, wait that number of seconds before moving on. Practice: ● 2.2- Greet the user, with multiple messages, giving them time in between to read each before moving on to the next.


Turn static files into dynamic content formats.

Create a flipbook
test by mechele7 - Issuu