#coding=gbk
import copy
import pygame
import random
from pygame.locals import *
#from gameobjects.vector2 import *
from sys import exit
from datas import chrs
from ksolution import ksolute
# 资源路径
bgimg = './res/new_bg.png'
blenderimg = './res/blender.png'
cherrsimg = './res/cherrs.png'
logoimg = './res/klotski.png'
kingimg = './res/king.png'
vleaderimg = './res/vleader.png'
hleaderimg = './res/hleader.png'
pawnimg = './res/pawn.png'
prebtnimg = './res/pre_button.png'
nextbtnimg = './res/next_button.png'
startbtnimg = './res/start_button.png'
answerbtnimg = './res/answer_button.png'
resetimg = './res/reset_button.png'
homeimg = './res/home_button.png'
undoimg = './res/undo_button.png'
pauseimg = './res/pause_button.png'
playimg = './res/play_button.png'
bgsound = './res/journey.mp3'
htsound = './res/p.wav'
wasound = './res/wa.wav'
# 棋局数据宏定义
# 对于多格角色,只有左上角格用K、H、V,其他占位格用S
# 如K在数组中表示为:K S
# S S
K = 7 # king,棋局中的'曹操'
H = 2 # 长度为2的横向元素,棋局中的横向大将
V = 3 # 长度为2的纵向元素,棋局中的纵向大奖
P = 4 # 长宽都为1的元素,棋局中的小兵
B = 0 # 长度都为1的空白位置
S = 1 # 多个角色的占位格
# 棋格宽高、棋盘左、上保留区、Blender位置
WID = 100
HID = 100
LM = 10
TM = 10
BLD_x = 10
BLD_y = 160
TIPS_POS = (210, 210)
TIME_POS = (14, 518)
STEP_POS = (360, 518)
RESET_POS = (110, 520)
HOME_POS = (185, 520)
UNDO_POS = (260, 520)
# 反方向定义
redest_dic = {
'up' : 'down',
'down' : 'up',
'left' : 'right',
'right' : 'left',
}
# 角色与图片对应关系
t2img_dict = {
K:kingimg, H:hleaderimg,
V:vleaderimg, P:pawnimg
}
# 角色类
class Elet(object):
# 角色的宽高格数
tsize = { K:(2, 2), H:(2, 1), V:(1, 2), P:(1, 1)}
# 初始化
# target : 对应的surface
# type :角色类型,数字2、3、4、7,意义见上文宏定义
# loct : 逻辑位置,整个棋盘为5x4矩阵,loct为矩阵行列,如(0, 2)表示第一行第三列
# kboard :当前棋局数据,为一个二维数组,意义见'棋局数据宏定义'
def __init__(self, target, type, loct, kboard):
self.surface = target
self.type = type
self.kboard = kboard
self.loct = loct
self.image = pygame.image.load(t2img_dict[type]).convert_alpha()
# 序列化,用于打印显示
def __str__(self):
dic = {2:'H', 3:'V', 4:'P', 7:'K', 1:'S', 0:'B'}
return 'type:%s loct:(%d, %d)' % (dic[self.type], self.loct[0], self.loct[1])
# 在surface上绘制角色
def draw(self):
x, y = self.loct[1] * WID + LM, self.loct[0] * HID + TM
self.surface.blit(self.image, (x, y))
# 判断某坐标点是否在角色范围内
def is_over(self, pos, WID=100, HID=100):
x, y = self.loct[1] * WID + LM, self.loct[0] * HID + TM
w, h = self.tsize[self.type][0]*WID, self.tsize[self.type][1]*HID
return (x <= pos[0] < x + w) and (y <= pos[1] < y + h)
# 将角色向某方向移动一格
# dest : 方向,取值为'up', 'left', 'right', 'down'
def move_once(self, dest):
# 先判断能否移动,不能则返回
if not self.can_move(dest):
return
# 移动角色
if self.type == K:
if dest == 'up':
self.kboard[self.loct[0]-1][self.loct[1]:self.loct[1]+2] = [K, S]
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+2] = [S, S]
self.kboard[self.loct[0]+1][self.loct[1]:self.loct[1]+2] = [B, B]
self.loct[0] -= 1
if dest == 'down':
self.kboard[self.loct[0]+1][self.loct[1]:self.loct[1]+2] = [K, S]
self.kboard[self.loct[0]+2][self.loct[1]:self.loct[1]+2] = [S, S]
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+2] = [B, B]
self.loct[0] += 1
if dest == 'left':
self.kboard[self.loct[0]][self.loct[1]-1:self.loct[1]+1] = [K, S]
self.kboard[self.loct[0]+1][self.loct[1]-1:self.loct[1]+1] = [S, S]
self.kboard[self.loct[0]][self.loct[1]+1] = B
self.kboard[self.loct[0]+1][self.loct[1]+1] = B
self.loct[1] -= 1
if dest == 'right':
self.kboard[self.loct[0]][self.loct[1]+1:self.loct[1]+3] = [K, S]
self.kboard[self.loct[0]+1][self.loct[1]+1:self.loct[1]+3] = [S, S]
self.kboard[self.loct[0]][self.loct[1]] = B
self.kboard[self.loct[0]+1][self.loct[1]] = B
self.loct[1] += 1
if self.type == V:
if dest == 'up':
self.kboard[self.loct[0]-1][self.loct[1]] = V
self.kboard[self.loct[0]][self.loct[1]] = S
self.kboard[self.loct[0]+1][self.loct[1]] = B
self.loct[0] -= 1
if dest == 'down':
self.kboard[self.loct[0]+1][self.loct[1]] = V
self.kboard[self.loct[0]+2][self.loct[1]] = S
self.kboard[self.loct[0]][self.loct[1]] = B
self.loct[0] += 1
if dest == 'left':
self.kboard[self.loct[0]][self.loct[1]-1:self.loct[1]+1] = [V, B]
self.kboard[self.loct[0]+1][self.loct[1]-1:self.loct[1]+1] = [S, B]
self.loct[1] -= 1
if dest == 'right':
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+2] = [B, V]
self.kboard[self.loct[0]+1][self.loct[1]:self.loct[1]+2] = [B, S]
self.loct[1] += 1
if self.type == H:
if dest == 'up':
self.kboard[self.loct[0]-1][self.loct[1]:self.loct[1]+2] = [H, S]
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+2] = [B, B]
self.loct[0] -= 1
if dest == 'down':
self.kboard[self.loct[0]+1][self.loct[1]:self.loct[1]+2] = [H, S]
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+2] = [B, B]
self.loct[0] += 1
if dest == 'left':
self.kboard[self.loct[0]][self.loct[1]-1:self.loct[1]+2] = [H, S, B]
self.loct[1] -= 1
if dest == 'right':
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+3] = [B, H, S]
self.loct[1] += 1
if self.type == P:
if dest == 'up':
self.kboard[self.loct[0]-1][self.loct[1]] = P
self.kboard[self.loct[0]][self.loct[1]] = B
self.loct[0] -= 1
if dest == 'down':
self.kboard[self.loct[0]+1][self.loct[1]] = P
self.kboard[self.loct[0]][self.loct[1]] = B
self.loct[0] += 1
if dest == 'left':
self.kboard[self.loct[0]][self.loct[1]-1:self.loct[1]+1] = [P, B]
self.loct[1] -= 1
if dest == 'right':
self.kboard[self.loct[0]][self.loct[1]:self.loct[1]+2] = [B, P]
self.loct[1] += 1
# 判断角色是否能向某个方向移动
# dest : 方向,取值为'up', 'left', 'right', 'down'
def can_move(self, dest):
if (dest == 'up') and (self.loct[0] > 0):
if (self.type in (P, V)) and (self.kboard[self.loct[0]-1][self.loct[1]] == B):
return True
if (self.type in (K, H)) and (self.kboard[self.loct[0]-1][self.loct[1]] == B) and \
(self.kboard[self.loct[0]-1][self.loct[1]+1] == B):
return True
if (dest == 'down') and (self.loct[0] + self.tsize[self.type][1] < 5):
if (self.type in (P, V)) and (self.kboard[self.loct[0]+self.tsize[self.type][1]][self.loct[1]] == B):
return True
if (self.type in (K, H)) and (self.kboard[self.loct[0]+self.tsize[self.type][1]][self.loct[1]] == B) and \