Update the keys array based on the keys being pressed. Change the following lines in draw the screen section: You now need to modify the existing code for drawing the player to use the new playerpos variable. Playerpos variable is where the program draws the player (bunny). Each item in the array corresponds to one key with particular action-first to W for up, second to A for left, S for down and D for right movement. The keys array keeps track of the keys being pressed in the following order: WASD. You can do this simply by making an array of key states that hold the state of each key you want to use for the game.Īdd the following code to game.py at the end of initialise the game section #2 (after you set the screen height and width): To do that, implement a method of keeping track of which keys are being pressed at any given moment. Next, add some actual gameplay elements, like making the bunny respond to key presses. 2: Bunny with background colour Make the bunny move If you run the program now, you would get something like shown in Fig. The next couple of lines draw castles on the screen. Then, within that loop, it loops through y and draws the grass at x and y values generated by ‘for’ loop. But if you check the grass image, you would notice that it does not cover the entire screen area, which is 640×480.Īdd the following code to game.py at the beginning of draw the screen section #6 (before the player is drawn on the screen):Īs you can see, the ‘for’ statement loops through x first. This loads the images of grass and castle, and puts these into specific variables. 1: Bunny on the screen Add sceneryĪdd a background to the game scene, which can be done with some more screen.blit() calls.Īt the end of load image section #3 above, after loading the player image, add the following code: W00t, the bunny, is on the scene and ready for action. If you run the code now (via Run\Run Module in Idle menu), you would see a screen as shown in Fig. Loop through the event (#8) checks for any new events if there is one, and if it is quit command, exit the program. Update the screen(#7) updates the screen. Fill the screen with black before you draw anything.ĭraw the screen element(#6) adds the bunny image that you loaded to the screen at x=100 and y=100. Keep looping through(#4) keeps looping over the following indented code.Ĭlear the screen(#5) clears the screen. Load images(#3) loads the image that you would use for the bunny. Initialize the game(#2) initialises PyGame and sets up the display window. Import library(#1) imports PyGame library and allows you to use functions from the library in your program. Save the file in the game folder (the one where the resources sub-folder is) and name it game.py. Run IDLE, open a new text editor window and type the following code: But what is a game without some great graphics and sound effects? The graphics and sound effects required for this game have been included in the design.Ĭreate a folder for your game on your hard disk and extract the resources folder into that folder so that your game folder has a sub-folder named Resources, with various resources grouped in the folders inside. Do note that, Run menu is only available if you have a file open in an editor window. Save your code via File\Save and run it via Run\Run Module (F5). Choose File\New Window from IDLE menu and you will get a text editor window where you can type in your Python code. The other way is to type in your code using IDLE editor, which is what we do here. On Mac, open Terminal and type Python, then drag the file that you saved in Terminal window and click Enter. On Windows, double-click this file to run it. Open a new text file, type in your Python code like print 1+1 and save it as xxx.py (xxx can be any descriptive file name). One way is to use a plain text editor like Notepad in Windows or TextEdit in Mac. There are several ways to run a Python program as a file. If you want to work on a bigger program (like a game), you should save your code to a file so that you do not have to type it repeatedly. It provides functionalities such as image handling and sound playback that you can incorporate into the game.ĭownload PyGame library for Python 2.7 version, PyGame installer appropriate for your system, from Running Python code from file PyGame is a Python library that makes writing games easy. Now that Python is working correctly, install PyGame to write a game using Python. You have just written your first Python program! Open Terminal once at Python prompt, to test if it is correctly working, type ‘print 1+1’ and hit Return. If you are using a Mac, you will already have Python installed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |