程序员

用Python实现2048游戏

2021-01-01  本文已影响0人  Miku丨无形

今天带大家完成一份终端版2048游戏

2048游戏还是很经典的

首先还是新建一个空白的棋盘

def screen_into():#棋盘初始化
    for i in range(4):
        list_width = []
        for j in range(4):
            list_width.append(' ')
        screen.append(list_width)

然后是棋盘的显示函数

def screen_print():#打印棋盘
    print('——' * 13)
    for i in range(4):
        print('|',end=' ')
        for j in range(4):
            print(' '+screen[i][j],end='  | ')
        print()
        print('——'*13)

2048必不可少的随机生成棋子

def produce_chess():#随机生成棋子
    global screen
    while True:
        x=random.randint(0,3)
        y=random.randint(0,3)
        if screen[x][y]==' ':
            screen[x][y]=str(num[random.randint(0,2)])
            break

然后是判断是否游戏结束

def referee():#裁判
    dete=False
    for i in range(4):
        for j in range(4):
            if screen[i][j]==' ':
                dete=True
    if not dete:
        for i in range(4):
            for j in range(3):
                if screen[i][j]==screen[i][j+1]:
                    dete=True
        for j in range(4):
            for i in range(3):
                if screen[i][j]==screen[i+1][j]:
                    dete=True
    return dete

游戏操作的上下左右 四个函数

def move_w():#往上
    global screen
    dete=False
    for k in range(3):
        for i in range(1,4):#判断是否可以移动
            for j in range(4):
                if screen[i][j]!=screen[i-1][j] and screen[i-1][j]==' ':
                    screen[i][j],screen[i-1][j]=screen[i-1][j],screen[i][j]
                    dete=True
                    if i>1:
                        if screen[i-1][j]!=screen[i-2][j] and screen[i-2][j]==' ':
                            screen[i-1][j],screen[i-2][j]=screen[i-2][j],screen[i-1][j]
                            if i>2:
                                if screen[i-2][j]!=screen[i-3][j] and screen[i-3][j]==' ':
                                    screen[i-2][j],screen[i-3][j]=screen[i-3][j],screen[i-2][j]
        for i in range(1,4):#判定是否相同 相同则合成(下同)
            for j in range(4):
                if screen[i][j]==screen[i-1][j] and screen[i][j]!=' ':
                    screen[i-1][j]=str(int(screen[i][j])+int(screen[i-1][j]))
                    screen[i][j]=' '
                    dete = True
                    if i>1:
                        if screen[i][j]==screen[i-1][j] and screen[i][j]!=' ':
                            screen[i-1][j]=str(int(screen[i][j])+int(screen[i-1][j]))
                            screen[i][j]=' '
                            screen[i-1][j],screen[i-2][j]=screen[i-2][j],screen[i-1][j]
                            if i>2:
                                if screen[i][j]==screen[i-1][j] and screen[i][j]!=' ':
                                    screen[i-1][j]=str(int(screen[i][j])+int(screen[i-1][j]))
                                    screen[i][j]=' '
                                    screen[i-1][j],screen[i-2][j]=screen[i-2][j],screen[i-1][j]
                                    screen[i-2][j],screen[i-3][j]=screen[i-3][j],screen[i-2][j]
    if dete:
        produce_chess()
    screen_print()

def move_s():#往下
    global screen
    dete=False
    for k in range(3):
        for i in range(2,-1,-1):
            for j in range(4):
                if screen[i][j]!=screen[i+1][j] and screen[i+1][j]==' ':
                    screen[i][j],screen[i+1][j]=screen[i+1][j],screen[i][j]
                    dete = True
                    if i<2:
                        if screen[i+1][j]!=screen[i+2][j] and screen[i+2][j]==' ':
                            screen[i+1][j],screen[i+2][j]=screen[i+2][j],screen[i+1][j]
                            if i<1:
                                if screen[i+2][j]!=screen[i+3][j] and screen[i+3][j]==' ':
                                    screen[i+2][j],screen[i+3][j]=screen[i+3][j],screen[i+2][j]
        for i in range(2,-1,-1):
            for j in range(4):
                if screen[i][j]==screen[i+1][j] and screen[i][j]!=' ':
                    screen[i+1][j]=str(int(screen[i][j])+int(screen[i+1][j]))
                    screen[i][j]=' '
                    dete = True
                    if i<2:
                        if screen[i][j]==screen[i+1][j] and screen[i][j]!=' ':
                            screen[i+1][j]=str(int(screen[i][j])+int(screen[i+1][j]))
                            screen[i][j]=' '
                            screen[i+1][j],screen[i+2][j]=screen[i+2][j],screen[i+1][j]
                            if i<2:
                                if screen[i][j]==screen[i+1][j] and screen[i][j]!=' ':
                                    screen[i+1][j]=str(int(screen[i][j])+int(screen[i+1][j]))
                                    screen[i][j]=' '
                                    screen[i+1][j],screen[i+2][j]=screen[i+2][j],screen[i+1][j]
                                    screen[i+2][j],screen[i+3][j]=screen[i+3][j],screen[i+2][j]
    if dete:
        produce_chess()
    screen_print()

