Slide Puzzle Python Code Examples

For algorithm or programming questions about sliding puzzles (such as the 8-puzzle or 15-puzzle), where the player is challenged to slide randomized, numbered tiles along certain routes on a board in order to arrive at a certain end configuration. Description of 8-Puzzle Problem: The 15-puzzle (also called Gem Puzzle, Boss Puzzle, Game of Fifteen, Mystic Square and many others) is a sliding puzzle that consists of a frame of numbered square tiles in random order with one tile missing. The puzzle also exists in other sizes, particularly the smaller 8-puzzle. The Slide Puzzle game will have three buttons: a “Reset” button that will undo any moves the player has made, a “New” button that will create a new slide puzzle, and a “Solve” button that will solve the puzzle for the player., Ka) is just a Python trick to make the code simpler.

Example

Importing and initialising pygame

As we do with any module in python we need to import pygame:

We then initialise all the imported pygame modules:

This is used to initialise all of the pygame modules. Without this the modules would not work

Defining Constants

We then define some constants here:

The WIDTH and HEIGHT constants are used to create a window, which would have a width of 300 pixels and a height of 200 pixels. The function used in SCREEN, pygame.display.set_mode((WIDTH, HEIGHT)), will set the mode of the display and return a Surface object. Note how the parameters for this function are the WIDTH and HEIGHT constants defined earlier.

Setting the name of the window

We then use this function to change the name of the window to My Game:

Defining colours

Afterwards we define 6 colours that can be used in our window:

When defining colours we put in 3 values that range between 0 and 255. The pygame.Color class normally goes by this format:

Python Code Examples Math

Where the r parameter sets the red value of the colour, the g parameter sets the green value of the colour and the b parameter sets the blue value of the colour. The a parameter sets the alpha value of the colour.

We then give this command:

This is a pygame.Surface.fill function which fills the Surface object, our screen, with the colour red.

Using pygame.display.flip()

We then use this function

This basically makes everything we have drawn on the screen Surface become visible and updates the contents of the entire display. Without this line, the user wouldn't see anything on their pygame screen.

The game loop

The next few lines are what's called a 'game loop'.

To start this off we make a variable and make it True:

So that we can start off our while loop:

which will be running throughout the whole game.

In it's most basic form, pygame has 'events' which takes user input, for example a button press or mouse click. Pygame handles these events through an event queue. We can get these events from the event queue with this for loop:

Which basically goes through a list of events, our event queue. These are the next 2 lines:

This will make it so that when the user presses the exit button in the top corner, the event with the type pygame.QUIT occurs.

This then ends the while loop, as is_running is now False and the script moves on to the final line:

Slide Puzzle Python Code ExamplesSlide puzzle python code examples free

Which uninitialises the pygame modules.

Python Example Code Pdf