![]() ![]() I have done that using the event.get() function. When you run this code, you will see that the screen that you saw earlier does not quit and also, it returns all the actions that take place over it. Print(event) #prints out all the actions that take place on the screen To fix that, you should make use of a game loop using the while loop before I actually quit the game as follows: import pygame The difference is that the update() method updates only the changes that are made (however, if no parameters are passed, updates the complete screen) but the flip() method redoes the complete screen again.īut when you run this code, the screen will appear, but it will immediately close as well. There is another method i.e flip() that works similarly to the update() function. The update() method is used to update any changes made to the screen. ![]() ![]() Also, you will have to make use of the init() and the quit() methods to initialize and uninitialize everything at the start and the end of the code. To create the screen using Pygame, you will need to make use of the t_mode() function. Will create a Pygame font from the System font resources Will set the caption text on the top of the display screen Takes a tuple or a list as its parameter to create a surface (tuple preferred) Initializes all of the imported Pygame modules (returns a tuple indicating success and failure of initializations) Before moving on, take a look at the Pygame functions that have been used in this Snake Game along with their descriptions. Once that is done, just import Pygame and start off with your game development. To do that, you can simply use the following command: The first thing you will need to do in order to create games using Pygame is to install it on your systems. Game Over when Snake hits the boundaries.Want to Upskill yourself to get ahead in your Career? Check out the or join our Data Science with Python Course.īefore moving on, let’s have a quick look at all the sub-bits that build the Snake Game in Python: This is what I will be showing you all in this article on Snake Game in Python. As kids, we all loved looking for cheats in order to never see the “Game Over” message but as techies, I know you would want to make this ‘Snake’ dance to your beats. We only included them to show you how it would be done.Yes, I know you all have played the Snake Game and definitely, you never wanted to lose. Two of the if statements are commented out because this is a side scroller game. The two if statements, if > 0: and if > 0: ensure that the player isn’t able to move off screen. The move_ip() takes two parameters, the first representing the distance to be moved in the X direction and second, the distance to be moved in the Y direction. If the if statement proves true, then the move_ip() method is called on Player.rect moving it in a certain direction. The if statements we’ve included after this, check for 4 keys, UP, DOWN, LEFT and RIGHT. When this function is called, the checks to see if any keys are pressed down or not. This is a method from the Player class that controls the movement of the player. If you aren’t careful, you might end up with the Rect and the Image in two different places. Later we’ll use the Rect’s coordinates to draw the image to the exact same location. The last line,, defines a starting position for the Rect. We will be using this in Collision Detection later on. This function is able to automatically create a rectangle of the same size as the image. ![]() This is instead done using the get_rect() function. Note, this does not define the borders for our Player Sprite. Next is the image.load() function to which we pass the file path of our image. You can look it up if you’re interested, else just include it the way we’ve shown above. super()._init_() is a whole different concept related to Classes in Python. Passing super().init() then calls the init() function of the Sprite class. Passing into the parameters,makes the Player Class it’s child class. Now, this doesn’t really apply to the Player Class, since most games will only have one player but it does apply to the Enemy Class as most games will have multiple enemies. The benefit of using classes here is that we can spawn multiple entities from the same block of code. Self.image = ("Player.png")Ībove you can see the Code for the Player Class. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |