# -*- coding: utf-8 -*-
import simpleguitk as simplegui
import random
#
载入外部图像
baymax = simplegui.load_image("http://mooc./c4x/XJAU/CS101/asset/baymax.jpg")
#
定义常量
#
画布的尺寸
WIDTH = 600
HEIGHT = WIDTH + 100
#
图像块的边长
IMAGE_SIZE = WIDTH / 3
#
图像块坐标列表
all_coordinates = [[IMAGE_SIZE*0.5, IMAGE_SIZE*0.5], [IMAGE_SIZE*1.5, IMAGE_SIZE*0.5],
[IMAGE_SIZE*2.5, IMAGE_SIZE*0.5], [IMAGE_SIZE*0.5, IMAGE_SIZE*1.5],
[IMAGE_SIZE*1.5, IMAGE_SIZE*1.5], [IMAGE_SIZE*2.5, IMAGE_SIZE*1.5],
[IMAGE_SIZE*0.5, IMAGE_SIZE*2.5], [IMAGE_SIZE*1.5, IMAGE_SIZE*2.5], None]
#
棋盘行列数
ROWS = 3
COLS = 3
#
移动步数
steps = 0
#
保存所有图像块的列表
board = [[None, None, None],
[None, None, None],
[None, None, None]]
#
图像块类
class Square:
def __init__(self, coordinate):
self.center = coordinate
def draw(self, canvas, board_pos):
canvas.draw_image(baymax, self.center, [IMAGE_SIZE,
IMAGE_SIZE],[(board_pos[1]+0.5)*IMAGE_SIZE, (board_pos[0]+0.5)*IMAGE_SIZE], [IMAGE_SIZE,
IMAGE_SIZE])
#
初始化拼图板
def init_board():
#
打乱图像块坐标
random.shuffle(all_coordinates)
#
填充拼图板
for i in range(ROWS):
for j in range(COLS):
idx = i * ROWS + j
square_center = all_coordinates[idx]
if square_center is None:
board[i][j] = None
else:
board[i][j] = Square(square_center)
#
重置游戏
def play_game():
global steps