Pygame: Collision of two images
<p>I'm working on my school project for which im designing a 2D game.</p>

<p>I have 3 images, one is the player and the other 2 are instances (coffee and computer). What i want to do is, when the player image collides with one of the 2 instances i want the program to print something.</p>

<p>I'm unsure if image collision is possible. But i know rect collision is possible. However, after several failed attempts, i can't manage to make my images rects. Somebody please help me. Here is my source code:</p>

<pre><code>import pygame
import os

black=(0,0,0)
white=(255,255,255)
blue=(0,0,255)


class Player(object):  
    def __init__(self):
        self.image = pygame.image.load("player1.png")
        self.image2 = pygame.transform.flip(self.image, True, False)
        self.coffee=pygame.image.load("coffee.png")
        self.computer=pygame.image.load("computer.png")
        self.flipped = False
        self.x = 0
        self.y = 0


    def handle_keys(self):
        """ Movement keys """
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN]: 
            self.y += dist 
        elif key[pygame.K_UP]: 
            self.y -= dist 
        if key[pygame.K_RIGHT]: 
            self.x += dist
            self.flipped = False
        elif key[pygame.K_LEFT]:
            self.x -= dist
            self.flipped = True

    def draw(self, surface):
        if self.flipped:
            image = self.image2
        else:
            im = self.image            
        for x in range(0, 810, 10):
            pygame.draw.rect(screen, black, [x, 0, 10, 10])
            pygame.draw.rect(screen, black, [x, 610, 10, 10])

        for x in range(0, 610, 10):
            pygame.draw.rect(screen, black, [0, x, 10, 10])
            pygame.draw.rect(screen, black, [810, x, 10, 10])

        surface.blit(self.coffee, (725,500))
        surface.blit(self.computer,(15,500))
        surface.blit(im, (self.x, self.y))



pygame.init()



screen = pygame.display.set_mode((800, 600))#creates the screen

player = Player()
clock = pygame.time.Clock()

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()      # quit the screen
            running = False

    player.handle_keys()       # movement keys
    screen.fill((255,255,255)) # fill the screen with white




    player.draw(screen)        # draw the player to the screen
    pygame.display.update()    # update the screen

    clock.tick(60)             # Limits Frames Per Second to 60 or less
</code></pre>

<p>Use <a href="http://pygame.org/docs/ref/rect.html" rel="nofollow">pygame.Rect()</a> to keep image size and position. </p>

<p>Image (or rather <code>pygame.Surface()</code>) has function <code>get_rect()</code> which returns <code>pygame.Rect()</code> with image size (and position).</p>

<pre><code>self.rect = self.image.get_rect()
</code></pre>

<p>Now you can set start position ie. <code>(0, 0)</code></p>

<pre><code>self.rect.x = 0
self.rect.y = 0

# or 

self.rect.topleft = (0, 0)

# or

self.rect = self.image.get_rect(x=0, y=0)
</code></pre>

<p>(<code>Rect</code> use left top corner as (x,y)). </p>

<p>Use it to change position</p>

<pre><code>self.rect.x += dist
</code></pre>

<p>and to draw image</p>

<pre><code>surface.blit(self.image, self.rect)
</code></pre>

<p>and then you can test collision</p>

<pre><code>if self.rect.colliderect(self.rect_coffe):
</code></pre>

<hr>

<p>BTW: and now <code>class Player</code> looks almost like <a href="http://pygame.org/docs/ref/sprite.html" rel="nofollow">pygame.sprite.Sprite</a> :)</p>

python python-2.7 pygame