def move_a():#往左
    global screen
    dete = False
    for k in range(3):
        for i in range(4):
            for j in range(1,4):
                if screen[i][j]!=screen[i][j-1] and screen[i][j-1]==' ':
                    screen[i][j],screen[i][j-1]=screen[i][j-1],screen[i][j]
                    dete = True
                    if j>1:
                        if screen[i][j-1]!=screen[i][j-2] and screen[i][j-2]==' ':
                            screen[i][j-1],screen[i][j-2]=screen[i][j-2],screen[i][j-1]
                            if j>2:
                                if screen[i][j-2]!=screen[i][j-3] and screen[i][j-3]==' ':
                                    screen[i][j-2],screen[i][j-3]=screen[i][j-3],screen[i][j-2]
        for i in range(4):
            for j in range(1,4):
                if screen[i][j]==screen[i][j-1] and screen[i][j]!=' ':
                    screen[i][j-1]=str(int(screen[i][j])+int(screen[i][j-1]))
                    screen[i][j]=' '
                    dete = True
                    if j>1:
                        if screen[i][j]==screen[i][j-1] and screen[i][j]!=' ':
                            screen[i][j-1]=str(int(screen[i][j])+int(screen[i][j-1]))
                            screen[i][j]=' '
                            screen[i][j-1],screen[i][j-2]=screen[i][j-2],screen[i][j-1]
                            if j>2:
                                if screen[i][j]==screen[i][j-1] and screen[i][j]!=' ':
                                    screen[i][j-1]=str(int(screen[i][j])+int(screen[i][j-1]))
                                    screen[i][j]=' '
                                    screen[i][j-1],screen[i][j-2]=screen[i][j-2],screen[i][j-1]
                                    screen[i][j-2],screen[i][j-3]=screen[i][j-3],screen[i][j-2]
    if dete:
        produce_chess()
    screen_print()

def move_d():#往右
    global screen
    dete = False
    for k in range(3):
        for i in range(4):
            for j in range(2,-1,-1):
                if screen[i][j]!=screen[i][j+1] and screen[i][j+1]==' ':
                    screen[i][j],screen[i][j+1]=screen[i][j+1],screen[i][j]
                    dete = True
                    if j<2:
                        if screen[i][j+1]!=screen[i][j+2] and screen[i][j+2]==' ':
                            screen[i][j+1],screen[i][j+2]=screen[i][j+2],screen[i][j+1]
                            if j<1:
                                if screen[i][j+2]!=screen[i][j+3] and screen[i][j+3]==' ':
                                    screen[i][j+2],screen[i][j+3]=screen[i][j+3],screen[i][j+2]
        for i in range(4):
            for j in range(2,-1,-1):
                if screen[i][j]==screen[i][j+1] and screen[i][j]!=' ':
                    screen[i][j+1]=str(int(screen[i][j])+int(screen[i][j+1]))
                    screen[i][j]=' '
                    dete = True
                    if j<2:
                        if screen[i][j]==screen[i][j+1] and screen[i][j]!=' ':
                            screen[i][j+1]=str(int(screen[i][j])+int(screen[i][j+1]))
                            screen[i][j]=' '
                            screen[i][j+1],screen[i][j+2]=screen[i][j+2],screen[i][j+1]
                            if j<3:
                                if screen[i][j]==screen[i][j+1] and screen[i][j]!=' ':
                                    screen[i][j+1]=str(int(screen[i][j])+int(screen[i][j+1]))
                                    screen[i][j]=' '
                                    screen[i][j+1],screen[i][j+2]=screen[i][j+2],screen[i][j+1]
                                    screen[i][j+2],screen[i][j+3]=screen[i][j+3],screen[i][j+2]
    if dete:
        produce_chess()
    screen_print()

计算总成绩的函数

def score():#总成绩
    score_sum=0
    for i in range(4):
        for j in range(4):
            score_sum+=int(screen[i][j])
    return score_sum

完整代码如下

import random

screen=[]
num=[2,4,8]

def screen_into():#棋盘初始化
    for i in range(4):
        list_width = []
        for j in range(4):
            list_width.append(' ')
        screen.append(list_width)

