• <small id='KxJS3'></small><noframes id='KxJS3'>

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

      1. <legend id='KxJS3'><style id='KxJS3'><dir id='KxJS3'><q id='KxJS3'></q></dir></style></legend>

        Python只运行一次while循环

        Python only running while loop once(Python只运行一次while循环)
          <tbody id='Cpj5b'></tbody>
        • <bdo id='Cpj5b'></bdo><ul id='Cpj5b'></ul>

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

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

                  本文介绍了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循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)

                        <bdo id='sMRrM'></bdo><ul id='sMRrM'></ul>

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

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