Can't get my sprites to collide?
Question: I feel like I've tried everything, and I'm getting pretty freaking tired of
this now.... I'm a python and pygame noob trying to make my own game from
scratch without looking at tutorials, but i don't know how to solve this, i
must have missed something.

Here is my statement that checks for collision:

    
    
    for c in cakes:
            if pygame.sprite.collide_rect(jake, c) and c.eaten == False: 
                c.is_eaten()
                score += 1
                print score
    
    
    
            c.draw()
            c.update_pos()
    

So the game is basically a "catch the falling thing"-game. I have no idea what
i have missed...

Here is the Cake-class, it looks pretty much the same as the player-class:

    
    
    class Cake(pygame.sprite.Sprite):
    
    
    def load_image(self, image_name):
        try:
            image = pygame.image.load(image_name)
        except pygame.error as message:
            print ("Cannot load image: " + image_name)
            raise SystemExit(message)
        return image.convert_alpha()
    
    
    def __init__(self, screen, x, y, speed):
        self.image = self.load_image("cake.png")
        self.screen = screen
        self.x = x
        self.y = y
        self.rect = self.image.get_rect()
        self.w, self.h = self.image.get_size()
        self.rect.move(self.x, self.y)
        self.rect.topleft = (self.x, self.y)
        self.rect.bottomright = (self.x + self.w, self.y + self.h)
        self.eaten = False
        self.speed = speed
    
    
    def draw(self):
        if self.eaten == False:
            self.screen.blit(self.image, (self.x, self.y))
    
    
    def is_eaten(self):
        self.eaten = True
    
    
    def update_pos(self):
        if self.eaten == False:
            self.y += self.speed
    

I'm not looking for a clear or direct answer, just a nudge in the right
direction, thanks in advance.


Answer: Since you mention you're new to python, I'll go out on a limb and assume
you're new to programming in general.

When a problem appears and you've invested some effort on it and are still not
quite sure what's wrong, it's probably time to delve into **debugging**.

There's multiple debugging techniques for different languages, but the general
theory is to **verify that the program is working as you expect it to be** .
Very often a program will be failing for reasons completely unrelated to the
initially visible problem.

Here are some basic steps to help you debug a program:

  * **Reproduce the problem reliably** \- and, preferably, automatically. 

  * **Approach the problem in a structured way**. Make educated assumptions as to why the program is failing, then test them with...

  * **Prints**. Print the content of your variables, the values of function arguments, the results of function calls. Check carefully the output, and see if it matches your expectations. As soon as something doesn't, _stop_ and figure out why it doesn't. In python, you can use the excellent [`logging` module](https://docs.python.org/2/howto/logging.html) to separate your debugging prints from your normal output.

  * **Run your program step by step in a interactive debugger**. This will help you inspect your data structures more deeply than you can comfortably do with printing. For python, you can use [pdb](https://docs.python.org/2/library/pdb.html)

* * *

Let's see how to put this theory into practice in your particular problem.

While you don't mention it, your question implies that collisions are not
happening. I'll further assume that you can reproduce the problem reliably -
since you mention it never works.

Something you might want to do next is to **setup the game such that it always
starts with the same initial conditions**. If you have any calls to random,
setup the random number generator to use a fixed seed, and setup a seed that
can reproduce your problem quickly - and preferably, without user input.

You've already isolated the code where the problem is happening.

Next, you should **print the contents of the involved variables: the positions
of the sprites, the value of`eaten`**. Then, run your program again and watch
the prints to make sure everything is as you'd expect. If it is, figure out
_when_ (as in the number of pygame main loops) it starts to fail. You can then
**put a conditional in your code to launch the debugger** , `pdb`:

    
    
    if number_of_pygame_loop_iterations==PROBLEMATIC_ITERATION:
        import pdb; pdb.set_trace()
    

From there, you can then call `pygame.sprite.collide_rect` with the sprites in
their current position, and check what's amiss.

