Jumping simulation stop falling when returned to ground
<p>I am trying to simulate a jump in a Rect.</p>

<p>I set the KEYDOWN  and KEYUP events and K_SPACE button to simulate the jump. </p>

<p>My difficulty is stopping the returning fall when the rect reaches the ground (using cordX).</p>

<p>I realize that will not work and comment it out in the GAME_BEGIN statement.</p>

<pre><code>import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
speedX = 0
speedY = 0
cordX = 10
cordY = 100
jumping=False;
run=False;


def draw():
    screen.fill((0, 0, 0))
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
    pygame.display.flip();



while JogoAtivo:
    for evento in pygame.event.get():
        print(evento)
    #verifica se o evento que veio eh para fechar a janela
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_a:
                   print('GAME BEGIN')
                   GAME_BEGIN = True
                   draw();        
        if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_LEFT:
                   speedX=-0.006
                   run= True;
        if evento.type == pygame.KEYDOWN:
             if evento.key == pygame.K_RIGHT:
                   speedX=0.006
                   run= True;
        if evento.type == pygame.KEYDOWN:
             if evento.key == pygame.K_SPACE:
                   speedY=-0.090
                   jumping= True;
        if evento.type == pygame.KEYUP:
             if evento.key == pygame.K_SPACE:
                   speedY=+0.090
                   jumping= True;


    if GAME_BEGIN:
        if not jumping:
            gravity = cordX;
        """if gravity == cordY:
            speedY=0;"""
        cordX+=speedX
        cordY+=speedY
        draw() 
</code></pre>

<blockquote>
  <blockquote>
    <blockquote>
      <p>UPDATED BELOW</p>
    </blockquote>
  </blockquote>
</blockquote>

<pre><code>import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
speedX = 0
speedY = 0
cordX = 10
cordY = 100
groundX=0;
groundY=150;
jumping=False;
run=False;


def draw():
    screen.fill((0, 0, 0))
    ground = pygame.draw.rect(screen, (0, 255, 0), (groundX, groundY,400, 10))
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
    pygame.display.flip();



while JogoAtivo:
    for evento in pygame.event.get():
        print(evento)
    #verifica se o evento que veio eh para fechar a janela
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_a:
                   print('GAME BEGIN')
                   GAME_BEGIN = True
                   draw();        
            if evento.key == pygame.K_LEFT:
                   speedX=-0.006
                   run= True;
            if evento.key == pygame.K_RIGHT:
                   speedX=0.006
                   run= True;
            if evento.key == pygame.K_SPACE:
                   speedY=-0.090
                   jumping= True;
        if evento.type == pygame.KEYUP:
             if evento.key == pygame.K_SPACE:
                   speedY=+0.090
                   jumping= True;


    if GAME_BEGIN:
        cordX+=speedX
        cordY+=speedY
        draw();
        if cordY +50&gt;= groundY:
            speedY=0
            jumping=False;
</code></pre>

<p>Tks, i have done what you said, here is my code</p>

<pre><code>    import pygame

pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("shield hacking")
JogoAtivo = True
GAME_BEGIN = False
# Speed in pixels per frame
speedX = 0
speedY = 0
cordX = 10
cordY = 100
groundX=0;
groundY=150;
jumping=False;
run=False;


def draw():
    screen.fill((0, 0, 0))
    ground = pygame.draw.rect(screen, (0, 255, 0), (groundX, groundY,400, 10))
    quadrado = pygame.draw.rect(screen, (255, 0, 0), (cordX, cordY ,50, 52))
    pygame.display.flip();



while JogoAtivo:
    for evento in pygame.event.get():
        print(evento)
    #verifica se o evento que veio eh para fechar a janela
        if evento.type == pygame.QUIT:
               JogoAtivo = False
               pygame.quit();
        if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_a:
                   print('GAME BEGIN')
                   GAME_BEGIN = True
                   draw();        
            if evento.key == pygame.K_LEFT:
                   speedX=-0.006
                   run= True;
            if evento.key == pygame.K_RIGHT:
                   speedX=0.006
                   run= True;
            if evento.key == pygame.K_SPACE:
                   speedY=-0.090
                   jumping= True;
        if evento.type == pygame.KEYUP:
             if evento.key == pygame.K_SPACE:
                   speedY=+0.090
                   jumping= True;


    if GAME_BEGIN:
        cordX+=speedX
        cordY+=speedY
        draw();
        if cordY +50&gt;= groundY:
            speedY=0
            jumping=False;
</code></pre>

<blockquote>
  <blockquote>
    <blockquote>
      <p>Really appreciate the help.</p>
    </blockquote>
  </blockquote>
</blockquote>

python pygame