简体   繁体   中英

Making sprites bounce off of each other

To download the code, follow the link :

Background :

So I've been going through pygame tutorials since I'm new to it and I found Eli Bendersky's well-known tutorial . I was going through part one and was attempting to add my own flair to it by making it "Social Cats". The cats would wander around and if they touched each other then they would graze each other and go their separate ways. In other words the same thing Eli had but with collision detection and new sprites. I figure this would be good practice. I've spent the past few days researching collision detection and how different people do it, but I've yet to see one that would work with my scenario or something similar. I'm beginning to see how small of a community I'm in.

Objective :

Ultimately, I am trying to make it so when a cat runs into another one, the one that collided will go off in a random direction that equals 45 degrees less or more than its current direction.

Problem :

I'm importing vec2d and I have my Cat class and my main. I would like to do the collision detection in the main because later on I will create a GameManager class that watches over what's going on. According to OOP, the cats shouldn't know about each other anyways. I've been having trouble getting the collision detection to work. I've tried a couple of different ways. In both ways nothing happens when they touch each other. I'm getting the impression what I'm wanting to do is way more complex than how I'm perceiving it. How am I screwing this up? I feel as if I've wasted enough time on this one aspect. Of course, that's the learning process. Thoughts?

Way 1:

    mainWindow.fill(_white_)
    for cat in cats:
        cat.update(timePassed)
        cat.blitme()
        catsCollided = pg.sprite.spritecollide(cat, catGroup, True)
        print pg.sprite.spritecollide(cat, catGroup, False)

    for cat in catsCollided:
        cat.currentDirection.angle = randint(int(cat.currentDirection.angle - 45), int(cat.currentDirection.angle + 45))   

Way 2:

    mainWindow.fill(_white_)
    for cat in cats:
        cat.update(timePassed)
        cat.blitme()
        print pg.sprite.spritecollide(cat, catGroup, False)


    tempCatList = list(cats)
    for catA in cats:
        tempCatList.remove(catA)
        for catB in cats:
            if catA.rect.colliderect(catB.rect):
                cats[cats.index(catA)].currentDirection.angle = randint(int(cat.currentDirection.angle - 45), int(cat.currentDirection.angle + 45))

Your first way is correct, however there are just a few bugs. Sprite collide is the best way to do it. First of all, there are very few circumstances when you want the third argument in sprite collide to be true, and unless I completely misunderstand how your code is doing it, you do not want to use True. When you specify True, it will automatically delete both sprites upon collision. The other thing is that you want to make sure to filter out self collisions. Basically, if a sprite runs sprite collide on itself it registers a collision with itself. One final thing about your code is that although your randint chooser might work (you may want to test what it is returning though), random.choice() would be a better fit for what you are looking for. When these changes are implemented, it looks something like this:

mainWindow.fill(_white_)
for cat in cats:
    cat.update(timePassed)
    cat.blitme()
    catsCollided = pg.sprite.spritecollide(cat, catGroup, False)   #False makes it so colliding sprites are not deleted
    print pg.sprite.spritecollide(cat, catGroup, False)

for cat in catsCollided:                                           #by the way although this is perfectly fine code, the repetition of the cat variable could be confusing
    if cat != self:                                                #checks that this is not a self collision
        cat.currentDirection.angle = random.choice([int(cat.currentDirection.angle - 45), int(cat.currentDirection.angle + 45])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM