python写的2048详细教程

话不多说,直接上代码,不清楚2048规则的玩几局就好了,下面对于每个函数都有注释

import os
a = []
score=0
q=0#用在game——over中,判断游戏是否结束
import random#生产随机数

for i in range(4):
    a.append([])
    for j in range(4):
        a[i].append(0)


def random_number():  # 随机生成2或4
    b = random.randrange(2,5,2)
    return (b)


def cout():  # 输出二维数组
    print("score:",score)
    for i in range(4):
        for j in range(4):
            if(a[i][j]!=0):
                print("{:<4}".format(a[i][j]),"|",end=" ")
            else:
                print("{:<4}".format(" "),"|",end=" ")
        print()
    print("(w)up,(s)down,(a)left,(d)right,")
    print("(r)restart,(q)exit")


def right():# 向右移动
    b = 0#记录游戏得分
    paixu1()
    for i in range(4):
        for j in range(3,0,-1):
            if (a[i][j] ==a[i][j - 1]):
                a[i][j] =a[i][j] +a[i][j - 1]
                a[i][j-1]=0
                b = b + a[i][j]
    paixu1()
    return (b)

def left():#向左移动
    b=0
    paixu2()
    for i in range(4):
        for j in range(3):
            if(a[i][j]==a[i][j+1]):
                a[i][j]=a[i][j]+a[i][j+1]
                a[i][j+1]=0
                b=b+a[i][j]
    paixu2()
    return(b)

def up():#向上移动
    b=0
    paixu3()
    for i in range(4):
        for j in range(3):
            if(a[j][i]==a[j+1][i]):
                a[j][i]=a[j][i]+a[j+1][i]
                a[j+1][i]=0
                b=b+a[j][i]
    paixu3()
    return (b)

def down():#向下移动
    b=0#也可以不要
    paixu4()
    for i in range(4):
        for j in range(3,0,-1):
            if(a[j][i]==a[j-1][i]):
                a[j][i]=a[j-1][i]+a[j][i]#写成啊a[j][i]=a[j][i]*2也行
                a[j-1][i]=0
                b=b+a[j][i]
    paixu4()
    return(b)#写成return(a[j][i])也行


def paixu1():#右移动中实现把数字靠拢
    for i in range(4):
        for k in range(3):
            for j in range(3,0,-1):
                if(a[i][j]==0):
                    a[i][j]=a[i][j-1]
                    a[i][j-1]=0

def paixu2():#左移动中实现把数字靠拢
    for i in range(4):
        for k in range(3):
            for j in range(3):
                if(a[i][j]==0):
                    a[i][j]=a[i][j+1]
                    a[i][j+1]=0

def paixu3():#上移动中实现把数字靠拢
    for i in range(4):
        for k in range(3):
            for j in range(3):
                if(a[j][i]==0):
                    a[j][i]=a[j+1][i]
                    a[j+1][i]=0

def paixu4():#下移动中实现把数字靠拢
    for i in range(4):
        for k in range(3):
            for j in range(3,0,-1):
                if(a[j][i]==0):
                    a[j][i]=a[j-1][i]
                    a[j-1][i]=0

def init():#初始化
    for l in range(100):
        i = random.randrange(0,4)
        j = random.randrange(0,4)
        if(a[i][j]==0):
            a[i][j] = random_number()
            break

def restart():#重新开始
    for i in range(4):
        for j in range(4):
            a[i][j]=0
    chu()

def game_over():#判断游戏失败
    for i in range(4):
        for j in range(4):
            if(a[i][j]==0):
                q=1
                break
    if(q==0):
        print("GAME OVER")
    q=0

def chu():
    init()
    init()

chu()
cout()
while 1:
    x=input()
    if(score==2048):
        print("victory,您成功的合成了2048")
        break
    game_over()
    os.system('cls')#清屏函数,在控制台会出现?号,但换到终端后运行就是清屏效果啦'cls’是Windows的如果是Linux改为'clear'就行了
    if(x=='w' or x=='W'):
        score=score+up()
        init()
        cout()
    elif(x=='s'or x=='S'):
        score=score+down()
        init()
        cout()
    elif(x=='a'or x=='A'):
        score=score+left()
        init()
        cout()
    elif(x=='d'or x=='D'):
        score = score +right()
        init()
        cout()
    elif(x=='R'or x=='r'):
        restart()
        cout()
    elif(x=='q'or x=='Q'):
        print("退出游戏成功")
        break
    else:
        print("您的输入有误,请重新输入")







你可能感兴趣的:(python,游戏,程序人生)