Beginning Game Programming for Teens with Python. If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for visiting! Learn how to make a simple game with Python! This is a post by Tutorial Team Member Julian Meyer, a 1. You can find him on Google+ and Twitter. Have you ever wondered how video games are created? It’s not as complicated as you might think! In this tutorial, you’ll create a simple game called Bunnies and Badgers, where the hero, the bunny, has to defend a castle against an attacking horde of badgers. We chose Python for this tutorial because it’s a simple language to start out with, and is fun and easy to learn. Program Arcade Games With Python And Pygame < Previous. Next > Chapter 5: Introduction to Graphics. First, the program creates a variable that holds information about the font to be used, such as what typeface and. Welcome to the first pygame. Once you have imported pygame the first thing you need to do. Writing my first python script. Now I didn’t want to go too crazy with my first script so I thought I would start off with the simplest program that. Creating a Game in Python Using PyGame – Introduction; What am I. If you are new to Python, before you begin check out this book: Think Python: How to Think Like a Computer Scientist. That should get you up to speed. Then dive back here and get ready – there. Keep reading to jump into the fray! Getting Started: Installing Python. If you want to try this tutorial on a Windows PC, you need to install Python. Make sure you grab the 2. NOT the 3. 3. 0 version! After running the installer, you should have IDLE in your All Programs folder in your start menu. Launch IDLE to get started. If you are on a Mac, you already have Python installed! Just open Terminal (/Applications/Utilities/Terminal. Enter. Note: If you install Python from python. Py. Game working), then you’ll also have access to IDLE on the Mac as well. It should be in the “/Applications/Python 2. If you did it correctly, you should see something like this: Python 2. Apr 9 2. 01. 2, 2. You have just written your first Python program! Now that you know Python is working correctly, you need to install Py. Game in order to write a game using Python. Py. Game is a Python library that makes writing games a lot easier! It provides functionality such as image handling and sound playback that you can easily incorporate into your game. Go here and download the Py. Game installer appropriate for your system. Make sure you download a Python 2. Note: The Py. Game installer from the link above will not work with the default Python from Apple that is installed on a Mac. This is my first game, and I've run up on some problems. I have all the files I need. The pygame window opens up like it is going to start the game, then it just gives me the beach ball of death! 2016-09-13 Python 3.6.0b1 is the first of four planned beta releases of. 2016-08-16 Python 3.6.0a4 has been released. 3.6.0a4 is the last of four planned. The mission of the Python Software Foundation is to promote. You’ll need to download Python from python. Py. Game. Or, you can install both Python and Py. Game via Mac. Ports. To verify that you have Py. Game installed properly, open IDLE or run Python via the Terminal and type in import pygame at the Python prompt. If this doesn’t result in any output, then you’re good. If, on the other hand, it outputs an error like what’s shown below, then Py. Game is not installed. Python 2. 7. 2 (default, Jun 2. Running Python Code from File. While you can run short bits of Python code at the Python prompt, if you want to work on a bigger program (like a game), you probably want to save your code to a file so that you don’t have to type it in over and over again. There are several ways to run a Python program as a file. One way is to use a plain text editor like Notepad (Windows), or Text. Edit (Mac). Open a new text file, type in your Python code (like print 1+1). Then save it as XXX. The XXX can be any descriptive file name). Then on Windows, double- click this file to run it. On Mac, open Terminal and type python, then drag the file that you saved onto the Terminal window and press Enter. The other way is to type in your code using the IDLE editor, which is what you. To run idle, simply type idle from Terminal. Then choose File\New Window from the IDLE menu and you should have a text editor window where you can type in Python code. You can save your code changes via File\Save and even run the code via Run\Run Module (F5). Do note that the Run menu is only available if you have a file open in an editor window. Adding the Game Resources. You are almost ready to create your game. But what’s a game without some great graphics and sound effects? I’ve put together all the graphics and sound effects you’ll need for your game into a ZIP archive. You can download it here. Once you’ve downloaded the file, create a folder for your game on your hard disk and extract the resources folder into that folder so that your game folder has a subfolder named resources, with the various resources grouped in additional folders inside it, like this: You are now ready to begin creating Bunnies and Badgers. Type the following code into the editor window: # 1 - Import libraryimport pygame. Initialize the game. This allows you to use functions from the library in your program. Initialize Py. Game and set up the display window. Load the image that you will use for the bunny. Keep looping over the following indented code. So proper indentation is very important in Python . However, at least on Mac OS, the game would hang on exit unless pygame. If you run the code now (via Run\Run Module in the Idle menu), you should see a screen similar to the one below: w. But the game looks scary and lonely with the bunny just standing there on a black background. Time to prettify things a little bit. This can be done with a couple more screen. At the end of section #3, after loading the player image, add the following code: grass = pygame. Now they have to be drawn on screen. But if you check the grass image, you will notice that it won. This means you have to tile the grass over the screen area to cover it completely. Add the following code to game. Then, within that for loop, it loops through y and draws the grass at the x and y values generated by the for loops. The next couple of lines just draw the castles on the screen. If you run the program now, you should get something like this: Much better – this is starting to look good! You can do this simply by making an array of key states that holds the state of each key you want to use for the game. Add the following code to game. The keys array keeps track of the keys being pressed in the following order: WASD. Each item in the array corresponds to one key . Since the game will move the player to different positions, it’s easier to have a variable that contains the player position and then simply draw the player at that position. Now you need to modify the existing code for drawing the player to use the new playerpos variable. Change the following line in section #6: screen. To: screen. blit(player, playerpos)Next, update the keys array based on which keys are being pressed. Py. Game makes detecting key presses easy by adding event. At the end of section #8, right after the block checking for event. QUIT, put this code (at the same indentation level as the pygame. QUIT if block): if event. KEYDOWN. if event. K! Those are a lot of lines of code. If you break it down into the if statements though, it’s not that complicated. First you check to see if a key is being pressed down or released. Then you check which key is being pressed or released, and if the key being pressed or released is one of the keys you’re using, you update the keys variable accordingly. Finally, you need to update the playerpos variable in response to the key presses. This is actually very simple. Add the following code to the end of game. Move playerif keys. Run the game and you should get a player just like before. It works. Step 4: Turning the Bunny. Yes, your bunny now moves when you press keys but wouldn’t it be even cooler if you could use your mouse to rotate the bunny to face a direction of your choosing, so he? It’s simple enough to implement using trigonometry. Take a look at the following illustration: In the above image, if (5,3) is the position of the bunny and (2,4) is the current position of the mouse, you can find the rotation angle (z) by applying the atan. Of course, once you know the rotation angle, you can simply rotate the bunny accordingly. But this is why you should pay attention in Math class! To do this, you can use the Py. Game Surface. rotate(degrees) function. Incidentally, keep in mind that the Z value is in radians. So add this to the end of section #1 first: Then, replace the last line in section #6 (the player. Set player position and rotation. First you get the mouse and player positions. Then you feed those into the atan. After that, you convert the angle received from the atan. Since the bunny will be rotated, its position will change. So now you calculate the new bunny position and display the bunny on screen. Run the game again. If you use just the WASD keys, then the game should behave exactly like before. But if you move your mouse, the bunny rotates too. Cool! Step 5: Shoot, Bunny, Shoot! Now that your bunny’s moving around, it’s time to add a little more action. This is no mild- mannered rabbit! This step is a bit more complicated because you have to keep track of all the arrows, update them, rotate them, and delete them when they go off- screen. First of all, add the necessary variables to the end of the initialization section, section #2: The first variable keeps track of the player’s accuracy and the second array tracks all the arrows. The accuracy variable is essentially a list of the number of shots fired and the number of badgers hit. Later, we will be using this information to calculate an accuracy percentage. Next, load the arrow image at the end of section #3: arrow = pygame. Add the following to the end of section #8 as a new event handler: if event. MOUSEBUTTONDOWN. position=pygame. This rotation value is stored in the arrows array. Next, you have to actually draw the arrows on screen. Add the following code right after section #6. Draw arrowsfor bullet in arrows. The if statement just checks if the bullet is out of bounds and if it is, it deletes the arrow. The second for statement loops through the arrows and draws them with the correct rotation. Try and run the program. You should have a bunny that shoots arrows when you click the mouse! Badgers! OK, you have a castle and you have a hero who can move and shoot. Enemies who attack the castle that the hero can shoot at! In this step, you’ll create randomly generated badgers that run at the castle. There will be more and more badgers as the game progresses. So, let’s make a list of what you’ll need it to do. Add bad guys to a list an array.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |