#抬抬小手学Python# 用别人代码完成我的工作,愉快摸鱼「附源码」

Python05

#抬抬小手学Python# 用别人代码完成我的工作,愉快摸鱼「附源码」,第1张

模块是一个概念,它包含 1~N 个文件,如果文件是 Python 代码文件(就是.py 结尾的文件),那每个文件中可以包含函数,类等内容。

在公司工作,很多项目都是协作开发来完成,一个项目后面可能存在很多工程师,为了开发方便,每个人负责的功能函数或者类都尽量封装在一个 模块 中,模块英文请记住 module ,有的地方叫做 库 ,也有的地方叫做 包(package) ,对于现阶段的你来说,当成一样的内容就好。

互联网上存在大量的开源模块,这些模块最大的优势就是免费,很多时候使用这些模块能极大的提高编码效率,这也是很多人喜欢 Python 的原因之一。

模块学习的过程,不能按照语法结构来学习,它是一种抽象的知识,是一种代码的设计方式。例如将写好的函授放到模块中。

接下来就将上面的函数整合到一个模块中去,建立一个新的文件 stir_fry.py 然后将两个函数复制到新的文件中。

stir_fry.py 文件包含那两个函数

好了,完成任务,一个模块创建完毕了,这个 stir_fry.py 文件就是一个模块。

你现在脑中肯定出现黑人问号脸了,What?这就完了。是的,完了,一个低配模块完成。

下面就可以拿着这个模块给别人使用去了。会写模块成为大佬之后,就可以给新入行的菜鸟指点江山,写模块了。

在另一个文件中,可以通过 import 模块名 导入一个模块,例如导入刚才创建的 stir_fry 模块。

注意要新建一个文件,文件名随意但是不要与模块同名。

如果想要使用模块中的函数,只需要参考下述语法格式即可。

通过 stir_fry 调用模块中的函数。

当通过 import stir_fry 导入模块之后,该模块内的所有函数都一次性导入到新文件中了。

如果不想导入模块的所有函数,而只导入某个函数,使用一下语法可以解决该问题。

修改上一节案例:

直接导入模块中的函数,使用时不需要通过 模块名. 的方式调用,直接书写函数名即可。

导入模块中多个函数

语法格式如下:

导入模块所有函数

语法格式如下:

刚才通过模块导入函数你应该发现一个潜在的问题,就是函数名称太长怎么办,除了名称太长,还存在一种情况,模块中的函数名称与当前文件中函数的名称,存在重名的风险。此时可以学习一个新的内容,通过 as 给模块导入进来的函数起个别名,然后在该文件都使用别名进行编码。

语法格式如下:

上述内容应用到案例中如下述代码:

as 别名也可直接作用于模块,语法格式如下:

随着程序设计变的越来越复杂,只把函数放到模块中已经不能满足要求了,需要将更高级的内容放到模块中,也就是类。

首先在 dog_module.py 文件中定义一个类。

此时的 dog_module 就是模块的名称,而在该模块中只有一个类 Dog ,也可以在该模块中多创建几个类,例如:

与导入模块的函数部分知识一样,如果希望导入一个模块中的类,可以直接通过下述语法格式实现:

使用模块中的类,语法格式如下:

具体代码不在演示,自行完成吧。

导入模块的类和导入模块的的函数用法是一致的。

新建一个 demo.py 文件,在该文件导入 dog_module 模块中的类。

从模块中导入多个类

该方式与函数的导入也一致,语法格式如下:

导入模块中所有类

学到这里,你应该已经发现导入模块中的函数与导入模块中的类,从代码编写的角度几乎看不出区别,对比着学习即可。

导入类的时候也可以应用别名,同样使用 as 语法。

学习到这里你对模块是什么,模块怎么用已经有了一个基本认知,接下来先不用自己写一个特别牛的模块,我们先把一些常见的模块应用起来。

通过随机数模块可以获取到一个数字,它的使用场景非常广,例如 游戏 相关开发、验证码相关、抽奖相关,学习了随机数之后可以完成一些非常不错的小案例。

randint 方法

导入随机数模块之后,可以通过 randint 方法随机生成一个整数,例如下述代码:

反复运行代码会得到一个 1~10 之间的数字,由此可以 randint 方法中的参数含义。

choice 方法

通过 choice 方法可以配合列表实现一些效果,choice 可以随机返回列表中的一个元素。

如果你想知道 choice 方法的具体用法,还记得怎么查询吗?

shuffle 方法

该方法可以将一个列表的顺序打乱。

简单挑选了 random 模块中的三个方法做为说明,对于模块的学习,后面将为每个模块单开一篇文章书写。

时间模块是 Python 中非常重要的一个内置模块,很多场景都离不开它,内置模块就是 Python 安装好之后自带的模块。

time 方法

time 模块主要用于操作时间,该方法中存在一个 time 对象,使用 time 方法之后,可以获取从 1970年1月1日 00:00:00 到现在的秒数,很多地方会称作时间戳。

输出内容:

sleep 方法

该方法可以让程序暂停,该方法的参数是的单位是

