The function of the program is very simple. It's still the knowledge of pygame and random number that we used before. The whole process is very clear. But we need to pay attention to: if the game is randomly scrambled directly on the two-dimensional array of the map, it's very likely that there will be no solution. Therefore, I use the idea of random movement (moving according to the rules of the game) to scramble the map on the basis of good spelling.
Randomly find a photo and code file in the same file folder.
import random import pygame # Initialization pygame.init() # Window title pygame.display.set_caption('Ahab Grocery store puzzle') # Window size s = pygame.display.set_mode((1200, 600))
Then initialize the game, set the title and the size of the game interface.
# Cartographic map imgMap = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] # Map of victory winMap = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ]
Draw the initial map and set the victory map, which is processed in the way of array.
# Click event for game def click(x, y, map): if y - 1 >= 0 and map[y - 1][x] == 8: map[y][x], map[y - 1][x] = map[y - 1][x], map[y][x] elif y + 1 <= 2 and map[y + 1][x] == 8: map[y][x], map[y + 1][x] = map[y + 1][x], map[y][x] elif x - 1 >= 0 and map[y][x - 1] == 8: map[y][x], map[y][x - 1] = map[y][x - 1], map[y][x] elif x + 1 <= 2 and map[y][x + 1] == 8: map[y][x], map[y][x + 1] = map[y][x + 1], map[y][x]
Here you need to set the click event of the game. In short, it is the logic of mouse clicking the picture to move. The main logic code is to make if judgment, which is easy to understand.
# Disrupt maps def randMap(map): for i in range(1000): x = random.randint(0, 2) y = random.randint(0, 2) click(x, y, map)
Use random numbers to scramble the map.
# Loading pictures img = pygame.image.load('2.jpg') # Random map randMap(imgMap) # Game main cycle while True: # Delay 32 ms, equivalent to FPS=30 pygame.time.delay(32) for event in pygame.event.get(): # Window close event if event.type == pygame.QUIT: exit() elif event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed() == (1, 0, 0): mx, my = pygame.mouse.get_pos() if mx < 498 and my < 498: x = int(mx / 166) y = int(my / 166) click(x, y, imgMap) if imgMap == winMap: print("Success!")
Load our photos and randomly scramble the map. Set the main cycle of the game, obtain the coordinates of the mouse, judge whether the mouse is within the operation range, calculate the blocks clicked by the mouse, and judge whether the operation is successful.
# Background color filled with green s.fill((0, 255, 0)) # Mapping for y in range(3): for x in range(3): i = imgMap[y][x] if i == 8: # Block 8 does not need to be drawn continue dx = (i % 3) * 166 # Calculate plot offset dy = (int(i / 3)) * 166 s.blit(img, (x * 166, y * 166), (dx, dy, 166, 166)) # Draw reference picture s.blit(img, (500, 0)) # Refresh interface pygame.display.flip()
Fill the background, draw pictures and set reference pictures.
If you are interested in Python programming, remember to come to the python learning group of Xiaobian: 1017759557, where there are resource sharing and technical solutions. You can exchange python programming experience together, and a python learning tutorial organized by Xiaobian, hoping to help you learn Python better.