title: Is there a way to keep an object in bounds in pygame, and make it move when you hold the directional keys down?
body: <p>I am using pygame 1.9.6 and python 3.7.7. I am looking for a way to have something drawn by pygame(or maybe even an image) that stays inside the window border(and stops when I release the key. I started with the “if keys[]:” method:</p>
  <pre><code>if keys[pygame.K_LEFT] and x&gt;0:
      x-=speed
  </code></pre>
  <p>that made it stay in bounds, but it only moved a few pixels and then did not repeat.
  Next I tried the “event.key” method:</p>
  <pre><code>if event.type==pygame.KEYDOWN:
      if event.key==pygame.K_LEFT:
          xchange=-speed
          ychange=-0
  </code></pre>
  <p>but that just makes the object move forever. I tried putting “and x&gt;0” on the same line right after the direction, an “if x&gt;0:” before, an “if x&lt;0:” after, but the event never updates to see that the coordinates are past the edge, even with update commands. It just keeps going and going. I also don’t know how to make the object stop moving when I release the key, since event.key’s have the event always on.</p>
  <p>Thank you for all the help you can offer.</p>