You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
10 KiB
259 lines
10 KiB
# -*- coding: UTF-8 -*-
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
import cfg
|
|
import random
|
|
import pygame
|
|
class Button:
|
|
def __init__(self, text, font, color, hover_color, pos, size):#初始化数
|
|
self.text = text
|
|
self.font = font
|
|
self.color = color
|
|
self.hover_color = hover_color
|
|
self.pos = pos
|
|
self.size = size
|
|
self.rect = pygame.Rect(pos, size)
|
|
self.hovered = False
|
|
|
|
def draw(self, surface):
|
|
if self.hovered:
|
|
pygame.draw.rect(surface, self.hover_color, self.rect)
|
|
else:
|
|
pygame.draw.rect(surface, self.color, self.rect)
|
|
|
|
text_surface = self.font.render(self.text, True, (255, 255, 255))
|
|
text_rect = text_surface.get_rect(center=self.rect.center)
|
|
surface.blit(text_surface, text_rect)
|
|
|
|
def update(self, mouse_pos):
|
|
if self.rect.collidepoint(mouse_pos):
|
|
self.hovered = True
|
|
else:
|
|
self.hovered = False
|
|
#鼠标监控
|
|
def clicked(self, mouse_pos):
|
|
return self.rect.collidepoint(mouse_pos)
|
|
|
|
'''判断游戏是否结束'''
|
|
def isGameOver(board, size):
|
|
assert isinstance(size, int)
|
|
num_cells = size * size
|
|
for i in range(num_cells-1):
|
|
if board[i] != i: return False
|
|
return True
|
|
|
|
|
|
'''将空白Cell左边的Cell右移到空白Cell位置'''
|
|
def moveR(board, blank_cell_idx, num_cols):
|
|
if blank_cell_idx % num_cols == 0: return blank_cell_idx
|
|
board[blank_cell_idx-1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-1]
|
|
return blank_cell_idx - 1
|
|
|
|
|
|
'''将空白Cell右边的Cell左移到空白Cell位置'''
|
|
def moveL(board, blank_cell_idx, num_cols):
|
|
if (blank_cell_idx+1) % num_cols == 0: return blank_cell_idx
|
|
board[blank_cell_idx+1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+1]
|
|
return blank_cell_idx + 1
|
|
|
|
|
|
'''将空白Cell上边的Cell下移到空白Cell位置'''
|
|
def moveD(board, blank_cell_idx, num_cols):
|
|
if blank_cell_idx < num_cols: return blank_cell_idx
|
|
board[blank_cell_idx-num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-num_cols]
|
|
return blank_cell_idx - num_cols
|
|
|
|
|
|
'''将空白Cell下边的Cell上移到空白Cell位置'''
|
|
def moveU(board, blank_cell_idx, num_rows, num_cols):
|
|
if blank_cell_idx >= (num_rows-1) * num_cols: return blank_cell_idx
|
|
board[blank_cell_idx+num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+num_cols]
|
|
return blank_cell_idx + num_cols
|
|
|
|
|
|
'''获得打乱的拼图'''
|
|
def CreateBoard(num_rows, num_cols, num_cells):
|
|
board = []
|
|
for i in range(num_cells): board.append(i)
|
|
# 去掉右下角那块
|
|
blank_cell_idx = num_cells - 1
|
|
board[blank_cell_idx] = -1
|
|
for i in range(cfg.NUMRANDOM):
|
|
# 0: left, 1: right, 2: up, 3: down
|
|
direction = random.randint(0, 3)
|
|
if direction == 0: blank_cell_idx = moveL(board, blank_cell_idx, num_cols)
|
|
elif direction == 1: blank_cell_idx = moveR(board, blank_cell_idx, num_cols)
|
|
elif direction == 2: blank_cell_idx = moveU(board, blank_cell_idx, num_rows, num_cols)
|
|
elif direction == 3: blank_cell_idx = moveD(board, blank_cell_idx, num_cols)
|
|
return board, blank_cell_idx
|
|
|
|
|
|
'''随机选取一张图片'''
|
|
def GetImagePath(rootdir):
|
|
imagenames = os.listdir(rootdir)
|
|
assert len(imagenames) > 0
|
|
return os.path.join(rootdir, random.choice(imagenames))
|
|
|
|
|
|
'''显示游戏结束界面'''
|
|
def ShowEndInterface(screen, width, height):
|
|
screen.fill(cfg.BACKGROUNDCOLOR)
|
|
font = pygame.font.Font(cfg.FONTPATH, width//15)
|
|
title = font.render('恭喜! 你成功完成了拼图!', True, (233, 150, 122))
|
|
rect = title.get_rect()
|
|
rect.midtop = (width/2, height/2.5)
|
|
screen.blit(title, rect)
|
|
pygame.display.update()
|
|
while True:
|
|
for event in pygame.event.get():
|
|
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
|
|
time.sleep(5)
|
|
pygame.quit()
|
|
sys.exit()
|
|
pygame.display.update()
|
|
|
|
|
|
'''显示游戏开始界面'''
|
|
def ShowStartInterface(screen, width, height):
|
|
screen.fill(cfg.BACKGROUNDCOLOR)
|
|
tfont = pygame.font.Font(cfg.FONTPATH, width//4)
|
|
cfont = pygame.font.Font(cfg.FONTPATH, width//20)
|
|
title = tfont.render('拼图游戏', True, (10, 10, 10))
|
|
content1 = cfont.render('J、K、L键开始游戏', True, (10, 10, 10))
|
|
content2 = cfont.render('L为5*5模式, K为4*4模式, J为3*3模式', True, (10, 10, 10))
|
|
trect = title.get_rect()
|
|
trect.midtop = (width/2, height/10)
|
|
# 创建按钮
|
|
button1 = Button('3*3模式', cfont, (100, 100, 100), (150, 150, 150), (width / 2 - 100, height / 2.2), (200, 50))
|
|
button2 = Button('4*4模式', cfont, (100, 100, 100), (150, 150, 150), (width / 2 - 100, height / 1.8), (200, 50))
|
|
button3 = Button('5*5模式', cfont, (100, 100, 100), (150, 150, 150), (width / 2 - 100, height / 1.5), (200, 50))
|
|
buttons = [button1, button2, button3]
|
|
crect1 = content1.get_rect()
|
|
crect1.midtop = (width/2, height/2.2)
|
|
crect2 = content2.get_rect()
|
|
crect2.midtop = (width/2, height/1.8)
|
|
screen.blit(title, trect)
|
|
screen.blit(content1, crect1)
|
|
screen.blit(content2, crect2)
|
|
while True:
|
|
mouse_pos = pygame.mouse.get_pos()
|
|
for button in buttons:
|
|
button.update(mouse_pos)
|
|
button.draw(screen)
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
pygame.quit()
|
|
sys.exit()
|
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
mouse_pos = pygame.mouse.get_pos()
|
|
for button in buttons:
|
|
if button.clicked(mouse_pos):
|
|
if button.text == '3*3模式':
|
|
return 3
|
|
# 执行 3*3 模式对应的操作
|
|
elif button.text == '4*4模式':
|
|
return 4
|
|
# 执行 4*4 模式对应的操作
|
|
elif button.text == '5*5模式':
|
|
return 5
|
|
# 执行 5*5 模式对应的操作
|
|
|
|
# 获取鼠标位置
|
|
|
|
|
|
# for event in pygame.event.get():
|
|
# if event.type == pygame.MOUSEBUTTONDOWN:
|
|
# return 3
|
|
# print(666)
|
|
pygame.display.update()
|
|
|
|
|
|
'''主函数'''
|
|
def main():
|
|
# 初始化
|
|
pygame.init()
|
|
clock = pygame.time.Clock()
|
|
# 加载图片
|
|
game_img_used = pygame.image.load("./image/pintu.jpg")
|
|
game_img_used = pygame.transform.scale(game_img_used, (1000, 1000))
|
|
game_img_used_rect = game_img_used.get_rect()
|
|
# 设置窗口
|
|
screen = pygame.display.set_mode((1000, 1000))
|
|
pygame.display.set_caption('拼图游戏')
|
|
# 游戏开始界面
|
|
size = ShowStartInterface(screen, game_img_used_rect.width, game_img_used_rect.height)
|
|
assert isinstance(size, int)
|
|
num_rows, num_cols = size, size
|
|
num_cells = size * size
|
|
# 计算Cell大小
|
|
cell_width = game_img_used_rect.width // num_cols
|
|
cell_height = game_img_used_rect.height // num_rows
|
|
# 避免初始化为原图
|
|
while True:
|
|
game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells)
|
|
if not isGameOver(game_board, size):
|
|
break
|
|
# 游戏主循环
|
|
is_running = True
|
|
while is_running:
|
|
# --事件捕获
|
|
for event in pygame.event.get():
|
|
# ----退出游戏
|
|
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
|
|
pygame.quit()
|
|
sys.exit()
|
|
# ----键盘操作
|
|
elif event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_LEFT or event.key == ord('a'):
|
|
blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)
|
|
elif event.key == pygame.K_RIGHT or event.key == ord('d'):
|
|
blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)
|
|
elif event.key == pygame.K_UP or event.key == ord('w'):
|
|
blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)
|
|
elif event.key == pygame.K_DOWN or event.key == ord('s'):
|
|
blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)
|
|
# ----鼠标操作
|
|
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
x, y = pygame.mouse.get_pos()
|
|
x_pos = x // cell_width
|
|
y_pos = y // cell_height
|
|
idx = x_pos + y_pos * num_cols
|
|
if idx == blank_cell_idx-1:
|
|
blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)
|
|
elif idx == blank_cell_idx+1:
|
|
blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)
|
|
elif idx == blank_cell_idx+num_cols:
|
|
blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)
|
|
elif idx == blank_cell_idx-num_cols:
|
|
blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)
|
|
# --判断游戏是否结束
|
|
if isGameOver(game_board, size):
|
|
game_board[blank_cell_idx] = num_cells - 1
|
|
is_running = False
|
|
# --更新屏幕
|
|
screen.fill(cfg.BACKGROUNDCOLOR)
|
|
for i in range(num_cells):
|
|
if game_board[i] == -1:
|
|
continue
|
|
x_pos = i // num_cols
|
|
y_pos = i % num_cols
|
|
rect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height)
|
|
img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height)
|
|
screen.blit(game_img_used, rect, img_area)
|
|
for i in range(num_cols+1):
|
|
pygame.draw.line(screen, (0,0,0), (i*cell_width, 0), (i*cell_width, game_img_used_rect.height))
|
|
for i in range(num_rows+1):
|
|
pygame.draw.line(screen, (0,0,0), (0, i*cell_height), (game_img_used_rect.width, i*cell_height))
|
|
pygame.display.update()
|
|
clock.tick(30)
|
|
# 游戏结束界面
|
|
ShowEndInterface(screen, game_img_used_rect.width, game_img_used_rect.height)
|
|
|
|
|
|
'''run'''
|
|
if __name__ == '__main__':
|
|
main()
|