<legend id='kYTlL'><style id='kYTlL'><dir id='kYTlL'><q id='kYTlL'></q></dir></style></legend>
  1. <small id='kYTlL'></small><noframes id='kYTlL'>

    <i id='kYTlL'><tr id='kYTlL'><dt id='kYTlL'><q id='kYTlL'><span id='kYTlL'><b id='kYTlL'><form id='kYTlL'><ins id='kYTlL'></ins><ul id='kYTlL'></ul><sub id='kYTlL'></sub></form><legend id='kYTlL'></legend><bdo id='kYTlL'><pre id='kYTlL'><center id='kYTlL'></center></pre></bdo></b><th id='kYTlL'></th></span></q></dt></tr></i><div id='kYTlL'><tfoot id='kYTlL'></tfoot><dl id='kYTlL'><fieldset id='kYTlL'></fieldset></dl></div>
      • <bdo id='kYTlL'></bdo><ul id='kYTlL'></ul>

    1. <tfoot id='kYTlL'></tfoot>

      Python只运行一次while循环

      时间:2023-10-08
      • <bdo id='pgQPu'></bdo><ul id='pgQPu'></ul>

          <small id='pgQPu'></small><noframes id='pgQPu'>

            <tbody id='pgQPu'></tbody>

            • <legend id='pgQPu'><style id='pgQPu'><dir id='pgQPu'><q id='pgQPu'></q></dir></style></legend>
                <tfoot id='pgQPu'></tfoot>
                <i id='pgQPu'><tr id='pgQPu'><dt id='pgQPu'><q id='pgQPu'><span id='pgQPu'><b id='pgQPu'><form id='pgQPu'><ins id='pgQPu'></ins><ul id='pgQPu'></ul><sub id='pgQPu'></sub></form><legend id='pgQPu'></legend><bdo id='pgQPu'><pre id='pgQPu'><center id='pgQPu'></center></pre></bdo></b><th id='pgQPu'></th></span></q></dt></tr></i><div id='pgQPu'><tfoot id='pgQPu'></tfoot><dl id='pgQPu'><fieldset id='pgQPu'></fieldset></dl></div>
                本文介绍了Python只运行一次while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                import pygame
                
                r_colour = (200, 100,100)
                bg_colour = (0,175,200)
                (width, height) = (600, 600)
                
                screen = pygame.display.set_mode((width, height))
                screen.fill(bg_colour)
                pygame.draw.rect(screen, r_colour, (30, 30, 100, 100), 0)
                
                pygame.display.flip()
                
                running = True
                while True:
                    for event in pygame.event.get():
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_s:
                                screen.fill(bg_colour)
                                pygame.draw.rect(screen, r_colour, (20, 30, 100, 100), 0)
                                pygame.display.update()
                
                
                if running == True:
                      for event in pygame.event.get():
                          if event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_s:
                                    screen.fill(bg_colour)
                                    pygame.draw.rect(screen, r_colour, (20, 30, 100, 100), 0)
                                    pygame.display.update()
                
                
                
                while running:
                  for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                      break
                      running = False
                
                
                
                pygame.quit()
                

                我试图让红色方块在按下s"键时移动,不知道为什么它只移动一次然后停止.对编程非常陌生,所以很抱歉,如果它很长或难以阅读.

                I am trying to get the red square to move when pressing the 's' key, not sure as to why it only moves once and then stops. Very new to programming, so I am sorry, if it's long or hard to read.

                推荐答案

                一个典型的应用程序有 1 个单一的应用程序循环.应用程序循环:

                A typical application has 1 single application loop. The application loop does:

                • 处理事件并根据事件更改状态
                • 清除显示
                • 绘制场景
                • 更新显示

                KEYDOWY 事件在按下某个键时发生一次,但在按住某个键时不会连续发生.
                对于连续运动,您可以通过 <代码>pygame.key.get_pressed():

                The KEYDOWY event occurs once when a key is pressed, but it does not occur continuously when a key is hold down.
                For a continuously movement you can get the state of the keys by pygame.key.get_pressed():

                keys = pygame.key.get_pressed()
                

                例如如果 s 的状态被按下,可以通过 keys[pygame.K_s] 来评估.

                e.g. If the state of s is pressed can be evaluated by keys[pygame.K_s].

                为矩形的位置添加坐标(x, y).当按键被按下时,在主应用程序循环中不断地操作位置.

                Add coordinates (x, y) for the position of the rectangle. Continuously manipulate the position in the main application loop, when a key is pressed.

                例如
                如果按下 d,则增加 x,如果按下 a,则减少 x.
                如果按下 s,则增加 y,如果按下 w,则减少 y:

                e.g.
                Increment x if d is pressed and decrement x if a is pressed.
                Increment y if s is pressed and decrement y if w is pressed:

                import pygame
                
                r_colour = (200, 100,100)
                bg_colour = (0,175,200)
                (width, height) = (600, 600)
                x, y = 20, 30
                
                screen = pygame.display.set_mode((width, height))
                
                running = True
                while running:
                
                    # handle the events
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            running = False
                
                    # change coordinates
                    keys = pygame.key.get_pressed()
                    if keys[pygame.K_d]:
                        x += 1
                    if keys[pygame.K_a]:
                        x -= 1
                    if keys[pygame.K_s]:
                        y += 1
                    if keys[pygame.K_w]:
                        y -= 1
                
                    # clear the display
                    screen.fill(bg_colour)
                    # draw the scene
                    pygame.draw.rect(screen, r_colour, (x, y, 100, 100), 0)
                    # update the display
                    pygame.display.update()
                
                pygame.quit()
                

                这篇关于Python只运行一次while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Spark 安装 - 错误:无法找到或加载主类 org.apache. 下一篇:如何使用 python 脚本使 Windows 10 计算机进入睡眠状

                相关文章

                最新文章

                <legend id='si5Mn'><style id='si5Mn'><dir id='si5Mn'><q id='si5Mn'></q></dir></style></legend>

                <small id='si5Mn'></small><noframes id='si5Mn'>

                • <bdo id='si5Mn'></bdo><ul id='si5Mn'></ul>
                1. <tfoot id='si5Mn'></tfoot>
                  <i id='si5Mn'><tr id='si5Mn'><dt id='si5Mn'><q id='si5Mn'><span id='si5Mn'><b id='si5Mn'><form id='si5Mn'><ins id='si5Mn'></ins><ul id='si5Mn'></ul><sub id='si5Mn'></sub></form><legend id='si5Mn'></legend><bdo id='si5Mn'><pre id='si5Mn'><center id='si5Mn'></center></pre></bdo></b><th id='si5Mn'></th></span></q></dt></tr></i><div id='si5Mn'><tfoot id='si5Mn'></tfoot><dl id='si5Mn'><fieldset id='si5Mn'></fieldset></dl></div>