15
2024
11

python编程学习-扫雷

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *

cubes = []
data = []
M = 16
N = 12
W = 64
oxy=0.001
isOver = False
lastTap = None

def ij2xy(i, j):
    return (j * W+oxy, i * W+oxy)

def xy2ij(x, y):
    return (int((y-oxy)//W), int((x-oxy)//W))

def ij2n(i, j):
    return i * N + j

def n2ij(n):
    return (int(n//N), n%N)

for i in range(M):
    for j in range(N):
        xy = ij2xy(i,j)
        hw = W / 2
        gap = 5
        fsize = hw
        cb = [
            box(xy[0],xy[1], W, W, "white", "black"),
            box(xy[0] + gap,xy[1] + gap, hw - gap, W - gap * 2, "black"),
            box(xy[0] + hw,xy[1] + gap, hw - gap, W - gap * 2, "black"),
            circle(xy[0] + hw, xy[1] + hw, hw - gap, "red", "black"),
            text("9", xy[0] + hw, xy[1] + hw + fsize / 2, fsize, "black", "center"),
            box(xy[0] + gap + (W - gap * 2) * 0.15,
                xy[1] + gap + (W - gap * 2) * 0.15, 
                (W - gap * 2) * 0.7, 
                (W - gap * 2) * 0.7, "red"),
            0
        ]
        cb[5].rotate(45)
        cubes.append(cb)
        data.append(0)

winTxt = text("You Win", 100, "green", "center")
overTxt = text("Game Over", 100, "red", "center")
neighbors = [
    [-1, -1], [-1, 0], [-1, 1],
    [0, -1],           [0, 1],
    [1, -1], [1, 0], [1, 1],
]

def isOpen(n):
    global cubes
    cb = cubes[n]
    return cb[6] == 1

def isClose(n):
    global cubes
    cb = cubes[n]
    return cb[6] == 0

def isOpenMine(n):
    global cubes,data
    cb = cubes[n]
    return cb[6] == 1 and data[n] == -1

def isMark(n):
    global cubes
    cb = cubes[n]
    return cb[6] == 2

def isOpenOrMark(n):
    global cubes
    cb = cubes[n]
    return cb[6] == 2 or cb[6] == 1

# tp = 0 关闭,1 打开,2 标记
def showCb(n, tp):
    global cubes, data
    cb = cubes[n]
    cb[0].show()
    cb[1].hide()
    cb[2].hide()
    cb[3].hide()
    cb[4].hide()
    cb[5].hide()
    cb[6] = tp
    if tp == 0:
        cb[1].show()
        cb[2].show()
    elif tp == 1:
        if data[n] == -1:
            cb[3].show()
        else:
            cb[4].show()
    elif tp == 2:
        cb[1].show()
        cb[2].show()
        cb[5].show()

def mineNum(i, j):
    num = 0
    global data,neighbors
    for k in range(8):
        a = neighbors[k]
        ii = i + a[0]
        jj = j + a[1]
        if ii >= 0 and ii < M and jj >= 0 and jj < N:
            n0 = ij2n(ii, jj)
            if data[n0] == -1:
                num = num + 1
    return num

def initData():
    global data, cubes
    tn = M * N
    for i in range(tn):
        data[i] = 0
        if random(9) == 5:
            data[i] = -1
    for i in range(tn):
        if data[i] == 0:
            ij = n2ij(i)
            data[i] = mineNum(ij[0], ij[1])
    for i in range(tn):
        cb = cubes[i]
        cb[4].change(str(data[i]))
        showCb(i, 0)
        #showCb(i, 1) # 直接处于打开状态,测试用

def reset():
    global isOver, overTxt, winTxt
    isOver = False
    overTxt.hide()
    winTxt.hide()
    initData()

def gameOver():
    global isOver, overTxt
    isOver = True
    overTxt.show()
    
def gameWin():
    global isOver, winTxt
    isOver = True
    winTxt.show()

def keydown(keycode, key):
    if keycode == 32:
        global isOver
        if isOver:
            reset()

def keyup(keycode, key):
    pass

#单击,标记
def delaySingleTap():
    global lastTap
    if lastTap != None:
        singleTap(lastTap)
        lastTap = None

# 标记
def singleTap(n):
    showCb(n, 2) # 标记
    checkWin()

# 双击打开
def doubleTap(n):
    showCb(n, 1) # 打开
    if isOpenMine(n):
        gameOver()
    else:
        autoOpen(n)
        checkWin()

def tap():
    global isOver,lastTap
    if isOver:
        reset()
        return
    ij = xy2ij(x, y)
    i = ij[0]
    j = ij[1]
    n = ij2n(i, j)
    if isOpen(n):
        return
    if lastTap == None:
        lastTap = n
        delay(delaySingleTap, 300)
    else:
        #print(n, lastTap)
        if n == lastTap:
            doubleTap(n)
        else:
            # 点两下,没点到一个格子里
            singleTap(lastTap)
            singleTap(n)
        lastTap = None

def loop():
    pass

def autoOpen(n):
    global data,cubes,neighbors
    if data[n] == 0:
        ij = n2ij(n)
        i = ij[0]
        j = ij[1]
        for k in range(8):
            a = neighbors[k]
            ii = i + a[0]
            jj = j + a[1]
            if ii >= 0 and ii < M and jj >= 0 and jj < N:
                n0 = ij2n(ii, jj)
                if isClose(n0):
                    showCb(n0, 1) # 打开
                    autoOpen(n0)
    
def checkWin():
    if isWin():
        gameWin()

def isWin():
    global data,cubes
    tn = M * N
    for i in range(tn):
        v = data[i]
        if v == -1: # 雷
            if not isMark(i): # 没标记
                return False
        else: # 非雷
            if not isOpen(i): # 没打开
                return False
    return True

reset()



也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-拼图  (2024-11-15 11:33:25)

python编程学习-小学数学加减分解题  (2024-11-15 11:32:29)

python编程学习-对角棋  (2024-11-15 11:32:8)

python编程学习-密室逃生  (2024-11-15 11:31:35)

python编程学习-围住神经猫  (2024-11-15 11:31:6)

python编程学习-四子棋  (2024-11-15 11:30:32)

python编程学习-华容道  (2024-11-14 16:43:25)

python编程学习-动画  (2024-11-14 16:39:37)

python编程学习-别踩白块  (2024-11-14 16:38:59)

python编程学习-俄罗斯方块  (2024-11-14 16:38:19)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。