title: implementing collision correctly for rectangles
body: <p>I'm trying to experiment with pygame and I am creating a simple prototype game.</p>
  <p>I need some help with collision for a player rect and a &quot;food&quot; rect.
  I am trying to make a simple game that has a player and a food object. the food object's location is randomized when the game starts</p>
  <pre class="lang-py prettyprint-override"><code>class Food(Player):
      def __init__(self):
          super().__init__()
          self.rand_x = random.randint(0, width)
          self.rand_y = random.randint(0, height)
          self.food = pg.Rect(self.rand_x,
                              self.rand_y,
                              15, 15)
      ...
  </code></pre>
  <p>this food class contains a function that handles the collision of the player and food, if the player collides with the food, the location of the food changes again</p>
  <pre class="lang-py prettyprint-override"><code>    def playerCollision(self):
          if self.sprite.colliderect(self.food) or self.food.colliderect(self.sprite):
              self.food.x = self.rand_x
              self.food.y = self.rand_y
  </code></pre>
  <p>Here's my player class</p>
  <pre class="lang-py prettyprint-override"><code>class Player:
      def __init__(self):
          self.sprite = pg.Rect(x, y, 50, 50)
  
      def handle_keys(self):
          keys = pg.key.get_pressed()
          if keys[pg.K_w]:
              self.sprite.move_ip(0, -velocity)
          elif keys[pg.K_a]:
              self.sprite.move_ip(-velocity, 0)
          elif keys[pg.K_s]:
              self.sprite.move_ip(0, velocity)
          elif keys[pg.K_d]:
              self.sprite.move_ip(velocity, 0)
  
      def draw(self, surface):
          pg.draw.rect(surface, spriteColors[0], self.sprite)
  </code></pre>
  <p>and heres the full food class</p>
  <pre class="lang-py prettyprint-override"><code>class Food(Player):
      def __init__(self):
          super().__init__()
          self.rand_x = random.randint(0, width)
          self.rand_y = random.randint(0, height)
          self.food = pg.Rect(self.rand_x,
                              self.rand_y,
                              15, 15)
  
      def randomize_pos(self):  # debugger function to test randomized food locations
          self.food.x = random.randint(0, width)
          self.food.y = random.randint(0, height)
          self.drawFood(screen)
  
      def playerCollision(self):
          if self.sprite.colliderect(self.food) or self.food.colliderect(self.sprite):
              self.food.x = self.rand_x
              self.food.y = self.rand_y
  
      def drawFood(self, surface):
          pg.draw.rect(surface, spriteColors[1], self.food)
  </code></pre>
  <p>I've looked everywhere for solutions and implementing solutions into my code but none work.</p>
  <p>Heres the full code if you all need it</p>
  <pre class="lang-py prettyprint-override"><code>import pygame as pg
  from pygame.locals import *
  import sys, random
  
  pg.init()
  
  # Setup and screen properties
  pg.display.set_caption(&quot;prototype \u2014 game&quot;)
  width, height = (800, 600)
  screen = pg.display.set_mode([width, height])
  spriteColors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]  # Red, Green, Blue
  
  x = 25
  y = 25
  velocity = 5
  
  class Player:
      def __init__(self):
          self.sprite = pg.Rect(x, y, 50, 50)
  
      def handle_keys(self):
          keys = pg.key.get_pressed()
          if keys[pg.K_w]:
              self.sprite.move_ip(0, -velocity)
          elif keys[pg.K_a]:
              self.sprite.move_ip(-velocity, 0)
          elif keys[pg.K_s]:
              self.sprite.move_ip(0, velocity)
          elif keys[pg.K_d]:
              self.sprite.move_ip(velocity, 0)
  
      def draw(self, surface):
          pg.draw.rect(surface, spriteColors[0], self.sprite)
  
  class Food(Player):
      def __init__(self):
          super().__init__()
          self.rand_x = random.randint(0, width)
          self.rand_y = random.randint(0, height)
          self.food = pg.Rect(self.rand_x,
                              self.rand_y,
                              15, 15)
  
      def randomize_pos(self):  # debugger function to test randomized food locations
          self.food.x = random.randint(0, width)
          self.food.y = random.randint(0, height)
          self.drawFood(screen)
  
      def playerCollision(self):
          if self.sprite.colliderect(self.food) or self.food.colliderect(self.sprite):
              self.food.x = self.rand_x
              self.food.y = self.rand_y
  
      def drawFood(self, surface):
          pg.draw.rect(surface, spriteColors[1], self.food)
  
  
  # Initialize Player and Food Object
  player = Player()
  food = Food()
  clock = pg.time.Clock()
  
  # Game Loop
  while True:
      for event in pg.event.get():
          if event.type == QUIT:
              pg.quit()
              sys.exit()
  
          elif event.type == pg.KEYDOWN:
              if event.key == pg.K_ESCAPE:
                  pg.quit()
                  sys.exit()
  
      screen.fill((0, 0, 0))
  
      player.handle_keys()
      player.draw(screen)
      food.drawFood(screen)
      food.playerCollision()
      pg.display.update()
  
      clock.tick(60)
  </code></pre>