Python游戏开发:Pygame游戏控制指南:键盘输入、鼠标输入与游戏循环详解

Python自学手册 编程文档

Pygame游戏控制详解

Pygame是一个开源的Python游戏开发库,它提供了许多用于创建游戏的功能和工具。在Pygame中,游戏控制是实现玩家与游戏互动的重要部分。本文档将详细介绍Pygame游戏控制的各个方面,包括键盘输入、鼠标输入、游戏循环等。

键盘输入

键盘输入是玩家与游戏互动的主要方式之一。Pygame提供了方便的键盘输入处理函数,使得处理键盘输入变得简单而高效。以下是一个处理键盘输入的示例代码:



python

<table><tbody style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="1" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="1" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">import</span> pygame</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="2" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="2" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="3" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="3" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 初始化Pygame  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="4" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="4" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">pygame.init()</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="5" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="5" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="6" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="6" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 设置窗口大小  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="7" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="7" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">screen = pygame.display.set_mode((<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">800</span>, <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">600</span>))</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="8" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="8" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="9" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="9" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 游戏循环  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="10" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="10" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">True</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="11" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="11" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">while</span> running:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="12" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="12" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 处理事件  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="13" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="13" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">for</span> event <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">in</span> pygame.event.get():</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="14" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="14" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> event.<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">type</span> == pygame.QUIT:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="15" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="15" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">False</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="16" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="16" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">elif</span> event.<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">type</span> == pygame.KEYDOWN:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="17" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="17" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 处理键盘按下事件  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="18" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="18" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> event.key == pygame.K_LEFT:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="19" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="19" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 左箭头键按下,移动角色向左移动  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="20" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="20" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">pass</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 在这里编写移动角色的逻辑  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="21" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="21" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">elif</span> event.key == pygame.K_RIGHT:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="22" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="22" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 右箭头键按下,移动角色向右移动  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="23" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="23" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">pass</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 在这里编写移动角色的逻辑  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="24" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="24" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">elif</span> event.key == pygame.K_UP:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="25" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="25" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 上箭头键按下,移动角色向上移动  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="26" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="26" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">pass</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 在这里编写移动角色的逻辑  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="27" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="27" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">elif</span> event.key == pygame.K_DOWN:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="28" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="28" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 下箭头键按下,移动角色向下移动  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="29" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="29" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">pass</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 在这里编写移动角色的逻辑</span></td></tr></tbody></table>


在上面的代码中,我们使用pygame.event.get()函数来获取事件队列,然后对每个事件进行处理。如果事件类型是pygame.QUIT,则退出游戏循环;如果事件类型是pygame.KEYDOWN,则处理键盘按下事件。在处理键盘按下事件时,我们可以通过event.key属性获取按下的键,然后根据不同的键编写相应的逻辑。例如,当按下左箭头键时,我们可以编写移动角色向左移动的逻辑。

鼠标输入

除了键盘输入外,鼠标输入也是玩家与游戏互动的重要方式之一。Pygame提供了鼠标输入处理的函数,使得处理鼠标输入变得简单而高效。以下是一个处理鼠标输入的示例代码:



python

<table><tbody style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="1" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="1" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">import</span> pygame</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="2" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="2" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="3" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="3" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 初始化Pygame  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="4" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="4" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">pygame.init()</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="5" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="5" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="6" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="6" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 设置窗口大小  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="7" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="7" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">screen = pygame.display.set_mode((<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">800</span>, <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">600</span>))</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="8" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="8" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="9" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="9" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 游戏循环  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="10" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="10" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">True</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="11" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="11" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">while</span> running:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="12" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="12" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 处理事件  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="13" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="13" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">for</span> event <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">in</span> pygame.event.get():</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="14" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="14" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> event.<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">type</span> == pygame.QUIT:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="15" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="15" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">False</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="16" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="16" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">elif</span> event.<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">type</span> == pygame.MOUSEBUTTONDOWN:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="17" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="17" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 处理鼠标按下事件  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="18" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="18" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> event.button == <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">1</span>: <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 左键按下  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="19" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="19" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 左键点击事件处理逻辑  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="20" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="20" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">pass</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 在这里编写左键点击事件的逻辑</span></td></tr></tbody></table>


在上面的代码中,我们使用pygame.event.get()函数来获取事件队列,然后对每个事件进行处理。如果事件类型是pygame.QUIT,则退出游戏循环;如果事件类型是pygame.MOUSEBUTTONDOWN,则处理鼠标按下事件。在处理鼠标按下事件时,我们可以通过event.button属性获取按下的鼠标按键,然后根据不同的按键编写相应的逻辑。例如,当左键按下时,我们可以编写左键点击事件的逻辑。

游戏循环

游戏循环是游戏的核心部分,它负责控制游戏的运行和更新。在Pygame中,游戏循环通常包括以下几个步骤:

  1. 初始化:在游戏开始前,需要对游戏进行初始化,包括设置窗口大小、加载图像、声音等资源。

  2. 游戏逻辑:在游戏循环中,需要处理游戏逻辑,包括角色的移动、敌人的攻击、碰撞检测等。

  3. 渲染:根据游戏逻辑更新游戏画面,将新的图像绘制到屏幕上。

  4. 更新:更新游戏状态,包括角色的位置、敌人的状态等。

  5. 控制时间:为了保证游戏的流畅性和稳定性,需要在游戏循环中控制时间,避免程序运行过快或过慢。

以下是一个简单的Pygame游戏循环示例代码:



python