def screen_print():#打印棋盘
    print('——' * 13)
    for i in range(4):
        print('|',end=' ')
        for j in range(4):
            print(' '+screen[i][j],end='  | ')
        print()
        print('——'*13)

def produce_chess():#随机生成棋子
    global screen
    while True:
        x=random.randint(0,3)
        y=random.randint(0,3)
        if screen[x][y]==' ':
            screen[x][y]=str(num[random.randint(0,2)])
            break

def referee():#裁判
    dete=False
    for i in range(4):
        for j in range(4):
            if screen[i][j]==' ':
                dete=True
    if not dete:
        for i in range(4):
            for j in range(3):
                if screen[i][j]==screen[i][j+1]:
                    dete=True
        for j in range(4):
            for i in range(3):
                if screen[i][j]==screen[i+1][j]:
                    dete=True
    return dete

def move_w():#往上
    global screen
    dete=False
    for k in range(3):
        for i in range(1,4):#判断是否可以移动
            for j in range(4):
                if screen[i][j]!=screen[i-1][j] and screen[i-1][j]==' ':
                    screen[i][j],screen[i-1][j]=screen[i-1][j],screen[i][j]
                    dete=True
                    if i>1:
                        if screen[i-1][j]!=screen[i-2][j] and screen[i-2][j]==' ':
                            screen[i-1][j],screen[i-2][j]=screen[i-2][j],screen[i-1][j]
                            if i>2:
                                if screen[i-2][j]!=screen[i-3][j] and screen[i-3][j]==' ':
                                    screen[i-2][j],screen[i-3][j]=screen[i-3][j],screen[i-2][j]
        for i in range(1,4):#判定是否相同 相同则合成(下同)
            for j in range(4):
                if screen[i][j]==screen[i-1][j] and screen[i][j]!=' ':
                    screen[i-1][j]=str(int(screen[i][j])+int(screen[i-1][j]))
                    screen[i][j]=' '
                    dete = True
                    if i>1:
                        if screen[i][j]==screen[i-1][j] and screen[i][j]!=' ':
                            screen[i-1][j]=str(int(screen[i][j])+int(screen[i-1][j]))
                            screen[i][j]=' '
                            screen[i-1][j],screen[i-2][j]=screen[i-2][j],screen[i-1][j]
                            if i>2:
                                if screen[i][j]==screen[i-1][j] and screen[i][j]!=' ':
                                    screen[i-1][j]=str(int(screen[i][j])+int(screen[i-1][j]))
                                    screen[i][j]=' '
                                    screen[i-1][j],screen[i-2][j]=screen[i-2][j],screen[i-1][j]
                                    screen[i-2][j],screen[i-3][j]=screen[i-3][j],screen[i-2][j]
    if dete:
        produce_chess()
    screen_print()

def move_s():#往下
    global screen
    dete=False
    for k in range(3):
        for i in range(2,-1,-1):
            for j in range(4):
                if screen[i][j]!=screen[i+1][j] and screen[i+1][j]==' ':
                    screen[i][j],screen[i+1][j]=screen[i+1][j],screen[i][j]
                    dete = True
                    if i<2:
                        if screen[i+1][j]!=screen[i+2][j] and screen[i+2][j]==' ':
                            screen[i+1][j],screen[i+2][j]=screen[i+2][j],screen[i+1][j]
                            if i<1:
                                if screen[i+2][j]!=screen[i+3][j] and screen[i+3][j]==' ':
                                    screen[i+2][j],screen[i+3][j]=screen[i+3][j],screen[i+2][j]
        for i in range(2,-1,-1):
            for j in range(4):
                if screen[i][j]==screen[i+1][j] and screen[i][j]!=' ':
                    screen[i+1][j]=str(int(screen[i][j])+int(screen[i+1][j]))
                    screen[i][j]=' '
                    dete = True
                    if i<2:
                        if screen[i][j]==screen[i+1][j] and screen[i][j]!=' ':
                            screen[i+1][j]=str(int(screen[i][j])+int(screen[i+1][j]))
                            screen[i][j]=' '
                            screen[i+1][j],screen[i+2][j]=screen[i+2][j],screen[i+1][j]
                            if i<2:
                                if screen[i][j]==screen[i+1][j] and screen[i][j]!=' ':
                                    screen[i+1][j]=str(int(screen[i][j])+int(screen[i+1][j]))
                                    screen[i][j]=' '
                                    screen[i+1][j],screen[i+2][j]=screen[i+2][j],screen[i+1][j]
                                    screen[i+2][j],screen[i+3][j]=screen[i+3][j],screen[i+2][j]
    if dete:
        produce_chess()
    screen_print()

