歡迎光臨
每天分享高質量文章

如何用Python開發拼圖遊戲

點選“藍字”發現驚喜

一、效果的演示

小編用了91步才完成這拼圖…你會用多少步呢?

二、 遊戲的玩法

思路:先設定一個棋盤,棋盤裡面有我們的影象,在影象裡面我們有一個個的小方塊,透過這些錯亂的小方塊拼接圖板。拼接的過程就是滑鼠點選事件的一個過程

三、具體的實現步驟

1.設定影象

2.定義一個影象塊的類

3.定義一個方法開始拼接圖板

4.重置遊戲

5.繪製遊戲介面各元素

6.定義滑鼠的點選事件

7.建立框架

8.註冊滑鼠事件

9.初始化遊戲

10.啟動框架

環境:Python 3.6 + Windows

IDE: sublime txt3

使用到的模組:Simpleguitk

安裝模組:pip install simpleguitk


Python程式碼

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import simpleguitk as simplegui
import random

byamax = simplegui.load_image('https://timgsa.baidu.com/timg?image&quality;=80&size;=b9999_10000&sec;=1523374883465&di;=d0545c2c8adb05310f4f56a35e2c6976&imgtype;=0&src;=http%3A%2F%2Fimg01.taopic.com%2F160625%2F235106-1606250Q05845.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,coordinage):
        self.center = coordinage

    def draw(self,canvas,board_pos):

        canvas.draw_image(byamax,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(byamax,[WIDTH/2,WIDTH/2],[WIDTH,WIDTH],[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 mouseclick(pos):

    global steps

    r = int(pos[1]//IMAGE_SIZE)
    c = int(pos[0]//IMAGE_SIZE)

    if r<3 and c<3:
        if board[r][cis 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 c + 1 <= 2 and board[r][c + 1is None:  # 判斷右面
                board[r][c] = None
                board[r][c + 1] = current_square
                steps += 1
            elif r + 1 <= 2 and board[r + 1][cis 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



frame = 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(mouseclick)

play_game()

frame.start()


Python影片詳解