Statistics
8
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-05-03

AuthorCarol Vorderman, Craig Steele, Claire Quigley, Daniel McCafferty, Martin Goodfellow

A visual step-by-step guide to writing code in Python. Beginners and experienced programmers can use Python to build and play computer games, from mind-bending brainteasers to crazy action games with explosive sound effects and 3-D graphics. Each chapter in Coding Games in Python shows how to construct a complete working game in simple numbered steps. The book teaches how to use freely available resources, such as PyGame Zero and Blender, to add animations, music, scrolling backgrounds, 3-D scenery, and other pieces of professional wizardry to games. After building a game, instructions show how to adapt it using secret hacks and cheat codes. Instructions are illustrated with zany Minecraft-style pixel art. Master the key concepts that programmers need to write code--not just in Python, but in all programming languages. Find out what bugs, loops, flags, strings, tuples, toggles, and turtles are. Learn how to plan and design the ultimate game--and then play it to destruction as you test and debug it. With coding theory interwoven into the instructions for building each game, learning coding is made effortless and fun.

Tags
No tags
ISBN: 1465473610
Publisher: DK Children
Publish Year: 2018
Language: 英文
Pages: 223
File Format: PDF
File Size: 29.6 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
(This page has no text content)
R codinG IN PYTHON Games US_001_half_title.indd 1 23/02/18 6:38 pm
US_002-003_Title.indd 2 23/02/18 6:38 pm R codinG IN PYTHON Games US_002-003_Title.indd 3 23/02/18 6:38 pm
R codinG IN PYTHON Games US_002-003_Title.indd 3 23/02/18 6:38 pm
DK INDIA Senior editor Bharti Bedi Project art editor Sanjay Chauhan Editor Tina Jindal Assistant art editors Rabia Ahmad, Simar Dhamija, Sonakshi Singh Jacket designer Juhi Sheth Jackets editorial coordinator Priyanka Sharma Managing jackets editor Saloni Singh DTP designer Sachin Gupta Senior DTP designer Harish Aggarwal Senior managing editor Rohan Sinha Managing art editor Sudakshina Basu Pre-production manager Balwant Singh First American Edition, 2018 Published in the United States by DK Publishing 345 Hudson Street, New York, New York 10014 Copyright © 2018 Dorling Kindersley Limited DK, a Division of Penguin Random House LLC 18 19 20 21 22 10 9 8 7 6 5 4 3 2 1 001–309872–July/2018 All rights reserved. Without limiting the rights under the copyright reserved above, no part of this publication may be reproduced, stored in or introduced into a retrieval system, or transmitted, in any form, or by any means (electronic, mechanical, photocopying, recording, or otherwise), without the prior written permission of the copyright owner. Published in Great Britain by Dorling Kindersley Limited A catalog record for this book is available from the Library of Congress. ISBN: 978-1-4654-7361-5 Printed in China A WORLD OF IDEAS: SEE ALL THERE IS TO KNOW www.dk.com DK UK Project editor Ben Ffrancon Davies Senior art editor Sunita Gahir Consultant editor Craig Steele Jacket design development manager Sophia MTT Jacket editor Claire Gell Producer, pre-production Gillian Reid Senior Producer Alex Bell US editors Jill Hamilton, Kayla Dugger Managing editor Lisa Gillespie Managing art editor Owen Peyton Jones Publisher Andrew Macintyre Associate publishing director Liz Wheeler Art director Karen Self Design director Phil Ormerod Publishing director Jonathan Metcalf US_004-005_imprint.indd 4 22/02/18 1:40 pm CAROL VORDERMAN MBE is one of Britain’s best-loved TV presenters and is renowned for her mathematical skills. She has hosted numerous TV shows on science and technology, from Tomorrow’s World to How 2, and was co-host of Channel 4’s Countdown for 26 years. A Cambridge University engineering graduate, she has a passion for communicating science and technology and is particularly interested in coding. CRAIG STEELE is a specialist in computing science education who helps people develop digital skills in a fun and creative environment. He is a founder of CoderDojo in Scotland, which runs free coding clubs for young people. Craig has run digital workshops with the Raspberry Pi Foundation, Glasgow Science Centre, Glasgow School of Art, BAFTA, and the BBC micro:bit project. Craig’s first computer was a ZX Spectrum. DR. CLAIRE QUIGLEY studied computing science at Glasgow University, where she obtained BSc and PhD degrees. She has worked in the Computer Laboratory at Cambridge University and Glasgow Science Centre, and is currently working on a project to develop a music and technology resource for primary schools in Edinburgh. She is a mentor at CoderDojo Scotland. DANIEL McCAFFERTY holds a degree in computer science from the University of Strathclyde. He has worked as a software engineer for companies big and small in industries from banking to broadcasting. Daniel lives in Glasgow with his wife and two children, and when not teaching young people to code, he enjoys bicycling and spending time with his family. DR. MARTIN GOODFELLOW is a teaching associate in the Computer and Information Sciences department at the University of Strathclyde. He has also developed educational computer science content and workshops for other organizations in the UK and China, including CoderDojo Scotland, Glasgow Life, Codemao, and the BBC. He is currently the Scottish Ambassador for National Coding Week. US_004-005_imprint.indd 5 22/02/18 1:40 pm
CAROL VORDERMAN MBE is one of Britain’s best-loved TV presenters and is renowned for her mathematical skills. She has hosted numerous TV shows on science and technology, from Tomorrow’s World to How 2, and was co-host of Channel 4’s Countdown for 26 years. A Cambridge University engineering graduate, she has a passion for communicating science and technology and is particularly interested in coding. CRAIG STEELE is a specialist in computing science education who helps people develop digital skills in a fun and creative environment. He is a founder of CoderDojo in Scotland, which runs free coding clubs for young people. Craig has run digital workshops with the Raspberry Pi Foundation, Glasgow Science Centre, Glasgow School of Art, BAFTA, and the BBC micro:bit project. Craig’s first computer was a ZX Spectrum. DR. CLAIRE QUIGLEY studied computing science at Glasgow University, where she obtained BSc and PhD degrees. She has worked in the Computer Laboratory at Cambridge University and Glasgow Science Centre, and is currently working on a project to develop a music and technology resource for primary schools in Edinburgh. She is a mentor at CoderDojo Scotland. DANIEL McCAFFERTY holds a degree in computer science from the University of Strathclyde. He has worked as a software engineer for companies big and small in industries from banking to broadcasting. Daniel lives in Glasgow with his wife and two children, and when not teaching young people to code, he enjoys bicycling and spending time with his family. DR. MARTIN GOODFELLOW is a teaching associate in the Computer and Information Sciences department at the University of Strathclyde. He has also developed educational computer science content and workshops for other organizations in the UK and China, including CoderDojo Scotland, Glasgow Life, Codemao, and the BBC. He is currently the Scottish Ambassador for National Coding Week. US_004-005_imprint.indd 5 22/02/18 1:40 pm
GETTING STARTED 12 What is Python? 14 Gaming in Python 16 Installing Python 18 Installing Pygame Zero 20 Using IDLE 22 Your first program LEARNING THE BASICS 28 Creating variables 32 Making decisions 36 Playing with loops 40 Functions 44 Fixing bugs SHOOT THE FRUIT 50 How to build Shoot the Fruit Contents COIN COLLECTOR 60 How to build Coin Collector FOLLOW THE NUMBERS 70 How to build Follow the Numbers RED ALERT 82 How to build Red Alert 8 F O R E W O R D Score: 0 US_006-007_Contents.indd 6 22/02/18 12:23 pm HAPPY GARDEN 156 How to build Happy Garden SLEEPING DRAGONS 178 How to build Sleeping Dragons REFERENCE 198 Project reference 220 Glossary 222 Index 224 Acknowledgments BIG QUIZ 100 How to build Big Quiz BALLOON FLIGHT 118 How to build Balloon Flight DANCE CHALLENGE 138 How to build Dance Challenge Garden happy for: 16 seconds 26 Score: 0 Score: 0 7 London Berlin Paris Tokyo What is the capital of France? US_006-007_Contents.indd 7 22/02/18 12:23 pm
HAPPY GARDEN 156 How to build Happy Garden SLEEPING DRAGONS 178 How to build Sleeping Dragons REFERENCE 198 Project reference 220 Glossary 222 Index 224 Acknowledgments BIG QUIZ 100 How to build Big Quiz BALLOON FLIGHT 118 How to build Balloon Flight DANCE CHALLENGE 138 How to build Dance Challenge Garden happy for: 16 seconds 26 Score: 0 Score: 0 7 London Berlin Paris Tokyo What is the capital of France? US_006-007_Contents.indd 7 22/02/18 12:23 pm
Foreword Computer programmers are the unsung heroes of the modern world. From smartphones to laptops, traffic systems to bank cards, their hard work touches almost every aspect of our lives. Behind each technological advance is a team of creative coders. Over the past 30 years, computer games have become one of the most exciting and popular areas of the entertainment industry to work in. Becoming a game programmer takes creative flair to help create the story, graphics, music, and characters you need for your games, and the technical know-how to bring them to life. Who knows? This book may be the very first step on your journey from gamer to game maker. Learning to code isn’t just for people who want to be professional programmers, though. Coding skills are useful in lots of different jobs that may seem to have nothing to do with computers at first. Programming expertise is essential to subject areas as diverse as science, business, art, and music. This book uses a programming language called Python®, a fairly simple text-based language, and is perfect for beginners, or as a step up from Scratch™. However, unlike Scratch, it was not created especially to teach coding. Python is as popular with budding coders as it is with professionals, and is one of the most widely used professional programming languages in the world. It pops up in banking, medicine, animation, and even space exploration. The best way to learn any new language is to get immersed in it, and programming languages are no different. Building your own computer games is a fun way to combine theory and practice. If you’re a brand-new coder, start off with the basics at the US_008-009_Foreword.indd 8 22/02/18 12:23 pm beginning of this book before moving on to the more complex games as the book progresses. By following the step-by-step guides, you’ll find out how professional coders think when they’re building a computer game. Follow those steps carefully and you’ll have your own games up and running in no time. Then, if you really want to push yourself, you can try tweaking the code to make your games unique. Everybody, whether a beginner or a pro, makes mistakes. Nothing frustrates a coder more than the bugs that manage to creep into their programs. If something goes wrong in one of your games, go back over your code and check it all carefully. There are hints and tips throughout the book that will help you do this. Most importantly, don’t get disheartened—finding and fixing errors in your code is all part of being a programmer. The more practice you get, the fewer bugs your code will contain, and the quicker you’ll catch the little ones that still appear. Most importantly, have fun! Once you’ve completed the games, you can show them off to your friends and family—they’ll be amazed by what you’ve managed to make. This book is packed with games to suit every audience, and we hope you enjoy building and playing them as much as we enjoyed creating them for you. Have fun coding! US_008-009_Foreword.indd 9 23/02/18 2:31 pm
beginning of this book before moving on to the more complex games as the book progresses. By following the step-by-step guides, you’ll find out how professional coders think when they’re building a computer game. Follow those steps carefully and you’ll have your own games up and running in no time. Then, if you really want to push yourself, you can try tweaking the code to make your games unique. Everybody, whether a beginner or a pro, makes mistakes. Nothing frustrates a coder more than the bugs that manage to creep into their programs. If something goes wrong in one of your games, go back over your code and check it all carefully. There are hints and tips throughout the book that will help you do this. Most importantly, don’t get disheartened—finding and fixing errors in your code is all part of being a programmer. The more practice you get, the fewer bugs your code will contain, and the quicker you’ll catch the little ones that still appear. Most importantly, have fun! Once you’ve completed the games, you can show them off to your friends and family—they’ll be amazed by what you’ve managed to make. This book is packed with games to suit every audience, and we hope you enjoy building and playing them as much as we enjoyed creating them for you. Have fun coding! US_008-009_Foreword.indd 9 23/02/18 2:31 pm
US_010-011_Chapter_opener_1.indd 10 22/02/18 1:40 pm Getting started US_010-011_Chapter_opener_1.indd 11 22/02/18 1:40 pm
Getting started US_010-011_Chapter_opener_1.indd 11 22/02/18 1:40 pm
12 G E T T I N G S T A R T E D What is Python? Computers need step-by-step instructions to perform different tasks. A set of instructions, or code, can be written in different programming languages. Python is one of the most popular programming languages. Why Python? Python is a powerful programming language that you can use to code simple programs quickly. It’s not too hard to learn and it’s great for building apps and games. Here are some of the reasons why Python is such a great tool for programmers. 1+1=2 △ Packed with tools Python comes with everything you need to start coding right away, so programmers say it comes with “batteries included.” It contains lots of prewritten code, called the Standard Library, that you can use in your programs. Python is great! I can take it anywhere. △ Portable The same Python code will work on PCs, Macs, Linux machines, and Raspberry Pi computers. The programs act in a similar way on each platform, so games created with Python can be played on lots of machines all around the world. △ Lots of help Python’s website is packed with support materials to help you learn how to use it. It has a guide to getting started, several pieces of sample code, and a reference section to help you understand the code. △ Easy to understand Unlike some other programming languages, Python doesn’t use complicated symbols. You type the code using a mixture of English words, characters, and numbers, so it’s easy to read and write—just like a book. ◁ Diverse applications Python is used to build systems and tools for lots of interesting tasks in different industries, such as banking, healthcare, the space industry, education, and many others. Better get started! US_012_013_what_is_python.indd 12 22/02/18 12:23 pm 13W H A T I S P Y T H O N ? From Scratch to Python Scratch is a visual language, whereas Python is text based. If you’ve used Scratch, you will recognize some of the features and ideas in Python. The two languages might look different, but lots of the elements that are used in Scratch are also used in Python. Goodbye! △ Set a condition with Scratch The “if-then-else” block lets you choose which part of the script to run depending on whether the condition is True or False. when clicked say Hello World! Hello World! This block displays the message in a speech bubble. △ Print in Scratch In Scratch, the “say” block is used to display a message on the screen. Type your message here. The message appears on the screen like this. △ Print in Python In Python, the “print” command displays a message on the screen. say Hello! say Goodbye! a = 2if else then Goodbye! ?Hello! △ Set a condition with Python In Python, “if-then-else” commands work exactly the same way, but they don’t use the word “then.” This command is executed if the condition after if is False. This message is displayed if the answer is False. if a == 2: print("Hello!") else: print("Goodbye!") print("Hello World!") Hello World! This command is executed if the condition after if is True. This message is displayed if the answer is True. This is the end of the “if-then-else” block. US_012_013_what_is_python.indd 13 22/02/18 12:23 pm
13W H A T I S P Y T H O N ? From Scratch to Python Scratch is a visual language, whereas Python is text based. If you’ve used Scratch, you will recognize some of the features and ideas in Python. The two languages might look different, but lots of the elements that are used in Scratch are also used in Python. Goodbye! △ Set a condition with Scratch The “if-then-else” block lets you choose which part of the script to run depending on whether the condition is True or False. when clicked say Hello World! Hello World! This block displays the message in a speech bubble. △ Print in Scratch In Scratch, the “say” block is used to display a message on the screen. Type your message here. The message appears on the screen like this. △ Print in Python In Python, the “print” command displays a message on the screen. say Hello! say Goodbye! a = 2if else then Goodbye! ?Hello! △ Set a condition with Python In Python, “if-then-else” commands work exactly the same way, but they don’t use the word “then.” This command is executed if the condition after if is False. This message is displayed if the answer is False. if a == 2: print("Hello!") else: print("Goodbye!") print("Hello World!") Hello World! This command is executed if the condition after if is True. This message is displayed if the answer is True. This is the end of the “if-then-else” block. US_012_013_what_is_python.indd 13 22/02/18 12:23 pm
14 G E T T I N G S T A R T E D Gaming in Python Video games are computer programs that contain a bunch of instructions. Python can be used to build lots of different types of games. With Python, there’s something for every gamer! Types of games There are lots of different categories, or genres, of computer games. These range from simple one-button games to more complex strategy ones. Which genre would you like to create first? △ Platform Platform games, such as racing games, create the illusion of speed by making the background scroll past the player’s viewpoint. The gameplay generally involves moving around obstacles or jumping over them. ▷ One button With Python, you can build fun, action-packed games that only need one button to be played. These games are so addictive, you’ll want to play them over and over again. ◁ Puzzles Puzzles are a great way to exercise your brain or test someone’s general knowledge. They come in all shapes and sizes, from jigsaw puzzles to word and number games. ◁ Strategy A strategy game is all about decisions. You need to plan ahead and make the right choices to win. △ Multiplayer Some games you play by yourself, but others let you compete against other players. You can use Python to build multiplayer games and challenge your friends. US_014-015_Gaming_in_python.indd 14 22/02/18 12:24 pm 15G A M I N G I N P Y T H O N Python modules Python has bundles of code called “modules” that help you complete common coding tasks. You can use these modules by importing them into your programs. Here are some Python modules that you might find useful. ▽ Pygame Pygame is designed for writing games in Python. With this module, you can easily add and control game characters, update scores and timers, use special animations and graphics, and use gamepads and joysticks with your games. It is not a built-in Python module, so it needs to be installed separately. ◁ Pygame Zero Pygame Zero is a great module for beginner game programmers. It’s a simplified version of Pygame, which makes it even easier to get started by hiding some of Pygame’s more complicated features. It comes with several tools that are useful for beginners, but it’s also powerful enough to build some impressive games. ▷ Random This module can pick a random number or shuffle a list into a random order. It is great for adding an element of chance to a game. Use it when you want to simulate rolling dice or when choosing a random enemy for the player to face. ◁ Tkinter This tool is used to build simple graphics in games to create Graphical User Interfaces (GUIs, pronounced “goo-eys”) that let users interact with, and control, Python programs. ◁ Time This module provides tools to work with time and dates in a program. For example, you might need to calculate how many seconds have passed since a game started. ▷ Math Math is a standard Python module that can be used to perform simple calculations in games. However, you might need to use other modules for trickier calculations. E X P E R T T I P S Downloading modules Python comes with several built-in modules for developing games, such as Pyglet. But some other modules, like Pygame, have to be downloaded separately. US_014-015_Gaming_in_python.indd 15 22/02/18 12:24 pm
15G A M I N G I N P Y T H O N Python modules Python has bundles of code called “modules” that help you complete common coding tasks. You can use these modules by importing them into your programs. Here are some Python modules that you might find useful. ▽ Pygame Pygame is designed for writing games in Python. With this module, you can easily add and control game characters, update scores and timers, use special animations and graphics, and use gamepads and joysticks with your games. It is not a built-in Python module, so it needs to be installed separately. ◁ Pygame Zero Pygame Zero is a great module for beginner game programmers. It’s a simplified version of Pygame, which makes it even easier to get started by hiding some of Pygame’s more complicated features. It comes with several tools that are useful for beginners, but it’s also powerful enough to build some impressive games. ▷ Random This module can pick a random number or shuffle a list into a random order. It is great for adding an element of chance to a game. Use it when you want to simulate rolling dice or when choosing a random enemy for the player to face. ◁ Tkinter This tool is used to build simple graphics in games to create Graphical User Interfaces (GUIs, pronounced “goo-eys”) that let users interact with, and control, Python programs. ◁ Time This module provides tools to work with time and dates in a program. For example, you might need to calculate how many seconds have passed since a game started. ▷ Math Math is a standard Python module that can be used to perform simple calculations in games. However, you might need to use other modules for trickier calculations. E X P E R T T I P S Downloading modules Python comes with several built-in modules for developing games, such as Pyglet. But some other modules, like Pygame, have to be downloaded separately. US_014-015_Gaming_in_python.indd 15 22/02/18 12:24 pm
16 G E T T I N G S T A R T E D Installing Python The games in this book use Python 3. It’s free, and you can download it from the Python website. Follow the instructions that match your computer. Never install any program unless you have the computer owner’s permission. Installing Python on Windows First you need to find out if your computer uses the 32-bit or 64-bit version of Windows. Go to the Start menu, then Computer, Properties, and choose System if the option appears. Click the installer. Install Python Open the installer file, then click Custom Installation, then Next until you get to Advanced Options. Leave the checked boxes as they are, but make sure “Install for all users” and “Add Python to environment variables” are also checked. Then click Install and Next at each prompt. 2 Start IDLE Once the installation process is complete, open IDLE by searching for it or going to the Start menu, choosing All Apps, then selecting IDLE. A window like the one below should appear. 3 Download Python Go to www.python.org and click on Downloads. Click on the latest version of Python for Windows. It should start with the number 3. Select executable installer from the different installer options that appear. Use this installer if you have a 32-bit version of Windows. Use this installer if you have a 64-bit version of Windows. The version number might not be exactly the same as this one—just make sure it has 3 at the beginning. • Python 3.6.2 - 2017-05-15 • Windows x86 executable installer • Windows x86-64 executable installer 1 Python 3.6.2 (v3.6.2:5fd3365926, Aug 15 2017, 00:45:10) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> Python 3.6.2 Shell IDLE File Edit Shell Debug Window Help L I N G O IDLE When you install Python 3, you will also get another free program called IDLE (short for Integrated Development Environment). Designed for beginners, IDLE includes a basic text editor that allows you to write and edit Python code. Z Z Z US_016-017_Installing_python.indd 16 23/02/18 3:52 pm I N S T A L L I N G P Y T H O N 17 Start IDLE Once the installation is complete, check that it was successful by opening the IDLE program. Search for it in Spotlight or select the Applications folder, then the Python folder, and double-click IDLE. A window like this should appear. Download Python Go to www.python.org and click on Downloads. Click on the version of Python 3 that matches your operating system. The “Python.pkg” file will download to your Mac automatically. Install Python Double-click the “.pkg” file in the Downloads folder to start the installation. Select Continue and then Install to accept the default settings. Installing Python on a Mac Before you install Python 3 on a Mac, you need to check which operating system your Mac uses. To do this, click the Apple icon in the top left of the screen and choose About This Mac from the drop-down menu. The version number might not be exactly the same as this one—just make sure it has a 3 at the beginning. Click the package to run the installer. • Python 3.6.2 - 2017-08-15 • Download macOS X 64-bit/32-bit installer Python 3.6.2 (v3.6.2:5fd3365926, Aug 15 2017, 13:38:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> Python 3.6.2 Shell IDLE File Edit Shell Debug Window Help 1 2 3 I better get going! Where should I plug this in? E X P E R T T I P S Raspberry Pi If you’re using a Raspberry Pi computer, then you can skip the download step because Python 2 and Python 3 come preinstalled on the machine. Remember this book uses Python 3, so make sure you open the right version. You’ll find Python 3 in the Applications menu on your Raspberry Pi. Open it now and check that it works. US_016-017_Installing_python.indd 17 22/02/18 12:24 pm
I N S T A L L I N G P Y T H O N 17 Start IDLE Once the installation is complete, check that it was successful by opening the IDLE program. Search for it in Spotlight or select the Applications folder, then the Python folder, and double-click IDLE. A window like this should appear. Download Python Go to www.python.org and click on Downloads. Click on the version of Python 3 that matches your operating system. The “Python.pkg” file will download to your Mac automatically. Install Python Double-click the “.pkg” file in the Downloads folder to start the installation. Select Continue and then Install to accept the default settings. Installing Python on a Mac Before you install Python 3 on a Mac, you need to check which operating system your Mac uses. To do this, click the Apple icon in the top left of the screen and choose About This Mac from the drop-down menu. The version number might not be exactly the same as this one—just make sure it has a 3 at the beginning. Click the package to run the installer. • Python 3.6.2 - 2017-08-15 • Download macOS X 64-bit/32-bit installer Python 3.6.2 (v3.6.2:5fd3365926, Aug 15 2017, 13:38:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> Python 3.6.2 Shell IDLE File Edit Shell Debug Window Help 1 2 3 I better get going! Where should I plug this in? E X P E R T T I P S Raspberry Pi If you’re using a Raspberry Pi computer, then you can skip the download step because Python 2 and Python 3 come preinstalled on the machine. Remember this book uses Python 3, so make sure you open the right version. You’ll find Python 3 in the Applications menu on your Raspberry Pi. Open it now and check that it works. US_016-017_Installing_python.indd 17 22/02/18 12:24 pm
G E T T I N G S T A R T E D18 Installing Pygame Zero Now it’s time to add some extra tools to help you build great games. In this book, you’ll need two additional modules—Pygame and Pygame Zero. These are not included with Python, so you need to install them separately. E X P E R T T I P S Admin access Make sure you're signed into your computer as an admin; otherwise, the system won't let you install things properly. Always ask permission before installing new software on someone's computer. Installing Pygame Zero on Windows Follow these steps to install the latest versions of Pygame and Pygame Zero on your Windows computer. Your machine needs to be connected to the Internet to complete some of these steps. Install a package manager A package manager called “pip” should come with Python when you install it. It’s a tool that makes it easier to install Pygame Zero and other Python modules. Type this command into the Command Prompt and press Enter—it will check if pip is on your computer and install it if it is not. 2 Install Pygame Zero Finally, type this command. When you press Enter, this will install Pygame Zero, also known as pgzero for short. 4 pip install pgzero Install Pygame Once the package manager is installed, type the following command and press Enter. This uses pip to install Pygame. 3 pip install pygame Open the Command Prompt Click Start. Scroll down and open the Windows System folder. Click Command Prompt. If you can’t find it, try searching for it. You’ll need to type in some commands and press Enter to run each one. Make sure you spell everything correctly and put spaces in the right places or it won’t work. 1 python -m pip install -U pip Command Prompt Look out for this thumbnail in the menu. A C C E S S D E N I E D c:\ US_018-019_Installing_pygame_zero.indd 18 22/02/18 12:24 pm I N S T A L L I N G P Y G A M E Z E R O 19 Installing Pygame Zero on a Mac Follow these steps to install the latest versions of Pygame and Pygame Zero on your Mac. Your machine needs to be connected to the Internet to complete some of these steps. Open Terminal You’ll need to use the Terminal app to install the modules. You can find it in your Applications folder, or you can search for it with Spotlight. Follow the steps below, making sure all the spellings are correct and the spaces are in the right place. Install Pygame Now it’s time to install Pygame. Type in this command and press Enter. 5 Install other tools Type in this command next and press Enter. It uses Homebrew to install some tools that will be needed by Pygame Zero. 4 1 6 Install Pygame Zero Finally, this last command will install Pygame Zero. Check that Python 3 is installed Homebrew will check if Python 3 is already installed on your Mac and will install it if it’s not there. Even though you’ve already installed Python, it’s worth checking just to be sure. 3 Install a package manager Homebrew is a package manager tool that makes it easier to install Pygame Zero and other Python modules. Type in the command at right and press Enter to install Homebrew. It might ask you to enter your password again, and it will take a short while to install, so don't panic if nothing happens right away. 2 brew install sdl sdl_mixer sdl_sound sdl_ttf pip3 install pygame Don’t put a space before 3. This should fit on one line when you type it in. Type this line carefully in the Terminal window and check for any spelling errors and extra spaces. brew install python3 pip3 install pgzero E X P E R T T I P S Having trouble? Installing these modules might be a bit tricky at first. If you’re having trouble, you can get the most up-to-date installation instructions on the Pygame Zero website: https://pygame-zero.readthedocs.io This is what the Terminal app thumbnail looks like. Rabiahma – bash – 80x24 Last login: Thu Sep 14 11:22:51 on ttys000 LC-0926:~ rzvz ruby -e "$(curl -fsSL https://raw.git hubusercontent.com/Homebrew/install/master/install)" I N S T A L L I N G >_ US_018-019_Installing_pygame_zero.indd 19 22/02/18 12:24 pm