while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if finish:
continue
if event.type == KEYDOWN:
if event.key == K_LEFT or event.key == ord('a'):
blackCell = moveLeft(gameBoard, blackCell)
if event.key == K_RIGHT or event.key == ord('d'):
blackCell = moveRight(gameBoard, blackCell)
if event.key == K_UP or event.key == ord('w'):
blackCell = moveUp(gameBoard, blackCell)
if event.key == K_DOWN or event.key == ord('s'):
blackCell = moveDown(gameBoard, blackCell)
if event.type == MOUSEBUTTONDOWN and event.button == 1:
x, y = pygame.mouse.get_pos()
col = int(x / cellWidth)
row = int(y / cellHeight)
index = col + row * VHNUMS
if (
index == blackCell - 1 or index == blackCell + 1 or index == blackCell - VHNUMS or index == blackCell + VHNUMS):
gameBoard[blackCell], gameBoard[index] = gameBoard[index], gameBoard[blackCell]
blackCell = index
code
if (isFinished(gameBoard, blackCell)):
gameBoard[blackCell] = CELLNUMS - 1
finish = True
windowSurface.fill(BACKGROUNDCOLOR)
for i in range(CELLNUMS):
rowDst = int(i / VHNUMS)
colDst = int(i % VHNUMS)
rectDst = pygame.Rect(colDst * cellWidth, rowDst * cellHeight, cellWidth, cellHeight)
if gameBoard[i] == -1:
continue
rowArea = int(gameBoard[i] / VHNUMS)
colArea = int(gameBoard[i] % VHNUMS)
rectArea = pygame.Rect(colArea * cellWidth, rowArea * cellHeight, cellWidth, cellHeight)
windowSurface.blit(gameImage, rectDst, rectArea)
for i in range(VHNUMS + 1):
pygame.draw.line(windowSurface, BLACK, (i * cellWidth, 0), (i * cellWidth, gameRect.height))
for i in range(VHNUMS + 1):
pygame.draw.line(windowSurface, BLACK, (0, i * cellHeight), (gameRect.width, i * cellHeight))
pygame.display.update()
mainClock.tick(FPS)给我详细讲解这个代码
以下是使用Python的Pygame库编写的九宫格拼图游戏以及实现自动复原的代码示例:
python
import pygame
from pygame.locals import *
import random
# 游戏窗口大小
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
# 拼图区域大小
PUZZLE_SIZE = 3
PUZZLE_WIDTH = WINDOW_WIDTH // PUZZLE_SIZE
PUZZLE_HEIGHT = WINDOW_HEIGHT // PUZZLE_SIZE
# 拼图块的类
class PuzzlePiece:
def __init__(self, number, position):
self.number = number
self.position = position
def draw(self, surface):
x = self.position[0] * PUZZLE_WIDTH
y = self.position[1] * PUZZLE_HEIGHT
pygame.draw.rect(surface, (255, 255, 255), (x, y, PUZZLE_WIDTH, PUZZLE_HEIGHT))
pygame.draw.rect(surface, (0, 0, 0), (x, y, PUZZLE_WIDTH, PUZZLE_HEIGHT), 1)
font = pygame.font.Font(None, 36)
text = font.render(str(self.number), True, (0, 0, 0))
text_rect = text.get_rect(center=(x + PUZZLE_WIDTH // 2, y + PUZZLE_HEIGHT // 2))
surface.blit(text, text_rect)
def move(self, direction):
if direction == "up":
self.position = (self.position[0], self.position[1] - 1)
elif direction == "down":
self.position = (self.position[0], self.position[1] + 1)
elif direction == "left":
self.position = (self.position[0] - 1, self.position[1])
elif direction == "right":
self.position = (self.position[0] + 1, self.position[1])
def is_goal_position(self):
return self.number == self.position[0] + self.position[1] * PUZZLE_SIZE
# 拼图游戏类
class PuzzleGame:
def __init__(self, puzzle_size):
self.puzzle_size = puzzle_size
self.puzzle_pieces = []
self.empty_position = (0, 0)
self.moves = []
self.reset()
def reset(self):
numbers = list(range(1, self.puzzle_size ** 2)) + [0]
random.shuffle(numbers)
self.puzzle_pieces.clear()
for i in range(len(numbers)):
x = i % self.puzzle_size
y = i // self.puzzle_size
piece = PuzzlePiece(numbers[i], (x, y))
self.puzzle_pieces.append(piece)
self.empty_position = (self.puzzle_size - 1, self.puzzle_size - 1)
self.moves.clear()
def is_game_over(self):
for piece in self.puzzle_pieces:
if not piece.is_goal_position():
return False
return True
def move_piece(self, piece):
if piece.position[0] == self.empty_position[0] and piece.position[1] == self.empty_position[1] - 1:
piece.move("down")
self.empty_position = (self.empty_position[0], self.empty_position[1] - 1)
self.moves.append("down")
elif piece.position[0] == self.empty_position[0] and piece.position[1] == self.empty_position[1] + 1:
piece.move("up")
self.empty_position = (self.empty_position[0], self.empty_position[1] + 1)
self.moves.append("up")
elif piece.position[0] == self.empty_position[0] - 1 and piece.position[1] == self.empty_position[1]:
piece.move("right")
self.empty_position = (self.empty_position[0] - 1, self.empty_position[1])
self.moves.append("right")
elif piece.position[0] == self.empty_position[0] + 1 and piece.position[1] == self.empty_position[1]:
piece.move("left")
self.empty_position = (self.empty_position[0] + 1, self.empty_position[1])
self.moves.append("left")
def undo_move(self):
if len(self.moves) > 0