<table><tbody style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="1" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="1" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">import</span> pygame</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="2" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="2" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="3" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="3" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 初始化Pygame  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="4" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="4" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">pygame.init()</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="5" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="5" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="6" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="6" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 设置窗口大小  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="7" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="7" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">screen = pygame.display.set_mode((<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">800</span>, <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">600</span>))</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="8" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="8" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="9" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="9" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 加载图像  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="10" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="10" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">image = pygame.image.load(<span style="-webkit-font-smoothing: antialiased;list-style: none;color: green;line-height: 24px;">'image.png'</span>)</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="11" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="11" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="12" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="12" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 设置初始位置  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="13" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="13" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect = image.get_rect()</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="14" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="14" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect.x = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">100</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="15" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="15" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect.y = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">100</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="16" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="16" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="17" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="17" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 游戏循环  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="18" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="18" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">True</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="19" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="19" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">while</span> running:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="20" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="20" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 处理事件  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="21" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="21" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">for</span> event <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">in</span> pygame.event.get():</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="22" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="22" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> event.<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">type</span> == pygame.QUIT:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="23" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="23" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">False</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="24" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="24" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">elif</span> event.<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">type</span> == pygame.KEYDOWN:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="25" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="25" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> event.key == pygame.K_ESCAPE:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="26" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="26" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">running = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">False</span>  </td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="27" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="27" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="28" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="28" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 控制时间  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="29" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="29" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">clock = pygame.time.Clock()</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="30" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="30" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">dt = clock.tick(<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">60</span>) / <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">1000.0</span>  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 控制每秒帧数,并计算时间差  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="31" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="31" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="32" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="32" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 游戏逻辑和渲染  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="33" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="33" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">screen.fill((<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">255</span>, <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">255</span>, <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">255</span>))  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 清屏  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="34" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="34" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">screen.blit(image, rect)  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 将图像绘制到屏幕上  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="35" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="35" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">pygame.display.flip()  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 更新显示缓冲区,使绘制内容可见  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="36" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="36" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="37" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="37" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 更新位置和速度  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="38" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="38" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect.x += <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">5</span> * dt  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 水平方向移动,速度为5像素/秒  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="39" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="39" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect.y += <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> * dt  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 垂直方向不移动,速度为0像素/秒  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="40" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="40" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="41" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="41" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 检查是否超出边界  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="42" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="42" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> rect.x &lt; <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">or</span> rect.x &gt; <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">800</span> - rect.width:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="43" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="43" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect.x = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> rect.x &lt; <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">else</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">800</span> - rect.width  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 超出边界则重置位置  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="44" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="44" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> rect.y &lt; <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">or</span> rect.y &gt; <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">600</span> - rect.height:</td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="45" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="45" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">rect.y = <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">if</span> rect.y &lt; <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">0</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(121, 40, 161);line-height: 24px;">else</span> <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">600</span> - rect.height  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 超出边界则重置位置  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="46" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="46" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="47" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="47" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 控制时间,避免程序运行过快或过慢  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="48" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="48" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">clock = pygame.time.Clock()  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 重置时钟对象,确保每帧的时间间隔一致  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="49" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="49" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">dt = clock.tick(<span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">60</span>) / <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(170, 93, 0);line-height: 24px;">1000.0</span>  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 控制每秒帧数,并计算时间差,确保程序运行稳定  </span></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="50" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="50" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><br></td></tr><tr style="-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;"><td data-line-number="51" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;color: rgb(190, 191, 208);letter-spacing: 0px;max-width: 50px;overflow: hidden;text-align: justify;user-select: none;vertical-align: top;white-space-collapse: collapse;" width="30"><br></td><td data-line-number="51" style="padding: 0px;-webkit-font-smoothing: antialiased;list-style: none;line-height: 24px;">pygame.display.flip()  <span style="-webkit-font-smoothing: antialiased;list-style: none;color: rgb(105, 105, 105);line-height: 24px;"># 更新显示缓冲区,使绘制内容可见</span></td></tr></tbody></table>


**点击下方安全链接前往获取**

**[CSDN大礼包:《Python入门&进阶学习资源包》免费分享](https://mp.weixin.qq.com/s/b5jwyLC8FsKhg6O8ynyPUA)**

### 👉**Python实战案例**👈

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=https%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_jpg%2FicibaTNbiaaf5GZ01GhIvmG8eqmrXtRDHVyqEcxswMgOmYDTmNdDrWWyaDsNEvG5areslHPV3tJLqVsY6fSziaVkeA%2F640%3Fwx_fmt%3Djpeg%26wxfrom%3D5%26wx_lazy%3D1%26wx_co%3D1&pos_id=img-UsL4R429-1710143393028)

![图片](https://img-blog.csdnimg.cn/img_convert/4b72734066106939d3940f90192b01ec.png)

### **👉Python书籍和视频合集**👈

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

![图片](https://img-blog.csdnimg.cn/img_convert/49ebfc71853ba68a2f9342a472420cac.png)

### 👉**Python副业创收路线**👈

 ![图片](https://img-blog.csdnimg.cn/img_convert/738d65b3c51a50cc0bbfc8e9b8605000.png) 



 **这些资料都是非常不错的**,朋友们如果有需要《Python学习路线&学习资料》,**点击下方安全链接前往获取**

**[CSDN大礼包:《Python入门&进阶学习资源包》免费分享](https://mp.weixin.qq.com/s/b5jwyLC8FsKhg6O8ynyPUA)**

  

  

本文转自 网络,如有侵权,请联系删除。
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值