Javatpoint Logo
Javatpoint Logo

Slide Puzzle using PyGame - Python

A slide puzzle is a popular puzzle game that involves sliding tiles around on a board to rearrange them into a certain order. Sliding Tile Puzzle in Python Sliding tiles game also known as sliding puzzle or sliding blocks game. In this article, we'll build a slide puzzle game using PyGame, a popular Python library for game development. By the end of this article, you'll have a good understanding of how to build a simple game in Python using PyGame and will have a slide puzzle game to show off to your friends.

To get started, we'll first need to install PyGame. You can do this by running the following command in your terminal or command prompt:

Once you have PyGame installed, create a new Python file and import the library like so:

Next, we'll initialize PyGame and create a window for our game. Add the following code to your file:

The pygame. init() function initializes all PyGame's modules and sets up any necessary resources. The pygame. Display.set_mode() function creates a window for our game, with the size of window_size (400x400 pixels in this case). Finally, pygame. display.set_caption() sets the window title to "Slide Puzzle."

Now that we have a window set up, we need to create the tiles for our slide puzzle. We'll represent each tile as a rectangle with a number on it. To do this, we'll create a Tile class with a number and a position. Add the following code to your file:

The Tile class has two attributes: number and position. The position attribute represents the position of the tile on the window, and the number attribute represents the number that should be displayed on the tile.

The draw method takes a window as a parameter and uses PyGame's font rendering functions to display the number on the tile. The text is centered in the tile by calculating the center of the tile based on its position.

Next, we'll create an array of tiles and place them in the window. Add the following code to your file:

The event handling loop above checks for any events in the game, such as the player closing the window. If the player closes the window, the running variable is set to False, which will cause the game to exit.

Next, we'll draw the tiles in the window. Add the following code to your file:

The window. fill() function is used to clear the window and fill it with a solid black color. The for loop then iterates through each tile in the tiles array and calls the draw method to display the number on the tile. Finally, pygame.display.update() is used to update the window and display the tiles.

Now that we have the tiles displayed, we need to allow the player to move them around. To do this, we'll add code to detect mouse clicks on the tiles and swap the position of the clicked tile with the empty tile (which we'll represent with a None value in the tiles array). Add the following code inside the while loop:

The pygame.mouse.get_pressed()[0] function returns True if the left mouse button is pressed and False otherwise. If the left mouse button is pressed, we use pygame.mouse.get_pos() to get the mouse's current position and then iterate over the tiles array to find which tile was clicked on.

After locating the clicked tile, we utilize if statements to determine whether the tile to its right, left, top or bottom is empty (represented by a None value). If any of these tiles are empty, the clicked tile and the empty tile are switched.

When the window closes, add the following code at the end of your file to clear resources and finish the game:

And that's it! You now have a basic slide puzzle game built with PyGame. You can continue to build on this code and add additional features, such as checking if the puzzle has been solved or allowing the player to select a difficulty level.

To make the game more interesting, we can add a timer to track how long the player takes to solve the puzzle. To do this, we'll use the pygame.time.get_ticks() function returns the number of milliseconds since PyGame was initialized.

Next, let's add the code to display the Time elapsed since the start of the game. We'll use the pygame.font module to render the text, and add the following code inside the while loop:

The time_elapsed variable stores the number of milliseconds that have elapsed since the start of the game, which we calculate by subtracting start_time from the current Time using pygame. Time.get_ticks().

The time_text variable stores the rendered text that displays the Time elapsed, which we create using the font.render() function. The first parameter is the text to render, in this case, "Time: " followed by the Time elapsed in seconds (converted from milliseconds by dividing by 1000). The second parameter specifies whether to use antialiasing when rendering the text. The third parameter is the color of the text. Finally, the window.blit() function is used to draw the time_text on the window at the specified position (0, 0), which is the window's top-left corner.

We can also add a message to display when the puzzle has been solved and the game is over. To do this, we'll add a new variable game over at the top of the file:

And add the following code inside the while loop after the for loop that draws the tiles:

The first if-statement checks if there is a None value in the tiles array, which means the puzzle has not been solved. If there is no None value in the array, the puzzle has been solved, and we set game_over to True.

The second if-statement checks if game_over is True, and if so, it displays the "You won!" message on the window using the game_over_text variable.

Finally, it's a good idea to add a little delay before quitting the game when the puzzle has been solved, so the player has Time to see the "You won!" message. Add the following code to the end of the file:

The output of the above code would be a functioning slide puzzle game built using PyGame. The game window would display the tiles of the puzzle and a timer that keeps track of the Time elapsed since the start of the game. If the player successfully solves the puzzle, the game window will display a "You won!" message and close after a 3-second delay.

Of course, the exact appearance of the game may vary based on the specific design choices you make, such as the font, color, and size of the text, the size and shape of the tiles, and so on. But overall, the code provided above should give you a solid starting point for building your slide puzzle game using PyGame.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA