14
2024
11

python编程学习-2048

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

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

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

from xdf import *

SW = 768
SH = 1024
M = 4
N = M * M
CW = 128
OX = (SW - CW * M) // 2
OY = (SH - CW * M) // 2
isOver = False
cubes = []
datas = []

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

def xy2ij(x, y):
    return [(x-OX) // CW, (y-OY) // CW]

for i in range(M):
    cubes.append([])
    datas.append([])
    for j in range(M):
        xy = ij2xy(i, j)
        cubes[i].append([
            box(xy[0], xy[1], CW, CW, 'white', 'black'),
            box(xy[0], xy[1], CW, CW, 'blue', 'transparent'),
            box(xy[0], xy[1], CW, CW, 'green', 'transparent'),
            text('00', xy[0] + CW // 2, xy[1] + CW//2 + CW//8, CW//4, 'red', 'center'),
            1
        ])
        datas[i].append(0)

overTxt = text("Game Over", 50, "red", "center")
        
def refresh():
    for i in range(M):
        for j in range(M):
            cube = cubes[i][j]
            value = datas[i][j]
            if value == 0:
                cube[1].hide()
                cube[2].hide()
                cube[3].change('')
            else:
                if cube[4] == 1:
                    cube[2].show()
                    cube[1].hide()
                else:
                    cube[1].show()
                    cube[2].hide()
                cube[3].change(value)
            cube[4] = 0

def reset():
    #print("reset")
    global isOver,count
    count = 0
    isOver = False
    overTxt.hide()
    for i in range(M):
        for j in range(M):
            datas[i][j]=0
    addCube()
    refresh()

def isNotOut(p):
    return p[0] >= 0 and p[0] < M and p[1] >= 0 and p[1] < M

def nextP(p, d):
    return [p[0] + d[0], p[1] + d[1]]

def prevP(p, d):
    return [p[0] - d[0], p[1] - d[1]]

def move(sts, dr):
    for i in range(len(sts)):
        flag = True
        while flag:
            flag = False
            p0 = sts[i]
            p1 = nextP(p0, dr)
            while isNotOut(p1):
                c0 = datas[p0[0]][p0[1]]
                c1 = datas[p1[0]][p1[1]]
                if c0 == 0 and c1 != 0:
                    datas[p0[0]][p0[1]] = c1
                    datas[p1[0]][p1[1]] = 0
                    flag = True
                    #print('空白')
                elif c0 != 0 and c0 == c1:
                    datas[p0[0]][p0[1]] = c1 * 2
                    datas[p1[0]][p1[1]] = 0
                    flag = True
                    #print('合并')
                p0 = p1
                p1 = nextP(p0, dr)
            
    addCube()
    refresh()
    
def moveLeft():
    move([[0, 0], [0, 1], [0, 2],[0, 3]], [1, 0])

def moveRight():
    move([[3, 0], [3, 1], [3, 2],[3, 3]], [-1, 0])

def moveUp():
    move([[0, 0], [1, 0], [2, 0],[3, 0]], [0, 1])

def moveDown():
    move([[0, 3], [1, 3], [2, 3],[3, 3]], [0, -1])

def addCube():
    ps = []
    for i in range(M):
        for j in range(M):
            if datas[i][j] == 0:
                ps.append([i,j])
    if len(ps) == 0:
        gameOver()
    else:
        i = random(len(ps))-1
        p = ps[i]
        datas[p[0]][p[1]] = 1
        cubes[p[0]][p[1]][4] = 1
    
def gameOver():
    global isOver
    isOver = True
    overTxt.show()

def keydown(key, code):
    global isOver
    #print(key, code)
    if isOver:
        if key == 32:
            reset()
        return
    if key == 38:
        moveUp()
    elif key == 40:
        moveDown()
    elif key == 37:
        moveLeft()
    elif key == 39:
        moveRight()


reset()

mp0 = [0, 0]
def touch():
    global mp0
    mp0 = [x, y]
    
def untouch():
    global mp0
    dx = x - mp0[0]
    dy = y - mp0[1]
    if abs(dx) > 20 or abs(dy) > 20:
        if abs(dx) > abs(dy):
            if dx > 0:
                keydown(39, 'ArrowRight')
            else:
                keydown(37, 'ArrowLeft')
        else:
            if dy > 0:
                keydown(40, 'ArrowDown')
            else:
                keydown(38, 'ArrowUp')
    else:
        keydown(32, ' ')



也可以下载代码,本地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)

发表评论:

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