def move_a():#往左
    global screen
    dete = False
    for k in range(3):
        for i in range(4):
            for j in range(1,4):
                if screen[i][j]!=screen[i][j-1] and screen[i][j-1]==' ':
                    screen[i][j],screen[i][j-1]=screen[i][j-1],screen[i][j]
                    dete = True
                    if j>1:
                        if screen[i][j-1]!=screen[i][j-2] and screen[i][j-2]==' ':
                            screen[i][j-1],screen[i][j-2]=screen[i][j-2],screen[i][j-1]
                            if j>2:
                                if screen[i][j-2]!=screen[i][j-3] and screen[i][j-3]==' ':
                                    screen[i][j-2],screen[i][j-3]=screen[i][j-3],screen[i][j-2]
        for i in range(4):
            for j in range(1,4):
                if screen[i][j]==screen[i][j-1] and screen[i][j]!=' ':
                    screen[i][j-1]=str(int(screen[i][j])+int(screen[i][j-1]))
                    screen[i][j]=' '
                    dete = True
                    if j>1:
                        if screen[i][j]==screen[i][j-1] and screen[i][j]!=' ':
                            screen[i][j-1]=str(int(screen[i][j])+int(screen[i][j-1]))
                            screen[i][j]=' '
                            screen[i][j-1],screen[i][j-2]=screen[i][j-2],screen[i][j-1]
                            if j>2:
                                if screen[i][j]==screen[i][j-1] and screen[i][j]!=' ':
                                    screen[i][j-1]=str(int(screen[i][j])+int(screen[i][j-1]))
                                    screen[i][j]=' '
                                    screen[i][j-1],screen[i][j-2]=screen[i][j-2],screen[i][j-1]
                                    screen[i][j-2],screen[i][j-3]=screen[i][j-3],screen[i][j-2]
    if dete:
        produce_chess()
    screen_print()

def move_d():#往右
    global screen
    dete = False
    for k in range(3):
        for i in range(4):
            for j in range(2,-1,-1):
                if screen[i][j]!=screen[i][j+1] and screen[i][j+1]==' ':
                    screen[i][j],screen[i][j+1]=screen[i][j+1],screen[i][j]
                    dete = True
                    if j<2:
                        if screen[i][j+1]!=screen[i][j+2] and screen[i][j+2]==' ':
                            screen[i][j+1],screen[i][j+2]=screen[i][j+2],screen[i][j+1]
                            if j<1:
                                if screen[i][j+2]!=screen[i][j+3] and screen[i][j+3]==' ':
                                    screen[i][j+2],screen[i][j+3]=screen[i][j+3],screen[i][j+2]
        for i in range(4):
            for j in range(2,-1,-1):
                if screen[i][j]==screen[i][j+1] and screen[i][j]!=' ':
                    screen[i][j+1]=str(int(screen[i][j])+int(screen[i][j+1]))
                    screen[i][j]=' '
                    dete = True
                    if j<2:
                        if screen[i][j]==screen[i][j+1] and screen[i][j]!=' ':
                            screen[i][j+1]=str(int(screen[i][j])+int(screen[i][j+1]))
                            screen[i][j]=' '
                            screen[i][j+1],screen[i][j+2]=screen[i][j+2],screen[i][j+1]
                            if j<3:
                                if screen[i][j]==screen[i][j+1] and screen[i][j]!=' ':
                                    screen[i][j+1]=str(int(screen[i][j])+int(screen[i][j+1]))
                                    screen[i][j]=' '
                                    screen[i][j+1],screen[i][j+2]=screen[i][j+2],screen[i][j+1]
                                    screen[i][j+2],screen[i][j+3]=screen[i][j+3],screen[i][j+2]
    if dete:
        produce_chess()
    screen_print()

def score():#总成绩
    score_sum=0
    for i in range(4):
        for j in range(4):
            score_sum+=int(screen[i][j])
    return score_sum

def main():
    screen_into()
    produce_chess()
    screen_print()
    while referee():
        user=input('请输入你的操作(wasd代表上下左右)')
        if user=='w' or user=='W':
            move_w()
        elif user=='s' or user=='S':
            move_s()
        elif user=='a' or user=='A':
            move_a()
        elif user=='d' or user=='D':
            move_d()
        else:
            print('指令错误,重新输入')
    print('游戏结束,你的成绩为:',score())
    input('输入任何字符退出游戏')

if __name__=='__main__':
    main()

效果图如下:


在这里插入图片描述

一起学习python,小白指导,教学分享记得私信我

上一篇下一篇

猜你喜欢

热点阅读