<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>PYTHON学习</title>
        <link>https://paragraph.com/@python-2</link>
        <description>100天搞定PYTHON开发</description>
        <lastBuildDate>Wed, 03 Jun 2026 15:04:20 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <image>
            <title>PYTHON学习</title>
            <url>https://storage.googleapis.com/papyrus_images/d292e342a3f4f83c60e733be71ceb6694cee3e6b1eaf3422df4526f70cc8c8bd.jpg</url>
            <link>https://paragraph.com/@python-2</link>
        </image>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[python学习100天之第十天]]></title>
            <link>https://paragraph.com/@python-2/python-100</link>
            <guid>bYk08BlCACxAaAkMe9bv</guid>
            <pubDate>Mon, 09 May 2022 15:20:42 GMT</pubDate>
            <description><![CDATA[图形用户界面和游戏开发基于tkinter模块的GUIGUI是图形用户界面的缩写，图形化的用户界面对使用过计算机的人来说应该都不陌生，在此也无需进行赘述。Python默认的GUI开发模块是tkinter（在Python 3以前的版本中名为Tkinter），从这个名字就可以看出它是基于Tk的，Tk是一个工具包，最初是为Tcl设计的，后来被移植到很多其他的脚本语言中，它提供了跨平台的GUI控件。当然Tk并不是最新和最好的选择，也没有功能特别强大的GUI控件，事实上，开发GUI应用并不是Python最擅长的工作，如果真的需要使用Python开发GUI应用，wxPython、PyQt、PyGTK等模块都是不错的选择。 基本上使用tkinter来开发GUI应用需要以下5个步骤：导入tkinter模块中我们需要的东西。创建一个顶层窗口对象并用它来承载整个GUI应用。在顶层窗口对象上添加GUI组件。通过代码将这些GUI组件的功能组织起来。进入主事件循环(main loop)。下面的代码演示了如何使用tkinter做一个简单的GUI应用。import tkinter import tkinter....]]></description>
            <content:encoded><![CDATA[<h2 id="h-" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">图形用户界面和游戏开发</h2><h3 id="h-tkintergui" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">基于tkinter模块的GUI</h3><p>GUI是图形用户界面的缩写，图形化的用户界面对使用过计算机的人来说应该都不陌生，在此也无需进行赘述。Python默认的GUI开发模块是tkinter（在Python 3以前的版本中名为Tkinter），从这个名字就可以看出它是基于Tk的，Tk是一个工具包，最初是为Tcl设计的，后来被移植到很多其他的脚本语言中，它提供了跨平台的GUI控件。当然Tk并不是最新和最好的选择，也没有功能特别强大的GUI控件，事实上，开发GUI应用并不是Python最擅长的工作，如果真的需要使用Python开发GUI应用，wxPython、PyQt、PyGTK等模块都是不错的选择。</p><p>基本上使用tkinter来开发GUI应用需要以下5个步骤：</p><ol><li><p>导入tkinter模块中我们需要的东西。</p></li><li><p>创建一个顶层窗口对象并用它来承载整个GUI应用。</p></li><li><p>在顶层窗口对象上添加GUI组件。</p></li><li><p>通过代码将这些GUI组件的功能组织起来。</p></li><li><p>进入主事件循环(main loop)。</p></li></ol><p>下面的代码演示了如何使用tkinter做一个简单的GUI应用。</p><pre data-type="codeBlock" text="import tkinter
import tkinter.messagebox


def main():
    flag = True

    # 修改标签上的文字
    def change_label_text():
        nonlocal flag
        flag = not flag
        color, msg = (&apos;red&apos;, &apos;Hello, world!&apos;)\
            if flag else (&apos;blue&apos;, &apos;Goodbye, world!&apos;)
        label.config(text=msg, fg=color)

    # 确认退出
    def confirm_to_quit():
        if tkinter.messagebox.askokcancel(&apos;温馨提示&apos;, &apos;确定要退出吗?&apos;):
            top.quit()

    # 创建顶层窗口
    top = tkinter.Tk()
    # 设置窗口大小
    top.geometry(&apos;240x160&apos;)
    # 设置窗口标题
    top.title(&apos;小游戏&apos;)
    # 创建标签对象并添加到顶层窗口
    label = tkinter.Label(top, text=&apos;Hello, world!&apos;, font=&apos;Arial -32&apos;, fg=&apos;red&apos;)
    label.pack(expand=1)
    # 创建一个装按钮的容器
    panel = tkinter.Frame(top)
    # 创建按钮对象 指定添加到哪个容器中 通过command参数绑定事件回调函数
    button1 = tkinter.Button(panel, text=&apos;修改&apos;, command=change_label_text)
    button1.pack(side=&apos;left&apos;)
    button2 = tkinter.Button(panel, text=&apos;退出&apos;, command=confirm_to_quit)
    button2.pack(side=&apos;right&apos;)
    panel.pack(side=&apos;bottom&apos;)
    # 开启主事件循环
    tkinter.mainloop()


if __name__ == &apos;__main__&apos;:
    main()
"><code>import tkinter
import tkinter.messagebox


def main():
    <span class="hljs-attr">flag</span> = <span class="hljs-literal">True</span>

    <span class="hljs-comment"># 修改标签上的文字</span>
    def change_label_text():
        nonlocal flag
        <span class="hljs-attr">flag</span> = not flag
        color, <span class="hljs-attr">msg</span> = (<span class="hljs-string">'red'</span>, <span class="hljs-string">'Hello, world!'</span>)\
            if flag else ('blue', 'Goodbye, world!')
        label.config(<span class="hljs-attr">text</span>=msg, fg=color)

    <span class="hljs-comment"># 确认退出</span>
    def confirm_to_quit():
        if tkinter.messagebox.askokcancel('温馨提示', '确定要退出吗?'):
            top.quit()

    <span class="hljs-comment"># 创建顶层窗口</span>
    <span class="hljs-attr">top</span> = tkinter.Tk()
    <span class="hljs-comment"># 设置窗口大小</span>
    top.geometry('240x160')
    <span class="hljs-comment"># 设置窗口标题</span>
    top.title('小游戏')
    <span class="hljs-comment"># 创建标签对象并添加到顶层窗口</span>
    <span class="hljs-attr">label</span> = tkinter.Label(top, text=<span class="hljs-string">'Hello, world!'</span>, font=<span class="hljs-string">'Arial -32'</span>, fg=<span class="hljs-string">'red'</span>)
    label.pack(<span class="hljs-attr">expand</span>=<span class="hljs-number">1</span>)
    <span class="hljs-comment"># 创建一个装按钮的容器</span>
    <span class="hljs-attr">panel</span> = tkinter.Frame(top)
    <span class="hljs-comment"># 创建按钮对象 指定添加到哪个容器中 通过command参数绑定事件回调函数</span>
    <span class="hljs-attr">button1</span> = tkinter.Button(panel, text=<span class="hljs-string">'修改'</span>, command=change_label_text)
    button1.pack(<span class="hljs-attr">side</span>=<span class="hljs-string">'left'</span>)
    <span class="hljs-attr">button2</span> = tkinter.Button(panel, text=<span class="hljs-string">'退出'</span>, command=confirm_to_quit)
    button2.pack(<span class="hljs-attr">side</span>=<span class="hljs-string">'right'</span>)
    panel.pack(<span class="hljs-attr">side</span>=<span class="hljs-string">'bottom'</span>)
    <span class="hljs-comment"># 开启主事件循环</span>
    tkinter.mainloop()


if <span class="hljs-attr">__name__</span> == <span class="hljs-string">'__main__'</span>:
    main()
</code></pre><p>需要说明的是，GUI应用通常是事件驱动式的，之所以要进入主事件循环就是要监听鼠标、键盘等各种事件的发生并执行对应的代码对事件进行处理，因为事件会持续的发生，所以需要这样的一个循环一直运行着等待下一个事件的发生。另一方面，Tk为控件的摆放提供了三种布局管理器，通过布局管理器可以对控件进行定位，这三种布局管理器分别是：Placer（开发者提供控件的大小和摆放位置）、Packer（自动将控件填充到合适的位置）和Grid（基于网格坐标来摆放控件），此处不进行赘述。</p><h3 id="h-pygame" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">使用Pygame进行游戏开发</h3><p>Pygame是一个开源的Python模块，专门用于多媒体应用（如电子游戏）的开发，其中包含对图像、声音、视频、事件、碰撞等的支持。Pygame建立在<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://zh.wikipedia.org/wiki/SDL">SDL</a>的基础上，SDL是一套跨平台的多媒体开发库，用C语言实现，被广泛的应用于游戏、模拟器、播放器等的开发。而Pygame让游戏开发者不再被底层语言束缚，可以更多的关注游戏的功能和逻辑。</p><p>下面我们来完成一个简单的小游戏，游戏的名字叫“大球吃小球”，当然完成这个游戏并不是重点，学会使用Pygame也不是重点，最重要的我们要在这个过程中体会如何使用前面讲解的面向对象程序设计，学会用这种编程思想去解决现实中的问题。</p><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">制作游戏窗口</h4><pre data-type="codeBlock" text="import pygame


def main():
    # 初始化导入的pygame中的模块
    pygame.init()
    # 初始化用于显示的窗口并设置窗口尺寸
    screen = pygame.display.set_mode((800, 600))
    # 设置当前窗口的标题
    pygame.display.set_caption(&apos;大球吃小球&apos;)
    running = True
    # 开启一个事件循环处理发生的事件
    while running:
        # 从消息队列中获取事件并对事件进行处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


if __name__ == &apos;__main__&apos;:
    main()
"><code>import pygame


def main():
    <span class="hljs-comment"># 初始化导入的pygame中的模块</span>
    pygame.init()
    <span class="hljs-comment"># 初始化用于显示的窗口并设置窗口尺寸</span>
    <span class="hljs-attr">screen</span> = pygame.display.set_mode((<span class="hljs-number">800</span>, <span class="hljs-number">600</span>))
    <span class="hljs-comment"># 设置当前窗口的标题</span>
    pygame.display.set_caption('大球吃小球')
    <span class="hljs-attr">running</span> = <span class="hljs-literal">True</span>
    <span class="hljs-comment"># 开启一个事件循环处理发生的事件</span>
    while running:
        <span class="hljs-comment"># 从消息队列中获取事件并对事件进行处理</span>
        for event in pygame.event.get():
            if <span class="hljs-attr">event.type</span> == pygame.QUIT:
                <span class="hljs-attr">running</span> = <span class="hljs-literal">False</span>


if <span class="hljs-attr">__name__</span> == <span class="hljs-string">'__main__'</span>:
    main()
</code></pre><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">在窗口中绘图</h4><p>可以通过pygame中draw模块的函数在窗口上绘图，可以绘制的图形包括：线条、矩形、多边形、圆、椭圆、圆弧等。需要说明的是，屏幕坐标系是将屏幕左上角设置为坐标原点<code>(0, 0)</code>，向右是x轴的正向，向下是y轴的正向，在表示位置或者设置尺寸的时候，我们默认的单位都是<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://zh.wikipedia.org/wiki/%E5%83%8F%E7%B4%A0">像素</a>。所谓像素就是屏幕上的一个点，你可以用浏览图片的软件试着将一张图片放大若干倍，就可以看到这些点。pygame中表示颜色用的是色光<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://zh.wikipedia.org/wiki/%E5%8E%9F%E8%89%B2">三原色</a>表示法，即通过一个元组或列表来指定颜色的RGB值，每个值都在0~255之间，因为是每种原色都用一个8位（bit）的值来表示，三种颜色相当于一共由24位构成，这也就是常说的“24位颜色表示法”。</p><pre data-type="codeBlock" text="import pygame


def main():
    # 初始化导入的pygame中的模块
    pygame.init()
    # 初始化用于显示的窗口并设置窗口尺寸
    screen = pygame.display.set_mode((800, 600))
    # 设置当前窗口的标题
    pygame.display.set_caption(&apos;大球吃小球&apos;)
    # 设置窗口的背景色(颜色是由红绿蓝三原色构成的元组)
    screen.fill((242, 242, 242))
    # 绘制一个圆(参数分别是: 屏幕, 颜色, 圆心位置, 半径, 0表示填充圆)
    pygame.draw.circle(screen, (255, 0, 0,), (100, 100), 30, 0)
    # 刷新当前窗口(渲染窗口将绘制的图像呈现出来)
    pygame.display.flip()
    running = True
    # 开启一个事件循环处理发生的事件
    while running:
        # 从消息队列中获取事件并对事件进行处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


if __name__ == &apos;__main__&apos;:
    main()
"><code>import pygame


def main():
    <span class="hljs-comment"># 初始化导入的pygame中的模块</span>
    pygame.init()
    <span class="hljs-comment"># 初始化用于显示的窗口并设置窗口尺寸</span>
    <span class="hljs-attr">screen</span> = pygame.display.set_mode((<span class="hljs-number">800</span>, <span class="hljs-number">600</span>))
    <span class="hljs-comment"># 设置当前窗口的标题</span>
    pygame.display.set_caption('大球吃小球')
    <span class="hljs-comment"># 设置窗口的背景色(颜色是由红绿蓝三原色构成的元组)</span>
    screen.fill((242, 242, 242))
    <span class="hljs-comment"># 绘制一个圆(参数分别是: 屏幕, 颜色, 圆心位置, 半径, 0表示填充圆)</span>
    pygame.draw.circle(screen, (255, 0, 0,), (100, 100), 30, 0)
    <span class="hljs-comment"># 刷新当前窗口(渲染窗口将绘制的图像呈现出来)</span>
    pygame.display.flip()
    <span class="hljs-attr">running</span> = <span class="hljs-literal">True</span>
    <span class="hljs-comment"># 开启一个事件循环处理发生的事件</span>
    while running:
        <span class="hljs-comment"># 从消息队列中获取事件并对事件进行处理</span>
        for event in pygame.event.get():
            if <span class="hljs-attr">event.type</span> == pygame.QUIT:
                <span class="hljs-attr">running</span> = <span class="hljs-literal">False</span>


if <span class="hljs-attr">__name__</span> == <span class="hljs-string">'__main__'</span>:
    main()
</code></pre><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">加载图像</h4><p>如果需要直接加载图像到窗口上，可以使用pygame中image模块的函数来加载图像，再通过之前获得的窗口对象的<code>blit</code>方法渲染图像，代码如下所示。</p><pre data-type="codeBlock" text="import pygame


def main():
    # 初始化导入的pygame中的模块
    pygame.init()
    # 初始化用于显示的窗口并设置窗口尺寸
    screen = pygame.display.set_mode((800, 600))
    # 设置当前窗口的标题
    pygame.display.set_caption(&apos;大球吃小球&apos;)
    # 设置窗口的背景色(颜色是由红绿蓝三原色构成的元组)
    screen.fill((255, 255, 255))
    # 通过指定的文件名加载图像
    ball_image = pygame.image.load(&apos;./res/ball.png&apos;)
    # 在窗口上渲染图像
    screen.blit(ball_image, (50, 50))
    # 刷新当前窗口(渲染窗口将绘制的图像呈现出来)
    pygame.display.flip()
    running = True
    # 开启一个事件循环处理发生的事件
    while running:
        # 从消息队列中获取事件并对事件进行处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


if __name__ == &apos;__main__&apos;:
    main()
"><code>import pygame


def main():
    <span class="hljs-comment"># 初始化导入的pygame中的模块</span>
    pygame.init()
    <span class="hljs-comment"># 初始化用于显示的窗口并设置窗口尺寸</span>
    <span class="hljs-attr">screen</span> = pygame.display.set_mode((<span class="hljs-number">800</span>, <span class="hljs-number">600</span>))
    <span class="hljs-comment"># 设置当前窗口的标题</span>
    pygame.display.set_caption('大球吃小球')
    <span class="hljs-comment"># 设置窗口的背景色(颜色是由红绿蓝三原色构成的元组)</span>
    screen.fill((255, 255, 255))
    <span class="hljs-comment"># 通过指定的文件名加载图像</span>
    <span class="hljs-attr">ball_image</span> = pygame.image.load(<span class="hljs-string">'./res/ball.png'</span>)
    <span class="hljs-comment"># 在窗口上渲染图像</span>
    screen.blit(ball_image, (50, 50))
    <span class="hljs-comment"># 刷新当前窗口(渲染窗口将绘制的图像呈现出来)</span>
    pygame.display.flip()
    <span class="hljs-attr">running</span> = <span class="hljs-literal">True</span>
    <span class="hljs-comment"># 开启一个事件循环处理发生的事件</span>
    while running:
        <span class="hljs-comment"># 从消息队列中获取事件并对事件进行处理</span>
        for event in pygame.event.get():
            if <span class="hljs-attr">event.type</span> == pygame.QUIT:
                <span class="hljs-attr">running</span> = <span class="hljs-literal">False</span>


if <span class="hljs-attr">__name__</span> == <span class="hljs-string">'__main__'</span>:
    main()
</code></pre><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">实现动画效果</h4><p>说到<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://zh.wikipedia.org/wiki/%E5%8A%A8%E7%94%BB">动画</a>这个词大家都不会陌生，事实上要实现动画效果，本身的原理也非常简单，就是将不连续的图片连续的播放，只要每秒钟达到了一定的帧数，那么就可以做出比较流畅的动画效果。如果要让上面代码中的小球动起来，可以将小球的位置用变量来表示，并在循环中修改小球的位置再刷新整个窗口即可。</p><pre data-type="codeBlock" text="import pygame


def main():
    # 初始化导入的pygame中的模块
    pygame.init()
    # 初始化用于显示的窗口并设置窗口尺寸
    screen = pygame.display.set_mode((800, 600))
    # 设置当前窗口的标题
    pygame.display.set_caption(&apos;大球吃小球&apos;)
    # 定义变量来表示小球在屏幕上的位置
    x, y = 50, 50
    running = True
    # 开启一个事件循环处理发生的事件
    while running:
        # 从消息队列中获取事件并对事件进行处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        screen.fill((255, 255, 255))
        pygame.draw.circle(screen, (255, 0, 0,), (x, y), 30, 0)
        pygame.display.flip()
        # 每隔50毫秒就改变小球的位置再刷新窗口
        pygame.time.delay(50)
        x, y = x + 5, y + 5


if __name__ == &apos;__main__&apos;:
    main()
"><code>import pygame


def main():
    <span class="hljs-comment"># 初始化导入的pygame中的模块</span>
    pygame.init()
    <span class="hljs-comment"># 初始化用于显示的窗口并设置窗口尺寸</span>
    <span class="hljs-attr">screen</span> = pygame.display.set_mode((<span class="hljs-number">800</span>, <span class="hljs-number">600</span>))
    <span class="hljs-comment"># 设置当前窗口的标题</span>
    pygame.display.set_caption('大球吃小球')
    <span class="hljs-comment"># 定义变量来表示小球在屏幕上的位置</span>
    x, <span class="hljs-attr">y</span> = <span class="hljs-number">50</span>, <span class="hljs-number">50</span>
    <span class="hljs-attr">running</span> = <span class="hljs-literal">True</span>
    <span class="hljs-comment"># 开启一个事件循环处理发生的事件</span>
    while running:
        <span class="hljs-comment"># 从消息队列中获取事件并对事件进行处理</span>
        for event in pygame.event.get():
            if <span class="hljs-attr">event.type</span> == pygame.QUIT:
                <span class="hljs-attr">running</span> = <span class="hljs-literal">False</span>
        screen.fill((255, 255, 255))
        pygame.draw.circle(screen, (255, 0, 0,), (x, y), 30, 0)
        pygame.display.flip()
        <span class="hljs-comment"># 每隔50毫秒就改变小球的位置再刷新窗口</span>
        pygame.time.delay(50)
        x, <span class="hljs-attr">y</span> = x + <span class="hljs-number">5</span>, y + <span class="hljs-number">5</span>


if <span class="hljs-attr">__name__</span> == <span class="hljs-string">'__main__'</span>:
    main()
</code></pre><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">碰撞检测</h4><p>通常一个游戏中会有很多对象出现，而这些对象之间的“碰撞”在所难免，比如炮弹击中了飞机、箱子撞到了地面等。碰撞检测在绝大多数的游戏中都是一个必须得处理的至关重要的问题，pygame的sprite（动画精灵）模块就提供了对碰撞检测的支持，这里我们暂时不介绍sprite模块提供的功能，因为要检测两个小球有没有碰撞其实非常简单，只需要检查球心的距离有没有小于两个球的半径之和。为了制造出更多的小球，我们可以通过对鼠标事件的处理，在点击鼠标的位置创建颜色、大小和移动速度都随机的小球，当然要做到这一点，我们可以把之前学习到的面向对象的知识应用起来。</p><pre data-type="codeBlock" text="from enum import Enum, unique
from math import sqrt
from random import randint

import pygame


@unique
class Color(Enum):
    &quot;&quot;&quot;颜色&quot;&quot;&quot;

    RED = (255, 0, 0)
    GREEN = (0, 255, 0)
    BLUE = (0, 0, 255)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GRAY = (242, 242, 242)

    @staticmethod
    def random_color():
        &quot;&quot;&quot;获得随机颜色&quot;&quot;&quot;
        r = randint(0, 255)
        g = randint(0, 255)
        b = randint(0, 255)
        return (r, g, b)


class Ball(object):
    &quot;&quot;&quot;球&quot;&quot;&quot;

    def __init__(self, x, y, radius, sx, sy, color=Color.RED):
        &quot;&quot;&quot;初始化方法&quot;&quot;&quot;
        self.x = x
        self.y = y
        self.radius = radius
        self.sx = sx
        self.sy = sy
        self.color = color
        self.alive = True

    def move(self, screen):
        &quot;&quot;&quot;移动&quot;&quot;&quot;
        self.x += self.sx
        self.y += self.sy
        if self.x - self.radius &lt;= 0 or \
                self.x + self.radius &gt;= screen.get_width():
            self.sx = -self.sx
        if self.y - self.radius &lt;= 0 or \
                self.y + self.radius &gt;= screen.get_height():
            self.sy = -self.sy

    def eat(self, other):
        &quot;&quot;&quot;吃其他球&quot;&quot;&quot;
        if self.alive and other.alive and self != other:
            dx, dy = self.x - other.x, self.y - other.y
            distance = sqrt(dx ** 2 + dy ** 2)
            if distance &lt; self.radius + other.radius \
                    and self.radius &gt; other.radius:
                other.alive = False
                self.radius = self.radius + int(other.radius * 0.146)

    def draw(self, screen):
        &quot;&quot;&quot;在窗口上绘制球&quot;&quot;&quot;
        pygame.draw.circle(screen, self.color,
                           (self.x, self.y), self.radius, 0)
"><code><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, unique
<span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> sqrt
<span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint

<span class="hljs-keyword">import</span> pygame


<span class="hljs-meta">@unique</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Color</span>(<span class="hljs-title class_ inherited__">Enum</span>):
    <span class="hljs-string">"""颜色"""</span>

    RED = (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
    GREEN = (<span class="hljs-number">0</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0</span>)
    BLUE = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">255</span>)
    BLACK = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
    WHITE = (<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>)
    GRAY = (<span class="hljs-number">242</span>, <span class="hljs-number">242</span>, <span class="hljs-number">242</span>)

<span class="hljs-meta">    @staticmethod</span>
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">random_color</span>():
        <span class="hljs-string">"""获得随机颜色"""</span>
        r = randint(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>)
        g = randint(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>)
        b = randint(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>)
        <span class="hljs-keyword">return</span> (r, g, b)


<span class="hljs-keyword">class</span> <span class="hljs-title class_">Ball</span>(<span class="hljs-title class_ inherited__">object</span>):
    <span class="hljs-string">"""球"""</span>

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, x, y, radius, sx, sy, color=Color.RED</span>):
        <span class="hljs-string">"""初始化方法"""</span>
        self.x = x
        self.y = y
        self.radius = radius
        self.sx = sx
        self.sy = sy
        self.color = color
        self.alive = <span class="hljs-literal">True</span>

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">move</span>(<span class="hljs-params">self, screen</span>):
        <span class="hljs-string">"""移动"""</span>
        self.x += self.sx
        self.y += self.sy
        <span class="hljs-keyword">if</span> self.x - self.radius &#x3C;= <span class="hljs-number">0</span> <span class="hljs-keyword">or</span> \
                self.x + self.radius >= screen.get_width():
            self.sx = -self.sx
        <span class="hljs-keyword">if</span> self.y - self.radius &#x3C;= <span class="hljs-number">0</span> <span class="hljs-keyword">or</span> \
                self.y + self.radius >= screen.get_height():
            self.sy = -self.sy

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">eat</span>(<span class="hljs-params">self, other</span>):
        <span class="hljs-string">"""吃其他球"""</span>
        <span class="hljs-keyword">if</span> self.alive <span class="hljs-keyword">and</span> other.alive <span class="hljs-keyword">and</span> self != other:
            dx, dy = self.x - other.x, self.y - other.y
            distance = sqrt(dx ** <span class="hljs-number">2</span> + dy ** <span class="hljs-number">2</span>)
            <span class="hljs-keyword">if</span> distance &#x3C; self.radius + other.radius \
                    <span class="hljs-keyword">and</span> self.radius > other.radius:
                other.alive = <span class="hljs-literal">False</span>
                self.radius = self.radius + <span class="hljs-built_in">int</span>(other.radius * <span class="hljs-number">0.146</span>)

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">draw</span>(<span class="hljs-params">self, screen</span>):
        <span class="hljs-string">"""在窗口上绘制球"""</span>
        pygame.draw.circle(screen, self.color,
                           (self.x, self.y), self.radius, <span class="hljs-number">0</span>)
</code></pre><h4 id="h-" class="text-xl font-header !mt-6 !mb-3 first:!mt-0 first:!mb-0">事件处理</h4><p>可以在事件循环中对鼠标事件进行处理，通过事件对象的<code>type</code>属性可以判定事件类型，再通过<code>pos</code>属性就可以获得鼠标点击的位置。如果要处理键盘事件也是在这个地方，做法与处理鼠标事件类似。</p><pre data-type="codeBlock" text="def main():
    # 定义用来装所有球的容器
    balls = []
    # 初始化导入的pygame中的模块
    pygame.init()
    # 初始化用于显示的窗口并设置窗口尺寸
    screen = pygame.display.set_mode((800, 600))
    # 设置当前窗口的标题
    pygame.display.set_caption(&apos;大球吃小球&apos;)
    running = True
    # 开启一个事件循环处理发生的事件
    while running:
        # 从消息队列中获取事件并对事件进行处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            # 处理鼠标事件的代码
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                # 获得点击鼠标的位置
                x, y = event.pos
                radius = randint(10, 100)
                sx, sy = randint(-10, 10), randint(-10, 10)
                color = Color.random_color()
                # 在点击鼠标的位置创建一个球(大小、速度和颜色随机)
                ball = Ball(x, y, radius, sx, sy, color)
                # 将球添加到列表容器中
                balls.append(ball)
        screen.fill((255, 255, 255))
        # 取出容器中的球 如果没被吃掉就绘制 被吃掉了就移除
        for ball in balls:
            if ball.alive:
                ball.draw(screen)
            else:
                balls.remove(ball)
        pygame.display.flip()
        # 每隔50毫秒就改变球的位置再刷新窗口
        pygame.time.delay(50)
        for ball in balls:
            ball.move(screen)
            # 检查球有没有吃到其他的球
            for other in balls:
                ball.eat(other)


if __name__ == &apos;__main__&apos;:
    main()
"><code>def main():
    <span class="hljs-comment"># 定义用来装所有球的容器</span>
    <span class="hljs-attr">balls</span> = []
    <span class="hljs-comment"># 初始化导入的pygame中的模块</span>
    pygame.init()
    <span class="hljs-comment"># 初始化用于显示的窗口并设置窗口尺寸</span>
    <span class="hljs-attr">screen</span> = pygame.display.set_mode((<span class="hljs-number">800</span>, <span class="hljs-number">600</span>))
    <span class="hljs-comment"># 设置当前窗口的标题</span>
    pygame.display.set_caption('大球吃小球')
    <span class="hljs-attr">running</span> = <span class="hljs-literal">True</span>
    <span class="hljs-comment"># 开启一个事件循环处理发生的事件</span>
    while running:
        <span class="hljs-comment"># 从消息队列中获取事件并对事件进行处理</span>
        for event in pygame.event.get():
            if <span class="hljs-attr">event.type</span> == pygame.QUIT:
                <span class="hljs-attr">running</span> = <span class="hljs-literal">False</span>
            <span class="hljs-comment"># 处理鼠标事件的代码</span>
            if <span class="hljs-attr">event.type</span> == pygame.MOUSEBUTTONDOWN and event.button == <span class="hljs-number">1</span>:
                <span class="hljs-comment"># 获得点击鼠标的位置</span>
                x, <span class="hljs-attr">y</span> = event.pos
                <span class="hljs-attr">radius</span> = randint(<span class="hljs-number">10</span>, <span class="hljs-number">100</span>)
                sx, <span class="hljs-attr">sy</span> = randint(-<span class="hljs-number">10</span>, <span class="hljs-number">10</span>), randint(-<span class="hljs-number">10</span>, <span class="hljs-number">10</span>)
                <span class="hljs-attr">color</span> = Color.random_color()
                <span class="hljs-comment"># 在点击鼠标的位置创建一个球(大小、速度和颜色随机)</span>
                <span class="hljs-attr">ball</span> = Ball(x, y, radius, sx, sy, color)
                <span class="hljs-comment"># 将球添加到列表容器中</span>
                balls.append(ball)
        screen.fill((255, 255, 255))
        <span class="hljs-comment"># 取出容器中的球 如果没被吃掉就绘制 被吃掉了就移除</span>
        for ball in balls:
            if ball.alive:
                ball.draw(screen)
            else:
                balls.remove(ball)
        pygame.display.flip()
        <span class="hljs-comment"># 每隔50毫秒就改变球的位置再刷新窗口</span>
        pygame.time.delay(50)
        for ball in balls:
            ball.move(screen)
            <span class="hljs-comment"># 检查球有没有吃到其他的球</span>
            for other in balls:
                ball.eat(other)


if <span class="hljs-attr">__name__</span> == <span class="hljs-string">'__main__'</span>:
    main()
</code></pre><p>上面的两段代码合在一起，我们就完成了“大球吃小球”的游戏（如下图所示），准确的说它算不上一个游戏，但是做一个小游戏的基本知识我们已经通过这个例子告诉大家了，有了这些知识已经可以开始你的小游戏开发之旅了。其实上面的代码中还有很多值得改进的地方，比如刷新窗口以及让球移动起来的代码并不应该放在事件循环中，等学习了多线程的知识后，用一个后台线程来处理这些事可能是更好的选择。如果希望获得更好的用户体验，我们还可以在游戏中加入背景音乐以及在球与球发生碰撞时播放音效，利用pygame的mixer和music模块，我们可以很容易的做到这一点，大家可以自行了解这方面的知识。事实上，想了解更多的关于pygame的知识，最好的教程是<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.pygame.org/news">pygame的官方网站</a>，如果英语没毛病就可以赶紧去看看啦。 如果想开发<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://zh.wikipedia.org/wiki/3D%E6%B8%B8%E6%88%8F">3D游戏</a>，pygame就显得力不从心了，对3D游戏开发如果有兴趣的读者不妨看看<a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://www.panda3d.org/">Panda3D</a>。</p>]]></content:encoded>
            <author>python-2@newsletter.paragraph.com (PYTHON学习)</author>
        </item>
    </channel>
</rss>