Explanation:
Explanation:
Firstly, we will import the necessary libraries. So, for this project, we will make use of “Pygame”, “random”, “time” and “sys” module. If it’s not there in your system then install it by using the following command in your cmd: “pip install ”. After importing the Pygame module the next step is to initialize the module and with the help of “.init()” we will do so.
To set the frames per second for our window will make use of “pygame.time.Clock()” of the “time” module. With that in the next step, we will define the width and height of our game window
Now, to fulfill the most basic need to develop the game, we will use “pygame. the display.set_mode((width, height))” to create the game window. After creating, the title and icon need to be added to make it user-friendly. So with the help of “pygame. display.set_caption()” and “pygame. display.set_icon()” will do so. To load the picture of the icon to this project we will use “pygame.image.load(‘image link’)”.
In the next step, we will declare some variables which will help us out while creating the buttons. Thus, we want 2 rows, and 13 columns, the gap between them should be 20 and the size is 40 since that will be a square. So, first of all, we will make the boxes and then we will place the letters inside the boxes. Now, for the box we will run a nested loop for rows and cols, under that we will mention the x and y position of the boxes. To make the box we will use “pygame. rect(x,y, size, size)”. This command will create a single box and to make sure to display all the boxes we will create an empty list of boxes under the loop after creating every box we will append that in the boxes list by the “.append()” function.
Since we know that, to display every functionality on the game window we have to create a game loop. For that, we have declared a running variable. Initially, the value assigned to the variable is False. With the help of the “while” loop, we will process our game loop. With the help of “pygame.event.get()” we will run a loop for the events. Under this event loop, we will process the type of the event under the if-else ladder with the help of “.type”.
Firstly, we will check for a quit event with the help of “pygame. QUIT” and to show every single thing on the screen we will make use of “pygame. display. update()”.
Under this loop, we will run one for loop of the boxes to be shown on the game window. So we will draw the boxes under that loop.
The next step is to place the letter in the box, for that we will create an empty list named buttons. Now, we will run a for loop for the index and box. Since we know that for ‘A’ the ASCII Code is 65 and if we will add 1 into that we will get the 26 letters of the alphabet. So under that loop, we will declare a variable letter under which with the help of ASCII code we will get our letters, and we will append every single button in the buttons list.
Since, we will need a font as well and with the help of “pygame.font.Sysfont(‘name’,’ size’)” will do so. In the game loop firstly we will render the font with the help of “.render()” and then we want the letter to be at the center of the box so for that we will use “.get_rect(center=(positions))” and after that, we will blit the font on the screen with the help of “.blit()” function.
After drawing the boxes now the next step is to play with words. For that firstly we will create an empty list named words. Now to display the text we will again run a for loop for letters in the word. We will also create another empty list named as guessed which will tell the letter that is guessed. Under the for loop, if the letter exists in the word then we want the string that will gonna appear to have that letter otherwise we will just add an underscore.
For the letters also we need to make a font and will do so with the same function as mentioned above, then we will render it and blit it on the screen. After doing so you will see that the game window will contain the dash spaces.
Now, for the buttons to be get