title: Is there a way to make my sprite jump without a platform?
body: <p>Right now my player sprite can move right or left. I have set up gravity for my player and as expected it drops out of the screen. I have read many documentation on how to make sprites jump. However, they all explain it with platforms used as bases where the sprite can land and move around on. In order to do that they load platform images or create some with pygame. But before writing much code I had already drawn a background for my game which included ground. So I don't wish to create new platforms for my game (for now). Is there a way to make my sprite jump in my program without using platforms? A thought of mine would be to set the &quot;platform&quot; as a certain line on my y-axis. But how do I do that? I'm open to any ideas. Thanks beforehand. My code is separated into 3 files: a main.py, a settings.py and a Sprite1.py. Here's main.py:</p>
  <pre><code>import pygame
  import os
  import sys
  import time
  from pygame import mixer
  from Sprite1 import *
  from settings import *
  
  '''
  Setup
  '''
  pygame.init()
  
  clock = pygame.time.Clock()
  
  pygame.mixer.music.load('.\\sounds\\Fairy.mp3')
  pygame.mixer.music.play(-1, 0.0)
  
  all_sprites = pygame.sprite.Group()
  player = Player(all_sprites)
  
  
  player.rect.x = 50
  player.rect.y = 500
  
  
  
  
  showStartScreen(surface)
  
  '''
  Main loop
  '''
  
  main = True
  
  
  
  while main == True:
  
      background = pygame.image.load(os.path.join('images', 'Bg.png'))
      surface.blit(background, (0,0))
  
  
  
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.quit()
              sys.exit()
              main = False
  
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_LEFT or event.key == ord('a'):
                  player.control(-steps,0)
  
              if event.key == pygame.K_RIGHT or event.key == ord('d'):
                  player.control(steps,0)
  
              if event.key == pygame.K_SPACE:
                  player.jump()
  
          if event.type == pygame.KEYUP:
              if event.key == pygame.K_LEFT or event.key == ord('a'):
                  player.control(steps,0)
  
              if event.key == pygame.K_RIGHT or event.key == ord('d'):
                  player.control(-steps,0)
  
              if event.key == pygame.K_SPACE:
                  player.jump_cut()
  
      dt = clock.tick(60) / 1000
      all_sprites.update(dt)
      player.update(dt)
      all_sprites.draw(surface) #refresh player position
      pygame.display.flip()
  
  </code></pre>
  <p>Here is settings.py:</p>
  <pre><code>import pygame
  
  width = 960
  height = 720
  fps = 40        # frame rate
  pygame.display.set_caption('B.S.G.')
  surface = pygame.display.set_mode((width, height))
  PLAYER_ACC = 0.5
  PLAYER_FRICTION = -0.12
  PLAYER_GRAV = 0.8
  PLAYER_JUMP = 20
  PLAYER_LAYER = 2
  PLATFORM_LAYER = 1
  RED = (255, 0, 0)
  
  steps = 10      # how fast to move
  </code></pre>
  <p>And here's Sprite1.py:</p>
  <pre><code>import pygame
  import sys
  import os
  import time
  from pygame import mixer
  from pygame.locals import *
  from settings import *
  vec = pygame.math.Vector2
  
  def showStartScreen(surface):
      show = True
      while (show == True):
          background = pygame.image.load(os.path.join('images', 'Starting_scr.png'))
          # rect = surface.get_rect()
          surface.blit(background, (0,0))
          pygame.display.flip()
          for event in pygame.event.get():
              if event.type == pygame.MOUSEBUTTONDOWN:
                  show = False
  
  class Player(pygame.sprite.Sprite):
      def __init__(self, all_sprites):
          self._layer = PLAYER_LAYER
          #self.groups = game.all_sprites
          pygame.sprite.Sprite.__init__(self)
          #self.game = game
          self.movex = 0
          self.movey = 0
          self.frame = 0
          self.health = 10
          self.jumping = False
          self.images = []
          self.imagesleft = []
          self.imagesright = []
          self.direction = &quot;right&quot;
          self.alpha = (0,0,0)
          self.ani = 4 # animation cycles
          self.all_sprites = all_sprites
          self.add(self.all_sprites)
          self.bullet_timer = .1
          self.pos = vec(40, height - 100)
          self.vel = vec(0, 0)
          self.acc = vec(0, 0)
  
          for i in range(1,5):
              img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert()
              img.convert_alpha()
              img.set_colorkey(self.alpha)
              self.imagesright.append(img)
              self.image = self.imagesright[0]
              self.rect  = self.image.get_rect()
  
          for i in range(1,5):
              img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert()
              img = pygame.transform.flip(img, True, False)
              img.convert_alpha()
              img.set_colorkey(self.alpha)
              self.imagesleft.append(img)
              self.image = self.imagesleft[0]
              self.rect  = self.image.get_rect()
  
      def control(self,x,y):
          '''
          control player movement
          '''
          self.movex += x
          self.movey -= y
  
      def jump_cut(self):
          if self.jumping:
              if self.vel.y &lt; -3:
                  #self.rect.y += 100
                  self.vel.y = -3
                  #self.jumping = False
                  print(self.vel.y)
  
      def jump(self):
          # jump only if standing on a platform
          #self.rect.y += 20
          #hits = pygame.sprite.spritecollide(self, self.game.platforms, False)
          #self.rect.y -= 100
          if self.rect.y == 500 and not self.jumping:
              #self.game.jump_sound.play()
              self.jumping = True
              self.vel.y = -PLAYER_JUMP
              print(self.rect.y)
  
      def update(self, dt):
          '''
          Update sprite position
          '''
          self.rect.x = self.rect.x + self.movex
          self.rect.y = self.rect.y + self.movey
  
          self.acc = vec(0, PLAYER_GRAV)
  
          # apply friction
          self.acc.x += self.vel.x * PLAYER_FRICTION
          keys = pygame.key.get_pressed()
          if keys[pygame.K_LEFT]:
              self.acc.x = -PLAYER_ACC
          if keys[pygame.K_RIGHT]:
              self.acc.x = PLAYER_ACC
          # equations of motion
          self.vel += self.acc
          if abs(self.vel.x) &lt; 0.1:
              self.vel.x = 0
          self.pos += self.vel + 0.5 * self.acc
          # wrap around the sides of the screen
          if self.pos.x &gt; width + self.rect.width / 2:
              self.pos.x = 0 - self.rect.width / 2
          if self.pos.x &lt; 0 - self.rect.width / 2:
              self.pos.x = width + self.rect.width / 2
  
          self.rect.midbottom = self.pos
  
          # moving left
          if self.movex &lt; 0:
              self.frame += 1
              if self.frame &gt; 3*self.ani:
                  self.frame = 0
              self.image = self.imagesleft[self.frame//self.ani]
              self.direction = &quot;left&quot;
  
          # moving right
          if self.movex &gt; 0:
              self.frame += 1
              if self.frame &gt; 3*self.ani:
                  self.frame = 0
              self.image = self.imagesright[self.frame//self.ani]
              self.direction = &quot;right&quot;
  
          keys = pygame.key.get_pressed()
          if keys[pygame.K_SPACE]:
              self.bullet_timer -= dt  # Subtract the time since the last tick.
  
          if self.bullet_timer &lt;= 0:
              self.bullet_timer = 100  # Bullet ready.
              if keys:  # Left mouse button.
                  # Create a new bullet instance and add it to the groups.
                  if self.direction == &quot;right&quot;:
                      Bullet([self.rect.x + self.image.get_width(), self.rect.y + self.image.get_height()/2], self.direction, self.all_sprites)
                  else:
                      Bullet([self.rect.x, self.rect.y + self.image.get_height()/2], self.direction, self.all_sprites)
                  self.bullet_timer = .1  # Reset the timer.
  
  
  
  class Bullet(pygame.sprite.Sprite):
  
      IMAGE = None
      FLIPPED_IMAGE = None
  
      def __init__(self, pos, direction, *sprite_groups):
          super().__init__(*sprite_groups)
  
          if not Bullet.IMAGE:
              Bullet.IMAGE = pygame.image.load(os.path.join('images','fireball.png'))
              Bullet.FLIPPED_IMAGE = pygame.transform.flip(Bullet.IMAGE, True, False)
  
          if direction == &quot;right&quot;:
              self.vel = pygame.math.Vector2(750, 0)
              self.image = Bullet.IMAGE
          else:
              self.vel = pygame.math.Vector2(-750, 0)
              self.image = Bullet.FLIPPED_IMAGE
  
          self.pos = pygame.math.Vector2(pos)
          self.rect  = self.image.get_rect(center=pos)
  
      def update(self, dt):
          # Add the velocity to the position vector to move the sprite
          self.pos += self.vel * dt
          self.rect.center = self.pos  # Update the rect pos.
          if not pygame.display.get_surface().get_rect().colliderect(self.rect):
              self.kill()
  </code></pre>