15
2024
11

python编程学习-连连看

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

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

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

from xdf import *

CW = 128 #64
M = int(768 / CW) - 2
N = int(1024 / CW) - 2
MN = M * N
data = []
winTxt = text("恭喜过关,点击重新开始", 50, "red", "center")
isWin = False
select1 = None
select2 = None
total = 0

def xy2ij(x, y):
    return [int(y / CW) - 1, int(x / CW) - 1]

def ij2xy(i, j):
    return [(j + 1.5) * CW, (i + 1.5) * CW]

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

def n2ij(n):
    return [int(n//m), n % M]

for i in range(N):
    for j in range(M):
        xy = ij2xy(i, j)
        x = xy[0]
        y = xy[1]
        b = [
            0,
            box(x - CW / 2 + 2, y - CW / 2 + 2, CW - 4, CW - 4, "#ccc", "black"),
            text("0", x, y + CW * 0.25 , CW / 2, "red", "center"),
            box(x - CW / 2 + 2, y - CW / 2 + 2, CW - 4, CW - 4, "transparent", "red"),
            True,
            i,
            j
        ]
        data.append(b)
lines = [line(1,1,100,100, 'red')
        ,line(1,1,100,100, 'red')
        ,line(1,1,100,100, 'red')
        ]

def hide(b):
    b[1].hide()
    b[2].hide()
    b[3].hide()
    b[4]=False

def show(b):
    b[1].show()
    b[2].show()
    b[4] = True

def initData():
    n = 0
    for i in range(N):
        for j in range(M):
            data[n][0] = i
            show(data[n])
            n = n + 1

def upset():
    for i in range(MN):
        j = random(MN) - 1
        temp = data[i][0]
        data[i][0] = data[j][0]
        data[j][0] = temp
    
def tap():
    global data, isWin
    if isWin:
        reset()
        return
    ij = xy2ij(x, y)
    i = ij[0]
    j = ij[1]
    if i < 0 or i >= N or j < 0 or j >= M:
        return
    n = ij2n(i, j)
    b = data[n]
    if (not isEmpty(i, j)) and (not isSelected(b)):
        select(b)
    checkLink()
    refresh()
    
def isSelected(b):
    global select1, select2
    return b == select1 or b == select2

def select(b):
    global select1, select2
    if select1 == None:
        select1 = b
    else:
        select2 = b

def isSelect2():
    global select1, select2
    return select1 != None and select2 != None

def reset():
    global isWin, select1, select2, winTxt, total
    isWin = False
    winTxt.hide()
    initData()
    upset()
    select1 = None
    select2 = None
    refresh()
    total = MN
    hideLines()
    
def refresh():
    global data, select1, select2
    for i in range(MN):
        b = data[i]
        b[2].change(str(b[0]))
        b[3].hide()
    if select1 != None:
        select1[3].show()
    if select2 != None:
        select2[3].show()

def checkLink():
    global select1, select2
    if not isSelect2():
        return
    if canLink():
        checkWin()
        return
    select1 = select2
    select2 = None

def canLink():
    global select1, select2
    if select1[0] != select2[0]:
        return False
    i1 = select1[5]
    j1 = select1[6]
    i2 = select2[5]
    j2 = select2[6]
    if checkOne(i1, j1, i2, j2):
        link()
        showLine(i1, j1, i2, j2, 0)
        return True
    if checkTwo(i1, j1, i2, j2) or checkThree(i1, j1, i2, j2):
        link()
        return True
    return False

def showLine(i1, j1, i2, j2, n):
    global lines
    line = lines[n]
    line.show()
    if j2 < j1:
        temp = j1
        j1 = j2
        j2 = temp
    if i2 < i1:
        temp = i1
        i1 = i2
        i2 = temp
    xy1 = ij2xy(i1, j1)
    xy2 = ij2xy(i2, j2)
    # xdf库有bug,move指定了坐标,还是根据角度算的
    if j1 == j2:
        line.rotate(180)
    else:
        line.rotate(90)
    line.move(xy1[0], xy1[1], xy2[0], xy2[1])
    delay(hideLines, 300)

def hideLines():
    lines[0].hide()
    lines[1].hide()
    lines[2].hide()

def checkTwo(i1, j1, i2, j2):
    #print('two')
    if i1 == i2 or j1 == j2:
        return False
    if (isEmpty(i1, j2) and checkOne(i1, j1, i1, j2) and checkOne(i2, j2, i1, j2)):
        showLine(i1, j1, i1, j2, 0)
        showLine(i1, j2, i2, j2, 1)
        return True
    if (isEmpty(i2, j1) and checkOne(i1, j1, i2, j1) and checkOne(i2, j2, i2, j1)):
        showLine(i1, j1, i2, j1, 0)
        showLine(i2, j1, i2, j2, 1)
        return True
    return False

def checkThree(i1, j1, i2, j2):
    #print('three')
    for i in range(N+2):
        i = i - 1
        #print('i:', i, isEmpty(i, j1), isEmpty(i, j2), checkOne(i1, j1, i, j1), checkOne(i, j1, i, j2), checkOne(i, j2, i2, j2))
        if isEmpty(i, j1) and isEmpty(i, j2) and checkOne(i1, j1, i, j1) and checkOne(i, j1, i, j2) and checkOne(i, j2, i2, j2):
            showLine(i1, j1, i, j1, 0)
            showLine(i, j1, i, j2, 1)
            showLine(i, j2, i2, j2, 2)
            return True
    for j in range(M+2):
        j = j - 1
        #print('j:', j, isEmpty(i1, j), isEmpty(i2, j), checkOne(i1, j1, i1, j), checkOne(i1, j, i2, j), checkOne(i2, j, i2, j2))
        if isEmpty(i1, j) and isEmpty(i2, j) and checkOne(i1, j1, i1, j) and checkOne(i1, j, i2, j) and checkOne(i2, j, i2, j2):
            showLine(i1, j1, i1, j, 0)
            showLine(i1, j, i2, j, 1)
            showLine(i2, j, i2, j2, 2)
            return True
    return False

def checkOne(i1, j1, i2, j2):
    #print("one", i1, j1, i2, j2)
    global data
    if i1 == i2:
        dr = 1
        if j1 > j2:
            dr = -1
        for i in range(abs(j2 - j1) - 1):
            if not isEmpty(i1, j1 + dr*(i + 1)):
                return False;
        return True
    if j1 == j2:
        dr = 1
        if i1 > i2:
            dr = -1
        for i in range(abs(i2 - i1) - 1):
            if not isEmpty(i1 + dr*(i + 1), j1):
                return False;
        return True
    return False

def isEmpty(i, j):
    global data
    if i < 0 or i >= N or j < 0 or j >= M:
        return True
    n = ij2n(i, j)
    return not data[n][4]

def link():
    global select1, select2, total
    hide(select1)
    hide(select2)
    select1 = None
    select2 = None
    total = total - 2


def checkWin():
    global total, isWin
    if total == 0:
        winTxt.show()
        isWin = True

reset()


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



« 上一篇下一篇 »

相关文章:

python编程学习-迷宫01  (2024-11-15 11:41:55)

python编程学习-进入大圈  (2024-11-15 11:40:57)

python编程学习-跳伞  (2024-11-15 11:40:27)

python编程学习-跑酷01  (2024-11-15 11:40:3)

python编程学习-赛车  (2024-11-15 11:39:38)

python编程学习-贪食蛇  (2024-11-15 11:38:41)

python编程学习-警察抓小偷  (2024-11-15 11:38:7)

python编程学习-英雄天梯  (2024-11-15 11:37:35)

python编程学习-翻滚的方块  (2024-11-15 11:37:10)

python编程学习-玛丽跳跳跳  (2024-11-15 11:36:43)

发表评论:

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