14
2024
11

python编程学习-Sliding-Bricks

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

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

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

from xdf import *
setFrameRate(60)
W = 768
H = 1024
m = 5
CW = 100
OX = (W - CW * m) // 2
colors = ["#EE412F", "#38E62F", "#8F37E2", "#FDB323", "#008DB6"]
isOver = True
score = 0
speed = 1
count = 0
cubes = []
pool = []
def getCube():
    if len(pool):
        return pool.pop()
    return box(-CW, -CW, CW, CW, "red", "black")
hero = getCube()
tf = text("", -100, -100, 40, "black", "center")
overTf = text("", 50, "black", "center")

def shuffle(arr):
    n = len(arr)
    for i in range(0, n-1):
        for j in range(i+1,n):
            if random(10) > 5:
                temp = arr[i]
                arr[i] = arr[j]
                arr[j] = temp
    pass

def setCubeColor(cube, color):
    cube.change(color)
    cube.color = color
    return cube
def randomCubeColor(cube):
    return setCubeColor(cube, colors[random(0, len(colors)-1)])
def updateScore():
    tf.change(str(score))
def moveCube(cube, x, y):
    cube.move(x, y)
    cube.x0 = x
    cube.y0 = y
    pass
def moveCubeBy(cube, dx, dy):
    moveCube(cube, cube.x0 + dx, cube.y0 + dy)
def moveHero(x, y):
    moveCube(hero, x, y)
    tf.move(x, y)
def moveHeroBy(dx, dy):
    moveHero(hero.x0 + dx, hero.y0 + dy)
def addLine(oy):
    shuffle(colors)
    if len(cubes):
        c = cubes[-1]
        dy = (oy - CW) - c.y0
        for cube in cubes:
            moveCubeBy(cube, 0, dy)
        moveHeroBy(0, dy)
    else:
        i = int((hero.x0 - OX) / CW)
        if colors[i] == hero.color:
            j = 0 if i != 0 else 1
            tmp = colors[i]
            colors[i] = colors[j]
            colors[j] = tmp
    for i in range(m):
        cube = getCube()
        cubes.append(cube)
        moveCube(cube, OX + i * CW + CW // 2, oy)
        setCubeColor(cube, colors[i])
    pass
def reset():
    global isOver,score, speed, count
    isOver = False
    score = 0
    speed = 1
    count = 0
    moveHero(0, 0)
    randomCubeColor(hero)
    pool.extend(cubes)
    cubes.clear()
    for i in range(5):
        addLine(H + CW // 2)
    while hero.color == cubes[2].color:
        randomCubeColor(hero)
    moveHero(cubes[2].x0, cubes[2].y0 - CW)
    updateScore()
    overTf.hide()
    overTf.change("Game Over")
    pass
def tryAddLine():
    if len(cubes) == 0:
        addLine(hero.y0 + CW)
    elif cubes[-1].y0 + speed < H:
        addLine(cubes[-1].y0 + CW)
    pass
def checkOver():
    global isOver
    if hero.y0 < -CW // 2:
        isOver = True
        overTf.show()
        overTf.front()
    pass
def checkTap():
    if x >= OX and x <= OX + CW * m:
        moveHero(OX + (x - OX) // CW * CW + CW // 2, hero.y0)
    pass
def removeLine():
    for i in range(m):
        cube = cubes.pop(0)
        pool.append(cube)
    pass
def checkCube():
    global score, speed, count
    while True:
        i = int((hero.x0 - OX) / CW)
        if cubes[i].color == hero.color:
            removeLine()
            score += 1
            speed += 0.05
            count += 1
            moveHeroBy(0, CW)
            tryAddLine()
            pass
        else:
            break
    if count >= 5:
        c = hero.color
        while c == hero.color:
            randomCubeColor(hero)
        count = 0
    if hero.y0 > H - CW // 2:
        dy = H - CW // 2 - hero.y0
        moveHeroBy(0, dy)
        for cube in cubes:
            moveCubeBy(cube, 0, dy)
    pass
def tap():
    if isOver:
        reset()
    else:
        checkTap()
        pass
    pass

def loop():
    if isOver:
        return
    for cube in pool:
        cube.hide()
    for cube in cubes:
        cube.show()
        moveCubeBy(cube, 0, -speed)
    moveHeroBy(0, -speed)
    checkCube()
    tryAddLine()
    checkOver()
    updateScore()
    pass

reset()

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



« 上一篇下一篇 »

相关文章:

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)

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)

发表评论:

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