Python的学习心得和知识总结(十)|Python小游戏开发(2048-B站大佬版)

    技术2026-08-03  1

    因为这段时间没有刷番了,就想着回B站看看。然后就莫名其妙的看了一位B站大佬的视频,主要是做的一个2048游戏的Python程序。

    (看完之后,觉得人和人的差距怎么那么大!!!)

    1、效果展示如下: 2、源代码如下:

    #coding=utf-8 ''' Author :SongBaoBao Project :my2048 FileName:2048_main.py Currtime:2020/7/5--00:06 Commpany:Tsinghua University MyCsdnIs:https://blog.csdn.net/weixin_43949535 MyLolLpl:Royal Never Give Up ''' # # # 游戏的主模块 import pygame import sys,os import random import itertools def moveOperator(array,direction=0): # 第一步:过滤0 # 第二步:反方向补0 总数要多于4个 # 第三步:正方向进行切片 截取4个 return ([0,0,0,0]+[number for number in array if number])[-4:] if direction else ([number for number in array if number]+[0,0,0,0])[:4] def sumOperator(array,direction=0): # 上面完成移动之后,相同数字移动到一起了。这个时候就需要去进行一个相加操作 if array[1] and array[2] and array[1]==array[2]: return moveOperator([array[0],array[1]*2,0,array[3]],direction=direction) if array[0] and array[1] and array[0]==array[1]: array[0],array[1]=array[0]*2,0 if array[2] and array[3] and array[2]==array[3]: array[2],array[3]=array[2]*2,0 return moveOperator(array,direction=direction) #********************移动游戏界面的操作********************# def upOperator(operationInterface): for col in (0,1,2,3): for row_index,result in enumerate(sumOperator(moveOperator([row[col] for row in operationInterface]))): operationInterface[row_index][col]=result return operationInterface def downOperator(operationInterface): for col in [0,1,2,3]: for row_index,result in enumerate(sumOperator(moveOperator([row[col] for row in operationInterface],direction=1),direction=1)): operationInterface[row_index][col]=result return operationInterface def rightOperator(operationInterface): return [sumOperator(moveOperator(row,direction=1),direction=1) for row in operationInterface] def leftOperator(operationInterface): return [sumOperator(moveOperator(row,0),0) for row in operationInterface] #********************移动游戏界面的操作********************# class MyGameOf_2048: """ 这个的实例就相当于一局游戏 """ # 游戏的操作界面:我们这里选择一个高难度的 4*4 operationInterface=[] # 游戏的控制键:w s a d 分别代表 上 下 左 右 controlKey=["w","s","a","d"] # 随机取出来一个空格子,然后填充数字: 2 or 4 def random_fill(self): # 这个随机数种子是个伪随机 number=random.choice([2,4,2,4,2,4,2,4,2,4,2,4]) # 下面是生成所有格子的下表索引 随机抽到一个空格 x,y=random.choice([(x,y) for x,y in itertools.product([0,1,2,3],[0,1,2,3]) if self.operationInterface[x][y]==0]) self.operationInterface[x][y]=number # 打印整个游戏界面 def print_screen(self): # 首先清空上一次打印 os.system("cls") print("-" * 30) for row in self.operationInterface: print("|{}|".format("|".join([str(col or " ").center(4) for col in row]))) print("-" * 30) # 游戏的算法设计:逻辑操作 def logicOperator(self,controlkey): # 动作字典:根据用户输入的信息,进行调用相应的方向动作。 method={"w":upOperator,"s":downOperator,"a":leftOperator,"d":rightOperator}[controlkey] # 进行一个深拷贝,调用方法进行处理 newOperationInterface=method([[col for col in row] for row in self.operationInterface]) if newOperationInterface !=self.operationInterface: # 游戏界面已经发生变化:辞旧迎新 del self.operationInterface[:] self.operationInterface.extend(newOperationInterface) #****************************************************# # 每一步走完,都需要判断一下是否成功:条件为达到4096 if [result for result in itertools.chain(*newOperationInterface) if result >=32]: return 1,"Congratulation!" self.random_fill() else: # 移动不了,然后去看一下是不是四个方向都移动不了.意思就是:往这四个方向都试一下,如果最后列表为空 说明走投无路了 if not [1 for g in [func(newOperationInterface) for func in [upOperator,downOperator,leftOperator,rightOperator]] if g !=self.operationInterface]: return -1,"Game Over!" return 0,"" #0表示继续向下;-1表示 Game Over;1表示 Congratulation # 游戏的整个过程:实际上是一个循环,整个游戏的全部事件循环 def main_process(self): """ 整个loop循环,游戏的自始至终的生命周期 """ # 第一步:做一下初始化工作,初始化我们的游戏6*6界面 del self.operationInterface[:] self.operationInterface.extend([ [0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0] ]) # 第二步:调用随机填充函数,填上初始数字(两次) self.random_fill() self.random_fill() # 第三步:进入主事件的循环 while True: # 首先要做的就是打印整个游戏屏幕 self.print_screen() # 接收用户输入的方向控制信息 controlkey=input("请输入移动的方向(上下左右:w/s/a/d):") if controlkey in self.controlKey: status,showMessage=self.logicOperator(controlkey) # 在得到一个当前游戏的信息之后,进行判断下一步操作(下面就是status非0的情况) if status: print(showMessage) if "y"==input("兄弟,再来一局吗?[Y/N]").lower(): # 这个傻子要再来一局! break else: # 这个傻子智商太低,被劝退了! sys.exit(0) # 她要再来一局,break就到这里了! self.main_process() #*****************************************************# # 下面就来启动整个游戏实例: if __name__=="__main__": MyGameOf_2048().main_process()

    注:这个代码实现的功能也就罢了,但是里面涉及到的Python语法的使用 简直到了让人佩服的地步,于是特此收藏 给我们初学Python的小伙伴们一起学习之用。

    Processed: 0.009, SQL: 9