0

I am trying to get collision between my Player() and my Coin(). I have looking through SO questions and internet forums and I can't seem to find an answer that doesn't use a Sprite.

I drew two rectangles (Player and Coin) and I just want to know how to see if they collide. Here is my code:

import pygame, sys, random

pygame.init()

display_width = 640
display_height = 480

display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Tutorial")

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
yellow = (255,255,0)

clock = pygame.time.Clock()

running = True

class Player:

    def __init__(self,x,y,hspd,vspd,color,screen):
        self.x = x
        self.y = y
        self.hspd = hspd
        self.vspd = vspd
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(self.screen,self.color,(self.x,self.y,32,32))

    def move(self):
        self.x += self.hspd
        self.y += self.vspd

def collisionDetect(obj1,obj2):
    pygame.sprite.collide_rect(obj1,obj2)

enemies = []

class Enemy:

    def __init__(self,x,y,hspd,vspd,color,screen):
        self.x = x
        self.y = y
        self.hspd = hspd
        self.vspd = vspd
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(display,red,(self.x,self.y,32,32))

    def move(self):
        self.x += self.hspd
        self.y += self.vspd

    def checkBounce(self):
        if self.x > 640-32 or self.x < 0:
            self.hspd = self.hspd * -1
        if self.y > 480-32 or self.y < 0:
            self.vspd = self.vspd * -1


class Coin:

    def __init__(self,x,y,color,screen):
        self.x = random.randint(0,640-32)
        self.y = random.randint(0,480-32)
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(self.screen,yellow,(self.x,self.y,32,32))

    def move(self):
        if collisionDetect(self,player):
            self.x = random.randint(0,640-32)
            self.y = random.randint(0,480-32)


coin = Coin(255,255,yellow,display)   

player = Player(0,0,0,0,black,display)



def current_speed():
    currently_pressed = pygame.key.get_pressed()
    hdir = currently_pressed[pygame.K_RIGHT] - currently_pressed[pygame.K_LEFT]
    vdir = currently_pressed[pygame.K_DOWN] - currently_pressed[pygame.K_UP]
    return hdir * 4, vdir * 4

def updateEnemies():
    for enemy in enemies:
        enemy.move()
        enemy.draw()
        enemy.checkBounce()

CREATEENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(CREATEENEMY, 1000)


while running:

    clock.tick(60)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == CREATEENEMY:
            enemy = Enemy(0,0,random.randint(1,4),random.randint(1,4),red,display)
            enemies.append(enemy)

        player.hspd, player.vspd = current_speed()

    display.fill(white)

    player.move()

    player.draw()
    coin.draw()
    updateEnemies()

    pygame.display.flip()

    print len(enemies)
pygame.quit()
sys.exit()

Thanks in advance!

L4undry
  • 281
  • 1
  • 4
  • 16
  • 2
    Possible duplicate of [Determine if two rectangles overlap each other?](http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other) – Suever Feb 20 '16 at 17:21
  • @Suever That is in C++, This is in python. – L4undry Feb 20 '16 at 17:24
  • 2
    It's a generic question that shouldn't have an answer for literally every library and programming language. The accepted answer can be adapted to any rectangle implemented in any language. – Suever Feb 20 '16 at 17:25
  • The basic logic is to check if any corner of one rectangle is within the other. I agree the other answer should be easy to translate – OneCricketeer Feb 20 '16 at 17:28

1 Answers1

0

I looked through multiple websites as well, I couldn't find anything related to Python when it comes to collision detection, I found collision detection if statement in Javascript. after translating it into Python here it is :

if rect_1 x < rect_2 x + rect_1 width and rect_1 x + rect_2 width > rect_2 x and rect_1 y < rect_2 y + rect_1 height and rect_1 height + rect_1 y > rect_2 y:

the reason that this works is because it senses if the edges of the rectangles are touching.

pi guy
  • 106
  • 1
  • 3
  • 13
  • 1
    Welcome to SO! Thanks for answering this question... Could I suggest a couple of improvements, though? First you should move the stuff about you into your profile as it is not relevant to the answer. Second, can you please make your code syntactically correct (e.g. as a sample function) and explain why it works? Legodog5678's answer is a good example of what to aim for. – Peter Brittain Aug 04 '16 at 23:16
  • For collision there is [Sprite collision functions](http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide), and [Rect collision fuctions](http://www.pygame.org/docs/ref/rect.html) – ninMonkey Aug 05 '16 at 15:26