python拼图游戏代码_Python华容道、拼图游戏源代码

用Python做的拼图,需要拼图的图片可自选更换,程序运行截图:

6582ec8c68275a89d5669d814e1b5e8f.png

import simpleguitk as simpleguiimport randombaymax = simplegui.load_image('img_3518.jpg')width = 500height = 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 = 3COLS = 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    steps = 0    init_board()def draw(canvas):    """画界面上的元素"""    # 画下方图片    canvas.draw_image(baymax, [width / 2, height / 2], [width, height], [50, width + 50], [98, 98])    # 画下方步数    canvas.draw_text("步数: " + str(steps), [400, 680], 22, 'white')    # 绘制游戏界面各元素    for i in range(ROWS):        for j in range(COLS):            if board[i][j] is not None:                board[i][j].draw(canvas, [i, j])def mouse_click(pos):    """鼠标点击事件"""    global steps    # r为行数,c为列数    r = int(pos[1] // image_size)    c = int(pos[0] // image_size)    if r < 3 and c < 3:        # 点击到空白位置        if board[r][c] is None:            return        else:            # 依次检查当前图像位置的上下左右是否有空位置            current_square = board[r][c]            # 判断上面            if r - 1 >= 0 and board[r - 1][c] is None:                board[r][c] = None                board[r - 1][c] = current_square                steps += 1            # 判断下面            elif r + 1 <= 2 and board[r + 1][c] is None:                board[r][c] = None                board[r + 1][c] = current_square                steps += 1            # 判断在左边            elif c - 1 >= 0 and board[r][c - 1] is None:                board[r][c] = None                board[r][c - 1] = current_square                steps += 1            # 判断在右边            elif c + 1 <= 2 and board[r][c + 1] is None:                board[r][c] = None                board[r][c + 1] = current_square                steps += 1frame = simplegui.create_frame('拼图', width, height)frame.set_canvas_background('black')# 绘制界面frame.set_draw_handler(draw)# 创建窗口,绑定事件,设置大小frame.add_button('重新开始', play_game, 60)# 注册鼠标事件frame.set_mouseclick_handler(mouse_click)# 初始化游戏play_game()# 启动框架frame.start()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值