• No results found

The breakdown

In document Raspberry Pi for Beginners 7th Edition (Page 134-138)

07

Here we define the actual beginning of the code, with the function we’ve called ‘start’. It’s quite simple, printing our greeting to the player and then starting a while loop that will allow us to keep playing the game as many times as we wish. The pass statement allows the while loop to stop once we’ve finished, and could be used to perform a number of other tasks if so wished. If we do stop playing the game, the score function is then called upon – we’ll go over what that does when we get to it.

09

We start the move function off by putting it into a while loop. The whole point of move is to obtain an integer between one and three from the player, so the while loop allows us to account for the player making an unsupported entry. Next, we are setting the player variable to be created from the player’s input with raw_input. We’ve also printed instruction text to go along with it. The ‘\n’

we’ve used in the text adds a line break; this way, the instructions appear as a list.

10

The try statement is used to clean up code and handle errors or other exceptions. We parse what the player entered by turning it into an integer using int(). We use the if statement to check if it is either 1, 2, or 3 – if it is, move returns this value back up to the game function. If it throws up a ValueError, we use except to do nothing. It prints an error message and the while loop starts again. This will happen until an acceptable move is made.

08

We’ve kept the game function fairly simple so we can break down each step a bit more easily in the code. This is called upon from the start function, and first of all determines the player move by calling upon the move function below. Once that’s sorted, it sets the computer move. It uses the random module’s randint function to get an integer between one and three (1, 3). It then passes the player and computer move, stored as integers, onto the result function which we use to find the outcome.

07

08

09

10

The code in action

11

The result function only takes the variables player and computer for this task, which is why we set that in result(player, computer). We’re starting off by having a countdown to the result.

The printed numbers are self-explanatory, but we’ve also thrown in sleep from the time module we imported. Sleep pauses the execution of the code by the number of seconds in the brackets.

In this example, we’ve put a one-second pause

from the names we set earlier on, and then to insert that where {0} is.

13

Here we’re simply calling the scores that we set earlier. Using the global function allows for the variable to be changed and used outside of the variable, especially after we’ve appended a number to one of the scores of either the player or computer.

15

If it’s not a tie, we need to keep checking, as it could still be a win or a loss. Within the else, we start another if statement. Here, we use the rules list from earlier to see if the losing move to the player’s move is the same as the computer’s. If that is the case, we will print the message saying so, and add one to the player_score variable from before.

between counts, then half a second after that to show the results.

12

To print out what the computer threw, we’re using string.format(). The {0} in the printed text is where we’re inserting the move, which we have previously defined as numbers. Using names[computer], we’re telling the code to look up what the text version of the move is called

14

The way we’re checking the result is basically through a process of elimination. Our first check is to see if the move the player and computer used were the same, which is the simplest part.

We put it in an if statement so that if it’s true, this particular section of the code ends here. It then prints our tie message and goes back to the game function for the next step.

16

If we get to this point, the player has lost. We print the losing message, give the computer a point and it immediately ends the result function, returning to the game function.

11

12 13 14 15

16

The code in action

136 Raspberry Pi for Beginners

Programming

17

The next section of game calls upon a play_again function. Like the move function, this requires human input, asking the player if they would like to play again via a text message with raw_input, with the simple ‘y/n’ response.

18

Giving users an option of y/n like we have should expect a response in kind. The if

19

If we don’t get an expected response – that is either a y or n – we will assume that the player does not want to play again. If this is the case, we will print a goodbye message, and that will end this function. This will also cause the game function to move onto the next section and not restart the game again. This ensures that the game does not run without an end point.

statement checks to see if any of our defined positive responses have been entered. As Python doesn’t differentiate between upper or lower case, we’ve made sure that it accepts both y and Y. If this is the case, it returns a positive response to game, which will start it again. Entering n or N will return a negative response to the game and so it will not restart.

20

Going back to the start function, after game finishes we move onto the results.

This section calls the scores, which are integers, and then prints them individually after the names of the players. This is the end of the script, as far as the player is concerned. Currently, the code won’t permanently save the scores, but you can have Python write it to a file to keep if you wish.

21

The final part allows for the script to be used in two ways. Firstly, we can execute it in the command line and it will work fine. Secondly, we can import this into another Python script, perhaps if you wanted to add it as a game to a collection.

This way, it won’t execute the code when being imported.

IF also has the ELIF (else if) operator, which can be used in place of the second IF statement we employed. It’s usually used to keep code clean, but performs the same function.

ELIF

17 18 19

20

21

The code in action

W

e’re going to be remaking the classic game Pong. To do this, we’ll be using a Python module called Pygame, which is great, as it allows the programmer to create 2D games without having to worry about things such as rendering the graphics in too much detail. The main portion of the code will be that which makes up the game’s structure and logic.

Pong is an ideal game through which to

introduce the principles behind game development, as it is fairly simple, which makes it much easier to understand what’s going on. This also means that it requires less code, making it ideal for those just starting out in programming.

In document Raspberry Pi for Beginners 7th Edition (Page 134-138)

Related documents