使用语法格式为:

asctime 与 localtime 方法

以上两个方法都可以返回当前系统时间,只是展示的形式不同。

time 模块涉及的方法先只涉及这么多,后续滚雪球学习过程中在继续补充。

Python 还内置了很多模块,例如 sys 模块、os 模块、json 模块、pickle 模块、shelve 模块、xml 模块、re 模块、logging 模块等等内容,后续都将逐步学习到,有可能需要分开专题给大家讲解。

Python 模块,快速编码的一种途径,很多时候第三方模块可以帮你解决大多数常见编码场景,让你在编码的道路上飞奔。

话不多说,直接上菜

为了方便大家copy,我就不分段解释了

import turtle, random

# 定义一个类,用来画除了数字方块之外的图形

class BackGround(turtle.Turtle):

    def __init__(self):

        super().__init__()

        self.penup()

        self.ht()

    def draw_block(self):

        self.shape('bg.gif')  # 画出背景方块

        for i in allpos:

            self.goto(i)

            self.stamp()

        self.color('white', 'white')  # 画出其他背景

        self.goto(-215, 120)

        self.begin_fill()

        self.goto(215, 120)

        self.goto(215, 110)

        self.goto(-215, 110)

        self.end_fill()

        self.shape('title.gif')

        self.goto(-125, 210)

        self.stamp()

        self.shape('score.gif')

        self.goto(125, 245)

        self.stamp()

        self.shape('top_score.gif')

        self.goto(125, 170)

        self.stamp()

    # 游戏失败及达成2048的提示文字

    def judge(self):

        global flag_win, flag_win_lose_text

        self.color('blue')

        judge = 0  # 判断是否还有位置可以移动

        for i in block_dic.values():

            for j in block_dic.values():

                if i.num == 0 or i.num == j.num and i.distance(j) == 100:

                    judge += 1

        if judge == 0:  # 无位置可移动,游戏失败

            self.write('    GAME OVER\n重新开始请按空格键', align='center', font=('黑体', 30, 'bold'))

            flag_win_lose_text = False

        if flag_win is True:  # 此条件让2048达成的判断只能进行一次

            for k in block_dic.values():

                if k.num == 2048:  # 游戏达成

                    flag_win = False

                    self.write('    达成2048\n继续游戏请按回车键', align='center', font=('黑体', 30, 'bold'))

                    flag_win_lose_text = False

    def win_lose_clear(self):

        global flag_win_lose_text

        self.clear()

        flag_win_lose_text = True

    def show_score(self):  # 分值的显示

        global score, top_score

        if score >top_score:

            top_score = score

            with open('.\\score.txt', 'w') as f:

                f.write(f'{top_score}')

        self.color('white')

        self.goto(125, 210)

        self.clear()

        self.write(f'{score}', align='center', font=('Arial', 20, 'bold'))

        self.goto(125, 135)

        self.write(f'{top_score}', align='center', font=('Arial', 20, 'bold'))

# 数字方块类

class Block(turtle.Turtle):

    def __init__(self):

        super().__init__()

        self.ht()

        self.penup()

        self.num = 0

    def draw(self):

        self.clear()

        dic_draw = {2: '#eee6db', 4: '#efe0cd', 8: '#f5af7b',

                    16: '#fb9660', 32: '#f57d5a', 64: '#f95c3d',

                    128: '#eccc75', 256: '#eece61', 512: '#efc853',

                    1024: '#ebc53c', 2048: '#eec430', 4096: '#aeb879',

                    8192: '#aab767', 16384: '#a6b74f'}

        if self.num >0:  # 数字大于0,画出方块

            self.color(f'{dic_draw[self.num]}')  # 选择颜色

            self.begin_fill()

            self.goto(self.xcor()+48, self.ycor()+48)

            self.goto(self.xcor()-96, self.ycor())

            self.goto(self.xcor(), self.ycor()-96)

            self.goto(self.xcor()+96, self.ycor())

            self.goto(self.xcor(), self.ycor()+96)

            self.end_fill()

            self.goto(self.xcor()-48, self.ycor()-68)

            if self.num >4:  # 按照数字选择数字的颜色

                self.color('white')

            else:

                self.color('#6d6058')

            self.write(f'{self.num}', align='center', font=('Arial', 27, 'bold'))

            self.goto(self.xcor(), self.ycor()+20)

