We will define the colour and position of the snake and its size, i.e. So, let’s make the most important part of our game, the snake. Without the snake, our game has no significance. def Your_score(score): value = score_font.render("Your Score: " + str(score), True, yellow) dis.blit(value, ) We will calculate the score later on in our program. The below function takes in the score as a parameter that it displays. We will define the position where it would be displayed in the game and how will it render the score. So, at first, we define the function to help us with the score display. Here, we need to define three helper functions for the following purposes:- for displaying the score, for handling the snake and for handling the display of other relevant information in the game, respectively. font_style = ("bahnschrift", 25) score_font = ("comicsansms", 35) Defining helper functions Next, we describe the font size and font style for score display and for the display of other information, such as to notify that you lost the game or you want to play again. This is purely based on user choice, and I have also chosen some numbers which I felt like a good fit for the game. Now, let’s define the speed of our snake and also the size of the snake’s block i.e. This variable ‘clock’ will take care of all time-related queries in our complete code. Now, we will initialize our clock variable, which will make use of the time package which we imported earlier. dis = _mode((width, height)) _caption('Shubham Snake Game') Defining the variables being used in the Game Next, we will set our display with the defined width and height variables as input, and we would describe the name of the game to be displayed as a caption for our game. So, in this case, we would define the height to be 600px and the width to be 800px. Next, we should define the size of the display in which our game would be running. Make sure the colour combinations are good for playing the game. We have defined six colours in our game, but you are free to use any colour of your choice. Get the RGB codes from the internet and put these as coordinates for the colour variables. Then we would love to define the colours we would be using in the game with the help of RGB notations. We will first initialise the game to have a window with us in which the game will be played. Now, let’s begin building the actual part of the game. Starting the game and Setting the Graphics Time: This helps us with time-related functionalities in the game, such as the speed of the snake. Random: This library will help us spawn food at random locations in the game. Pygame: This library will help us get all the graphical components to build the game. We will need the below-mentioned libraries to build and run our game. We will do it step by step to get a clear understanding of every part of the game and what purpose it has in our game! So, let’s begin: Importing the librariesįirst, of all, we need to import the libraries into our code which will be used to build the game. Remember the old days when everyone used to play the iconic snake game? Let’s revive those old days and build one on our own! So, in this tutorial, we will be building that iconic game. Yes, you heard that right! We will be building a simple snake game using the pygame library. Hello readers let’s build a game using Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |