title: How do I make the collisions work in this side scroller?
body: <pre><code>import pygame #importing the pygame library
  
  pygame.init()
  
  
  all_sprites = pygame.sprite.Group()   
  obstacle_group = pygame.sprite.Group()
  player_group = pygame.sprite.Group()
  </code></pre>
  <p>defines the sprite groups we'll be calling on later</p>
  <pre><code>####Variables
  width, height = 626, 375
  fontObj = pygame.font.Font('Alsina Ultrajada.TTF', 16)
  pygame.display.set_caption(&quot;side scroller&quot;)
  screen = pygame.display.set_mode((width, height))
  bg = pygame.image.load(&quot;achtergrondPixel.png&quot;)
  gameOver = pygame.image.load(&quot;gameOver.png&quot;)
  
  R_char = pygame.transform.scale(pygame.image.load(&quot;character.png&quot;), (100, 100))
  L_char = pygame.transform.flip((R_char), True, False)
  char = R_char
  jade = (55, 255, 20)
  red = (255, 0, 0)
  hitbox = (150, 200, 100, 100)
  </code></pre>
  <p>here we are making our player class, first we are initializing our player</p>
  <pre><code>class Player(pygame.sprite.Sprite): #making a class for our character so we can easily call the variables
  
    def __init__(self, x, y, width, height, pos):
      global player_group
      super().__init__(player_group, all_sprites)
      self.x = x
      self.y = y
      self.width = width
      self.height = height
      self.charFlip = False
      self.isJump = False
      self.jumpCount = 10
      self.isFalling = False
      self.fallCount = int(1)
      self.pos = pos
      self.rect = pygame.Rect((self.x - self.pos + 21),(self.y + 20), (self.width - 33), (self.height- 33))
      self.add(player_group)
  
  </code></pre>
  <p>here we are making the function that draws our character, we also have a jump system and an falling system</p>
  <pre><code>  def draw(self, screen):
      screen.blit(char, (self.x, self.y))
      if self.isFalling:
        if self.y &lt;= 200  and self.x &lt; 488 and self.x &gt; 573:  #hier moet var worden aangemaakt voor als hij op platform staat
            self.y -= (self.fallCount**2) * 0.1 * -1
            self.fallCount += 1
        else:
          self.isFalling = False
          self.fallCount = 1
      if self.isJump:
        if self.jumpCount &gt;= 0:
          neg = 1
          if self.jumpCount == 0:
            self.isFalling = True
          self.y -= (self.jumpCount**2) * .25 * neg
          self.jumpCount -= 1
        else:
          self.isJump = False
          self.jumpCount = 10
      self.hitbox = pygame.Rect((self.x - self.pos + 21),(self.y + 20), (self.width - 33), (self.height- 33))
      if pygame.sprite.spritecollideany(self, obstacle_group):
        print(&quot;collide&quot;)
      
  </code></pre>
  <p>Here we are making the green rectangle that is our obstacle in the game, first initializing it and that drawing it to the screen in the color <code>jade</code></p>
  <pre><code>class Obstacle(pygame.sprite.Sprite):
  
    def __init__(self, x, y, width, height, pos):
      global obstacle_group
      super().__init__(obstacle_group, all_sprites)
      self.pos = pos
      self.x = x
      self.y = y
      self.width = width
      self.height = height
      self.rect = pygame.Rect((self.x - self.pos),self.y, self.width, self.height)
      #self.add(obstacle_group)
  
    def draw(self, screen, pos):
      pygame.draw.rect(screen, jade, pygame.Rect((self.x - self.pos), self.y, self.width, self.height))
      self.hitbox = pygame.Rect((self.x - self.pos),self.y, self.width, self.height)
      pygame.draw.rect(screen, red, self.hitbox, 1)
    
  </code></pre>
  <p><code>pos</code> is the variable we use to scroll the background</p>
  <pre><code>pos = 0
  </code></pre>
  <p>here we are making the player character and the only obstacle in the side scroller</p>
  <pre><code>obstacle2 = Obstacle(300, 200, 100, 20, pos)
  nkdMonkey = Player(150, 200, 100, 100, pos)
  
  FPS = 60
  run = True
  clock = pygame.time.Clock()
  
  </code></pre>
  <p>this is our main drawing function that we call on in our main <code>while</code> loop</p>
  <pre><code>def draw_window():
    screen.blit(bg, ((-1 * pos), 0))
    textSurfaceObj = fontObj.render((str(pos)), False, (240,240,240, 255))
    
    screen.blit(textSurfaceObj, (40,40)) 
    obstacle2.draw(screen, pos)
    nkdMonkey.draw(screen)
    #pygame.draw.rect(screen, red, nkdMonkey.hitbox, 1)
    #for if you want to see the hitbox of the player which is used for collisions
    pygame.display.update()
  </code></pre>
  <p>this is our while loop</p>
  <pre><code>while run:
  
     
      clock.tick(FPS)
  
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              run = False
      #all_sprites = pygame.sprite.Group()   
      #obstacle_group = pygame.sprite.Group()
      #player_group = pygame.sprite.Group()
      keys_pressed = pygame.key.get_pressed()
      #Checks if the front and and back button is pressed and if so, changes pos.
      if keys_pressed[pygame.K_d] and pos &lt; 1874:
          pos += 2
          char = R_char
      if keys_pressed[pygame.K_d] and keys_pressed[
              pygame.K_LSHIFT] and pos &lt; 2000:
          pos += 5
          char = R_char
      if keys_pressed[pygame.K_a] and pos &gt; 0:
          pos -= 2
          char = L_char
      if keys_pressed[pygame.K_a] and keys_pressed[pygame.K_LSHIFT] and pos &gt; 0:
          pos -= 5
          char = L_char
      if keys_pressed[pygame.K_w] and nkdMonkey.isJump == False:
          nkdMonkey.isJump = True
      if nkdMonkey.y &gt; 200 and nkdMonkey.x &gt; 488 and nkdMonkey.x &lt; 573:
        nkdMonkey.y -= 1  
      #if nkdMonkey.x &gt; 488 and nkdMonkey.x &lt; 573:
        #nkdMonkey.isFalling = True
      
  
      if pos &gt; 1980:
        
        run = False 
  </code></pre>
  <p>this is the point we can't figure out, we want it to print <code>collide</code> when the two sprites are colliding</p>
  <pre><code>    if pygame.sprite.spritecollideany(nkdMonkey, obstacle_group):
        print(&quot;collide&quot;)
      all_sprites.update()
      draw_window()
  </code></pre>
  <p>Here we made a simple end screen</p>
  <pre><code>screen.fill(jade)
  screen.blit(pygame.image.load(&quot;character.png&quot;), (0, 70))
  text = fontObj.render('You win!!', True, (0,0,0, 255))
  textRect = text.get_rect()
  score = fontObj.render(str(pos), True, red)
  screen.blit(score, (40,40)) 
  textRect.center = (width // 2, (height // 2 + 20))
  screen.blit(text, textRect)
  pygame.display.flip()
  </code></pre>