![]() Let's write a description of the general gameplay. Your plan will keep you focused and make it more likely that you'll complete the project. In your text editor, create a new file and save it as alien_invasion.py then enter the following: alien_invasion.py import sys Planning Your Project When you're building a large project, it's important to prepare a plan before you begin to write code. Creating a Pygame Window and Responding to User Input We'll make an empty Pygame window by creating a class to represent the game. We'll also make our game respond to user input, set the background color, and load a ship image. Later, we'll draw the game elements, such as the ship and the aliens, on this win- dow. NOTE If this command doesn't work on macOS, try running the command again without the -user flag NOTE Starting the Game Project We'll begin building the game by creating an empty Pygame window. If you haven't used version control before, see Appendix D for an overview. Also, if you feel comfortable using version control, you might want to use it for this project. Be sure to save all files for the project to this folder so your import statements will work correctly. Alien Invasion spans a number of different files, so make a new alien_invasion folder on your system. Experiment with different values and settings to bet- ter understand how to refine interactions in your games. As you work through this chapter, enter and run the code to identify how each code block contrib- utes to overall gameplay. It's deeply satisfying to play a game you wrote, and writing a simple game will help you comprehend how professionals develop games. Other than python to run programs or start a terminal session, such as python3, your command will look like this: $ python3 - pip install -user pygame Making games is an ideal way to have fun while learning a language. We'll refactor a lot of code and manage file contents to organize the project and make the code efficient. While building this game, you'll also learn how to manage large pro- jects that span multiple files. In the next two chapters, you'll create a fleet of aliens to destroy, and then continue to refine the game by setting limits on the number of ships you can use and adding a scoreboard. In this chapter, you'll set up Pygame, and then create a rocket ship that moves right and left and fires bullets in response to player input. With Pygame handling tasks like drawing images to the screen, you can focus on the higher-level logic of game dynamics. 12 A SHIP THAT FIRES BULLETS Let's build a game called Alien Invasion! We'll use Pygame, a collection of fun, power- ful Python modules that manage graphics, animation, and even sound, making it easier for you to build sophisticated games.
0 Comments
Leave a Reply. |