简体   繁体   English

使精灵互相弹起

[英]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 . 因此,自从接触它以来,我就一直在阅读pygame教程,并找到了Eli Bendersky的著名教程 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. 换句话说,Eli拥有相同的东西,但是具有碰撞检测和新的Sprite。 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. 最终,我试图做到这一点,以便当一只猫碰到另一只猫时,发生碰撞的那只猫会沿等于或小于当前方向45度的随机方向飞行。

Problem : 问题

I'm importing vec2d and I have my Cat class and my main. 我正在导入vec2d,并且具有Cat类和主类。 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. 我想主要进行碰撞检测,因为稍后我将创建一个GameManager类来监视发生的事情。 According to OOP, the cats shouldn't know about each other anyways. 根据OOP的说法,猫们无论如何都不应该互相认识。 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: 方法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: 方式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. 首先,在极少数情况下,您希望sprite碰撞中的第三个参数为true,除非我完全误解了代码的运行方式,否则您不希望使用True。 When you specify True, it will automatically delete both sprites upon collision. 当指定True时,它将在碰撞时自动删除两个精灵。 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. 基本上,如果一个sprite运行,sprite会在其自身上发生碰撞,则它会与自身发生碰撞。 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. 关于代码的最后一件事是,尽管您的randint选择器可能会工作(尽管您可能想测试它返回的内容),但random.choice()会更适合您要查找的内容。 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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