pythonpython-2.7pygame

Pygame: Collision of two images


I'm working on my school project for which im designing a 2D game.

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.

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:

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

Solution

  • Use pygame.Rect() to keep image size and position.

    Image (or rather pygame.Surface()) has function get_rect() which returns pygame.Rect() with image size (and position).

    self.rect = self.image.get_rect()
    

    Now you can set start position ie. (0, 0)

    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)
    

    (Rect use left top corner as (x,y)).

    Use it to change position

    self.rect.x += dist
    

    and to draw image

    surface.blit(self.image, self.rect)
    

    and then you can test collision

    if self.rect.colliderect(self.rect_coffe):
    

    BTW: and now class Player looks almost like pygame.sprite.Sprite :)