class Game():

    def init(self):

        back = BackGround()  # 实例画出游戏的背景

        back.draw_block()

        for i in allpos:  # 画出16个海龟对应16个数字块

            block = Block()

            block.goto(i)

            block_dic[i] = block

        game.grow()

    def restart(self):  # 重开游戏的方法

        global score, flag_win_lose_text

        score = 0

        for i in block_dic.values():

            i.num = 0

            i.clear()

        win_lose_text.clear()

        game.grow()

        flag_win_lose_text = True  # 此flag为游戏达成或失败出现提示语后的判断,要提示语被clear后才能继续move

    def grow(self):  # 随机出现一个2或4的数字块

        block_list = []

        for i in allpos:

            if block_dic[i].num == 0:

                block_list.append(block_dic[i])  # 挑出空白方块的海龟

        turtle_choice = random.choice(block_list)  # 随机选中其中一个海龟

        turtle_choice.num = random.choice([2, 2, 2, 2, 4])  # 赋属性num=2/4

        turtle_choice.draw()

        win_lose_text.judge()

        show_score_text.show_score()

        ms.update()

    def move_up(self):

        allpos1 = allpos[::4]  # 切片为四列

        allpos2 = allpos[1::4]

        allpos3 = allpos[2::4]

        allpos4 = allpos[3::4]

        self.move_move(allpos1, allpos2, allpos3, allpos4)

    def move_down(self):

        allpos1 = allpos[-4::-4]

        allpos2 = allpos[-3::-4]

        allpos3 = allpos[-2::-4]

        allpos4 = allpos[-1::-4]

        self.move_move(allpos1, allpos2, allpos3, allpos4)

    def move_left(self):

        allpos1 = allpos[:4]

        allpos2 = allpos[4:8]

        allpos3 = allpos[8:12]

        allpos4 = allpos[12:16]

        self.move_move(allpos1, allpos2, allpos3, allpos4)

    def move_right(self):

        allpos1 = allpos[-1:-5:-1]

        allpos2 = allpos[-5:-9:-1]

        allpos3 = allpos[-9:-13:-1]

        allpos4 = allpos[-13:-17:-1]

        self.move_move(allpos1, allpos2, allpos3, allpos4)

    def move_move(self, allpos1, allpos2, allpos3, allpos4):

        if flag_win_lose_text is True:

            count1 = self.move(allpos1)  # 四列或四行依次移动

            count2 = self.move(allpos2)

            count3 = self.move(allpos3)

            count4 = self.move(allpos4)

            if count1 or count2 or count3 or count4:  # 判断是否有方块移动,有才能继续出现新的数字块

                self.grow()

    def move(self, pos_list):

        num_list = []  # 为某一列或行的数字块海龟的坐标

        for i in pos_list:

            num_list.append(block_dic[i].num)  #  把这些海龟的NUM形成list

        new_num_list, count = self.list_oper(num_list)  #  只是list_oper的方法形成新的list

        for j in range(len(new_num_list)):  # 把新的list依次赋值给对应的海龟.num属性并调用draw()方法

            block_dic[pos_list[j]].num = new_num_list[j]

            block_dic[pos_list[j]].draw()

        return count

    def list_oper(self, num_list):  # num_list的操作,假设其为【2,0,2,2】

        global score

        count = True

        temp = []

        new_temp = []

        for j in num_list:

            if j != 0:

                temp.append(j)  # temp=[2,2,2]

        flag = True

        for k in range(len(temp)):

            if flag:

                if k <len(temp)-1 and temp[k] == temp[k+1]:

                    new_temp.append(temp[k]*2)

                    flag = False

                    score += temp[k]

                else:

                    new_temp.append(temp[k])  # new_temp=[4,2]

            else:

                flag = True

        for m in range(len(num_list)-len(new_temp)):

            new_temp.append(0)  # new_temp=[4,2,0,0]

        if new_temp == num_list:

            count = False  # 此变量判断num_list没有变化,数字块无移动

        return(new_temp, count)

if __name__ == '__main__':

    ms = turtle.Screen()  # 主窗口的设置

    ms.setup(430, 630, 400, 50)

    ms.bgcolor('gray')

    ms.title('2048')

    ms.tracer(0)

    ms.register_shape('bg.gif')

    ms.register_shape('title.gif')

    ms.register_shape('score.gif')

    ms.register_shape('top_score.gif')

    block_dic = {}  # 放数字方块海龟的字典,位置坐标为key,对应海龟为value

    allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),

              (-150, -50), (-50, -50), (50, -50), (150, -50),

              (-150, -150), (-50, -150), (50, -150), (150, -150),

              (-150, -250), (-50, -250), (50, -250), (150, -250)]

    flag_win = True  # 达成2048的判断,让达成的文字仅出现一次

    flag_win_lose_text = True  # 用来判断失败或成功的提示文字是否有被清除,不清除不能继续移动方块

    score = 0

    with open('.\\score.txt', 'r') as f:

        top_score = int(f.read())  #  读取score中的数据

    show_score_text = BackGround()

    win_lose_text = BackGround()

    game = Game()

    game.init()

    ms.listen()

    ms.onkey(game.move_up, 'Up')

    ms.onkey(game.move_down, 'Down')

    ms.onkey(game.move_left, 'Left')

    ms.onkey(game.move_right, 'Right')

    ms.onkey(win_lose_text.win_lose_clear, 'Return')

    ms.onkey(game.restart, 'space')

    ms.mainloop()

这是游戏界面:

欢迎挑战最高分。

要运行出来,必须本地要有这些文件:bg.gif,score.gif,title.gif,top_score.gif,score.txt

我把这些文件放在了群里,还有一些学习的资料,群号642109462,欢迎对python感兴趣的进群讨论。

支持作者的,可以关注和点赞。感谢你们!