14
2024
11

python编程学习-别踩白块

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

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

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

from xdf import *

cubes = []
offY = 0
tarY = 0
step = 0
pool = []
txt = text("失败了", 768 / 2, 1024 / 2, 50, "red")
txt.hide()
isPlaying = True

def addRow():
    ind = random(3)-1
    for i in range(3):
        if i == ind:
            addCube(1)
        else:
            addCube(0)

def addCube(typ):
    if len(pool) > 0:
        a = pool.pop(0)
        a[0].hide()
        a[1].hide()
        a[2] = typ
        if typ == 0:
            a[0].show()
        else:
            a[1].show()
        cubes.append(a)
    else:
        cub0 = box(0, 0, 256, 256, "#fff", "red")
        cub1 = box(0, 0, 256, 256, "#0f0", "red")
        cubes.append([cub0, cub1, typ])
        cub0.hide()
        cub1.hide()
        if typ == 0:
            cub0.show()
        else:
            cub1.show()

def update():
    cn = len(cubes)
    tn = cn // 3
    for i in range(cn):
        m = i // 3
        n = i % 3
        cubes[i][0].move((n + 0.5) * 256, (tn - m - 1.5 + offY) * 256)
        cubes[i][1].move((n + 0.5) * 256, (tn - m - 1.5 + offY) * 256)

def popRow():
    pool.append(cubes.pop(0))
    pool.append(cubes.pop(0))
    pool.append(cubes.pop(0))

def loop():
    global offY, tarY, step, isPlaying
    if not isPlaying:
        return
    if offY < tarY:
        offY += step
        if offY >= 1:
            offY -= 1
            tarY -= 1
            popRow()
            addRow()
    update()

def tap():
    global offY, cubes, tarY, offY, step, isPlaying
    if not isPlaying:
        return
    cn = len(cubes)
    tn = cn // 3
    # x = (n + 0.5) * 256
    # y = (tn - m - 1.5 + offY) * 256
    n = x / 256 - 0.5
    m = tn - 1.5 + offY - y / 256
    n = int(n + 0.5)
    m = int(m + 0.5)
    n0 = m * 3 + n
    a = cubes[n0]
    #print(m, n, cn, tn, n0, tarY)
    #print(n0, a[2])
    if m - tarY == 0 and a[2] == 1:
        tarY += 1
        step = (tarY - offY) / 10
    else:
        txt.show()
        txt.front()
        isPlaying = False
    

addRow()
addRow()
addRow()
addRow()
addRow()
update()



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



« 上一篇下一篇 »

相关文章:

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

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

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

python编程学习-井字棋  (2024-11-14 16:34:9)

python编程学习-五子棋  (2024-11-14 16:33:23)

python编程学习-乒乓  (2024-11-14 16:32:46)

python编程学习-一笔画关卡生成  (2024-11-14 16:32:6)

python编程学习-一笔画  (2024-11-14 16:31:27)

python编程学习-woblox  (2024-11-14 16:30:47)

python编程学习-where-my-ice-cream  (2024-11-14 16:29:57)

发表评论:

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