如何检查我的角色与哪个对象碰撞? [英] How to check which object my character is colliding with?

查看:77
本文介绍了如何检查我的角色与哪个对象碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏进行碰撞检测,因此,如果角色碰到物体,它们就会停止移动。作为我的解决方案的一部分,我制作了两个函数,这些函数遍历屏幕上的对象列表以检查碰撞。

I am working on collision detection for a game, so that if the character runs into an object they stop moving. As part of my solution for this, I have made two functions that iterate through a list of on screen objects to check for collision.

第一个函数(on_key_up)检查

The first function (on_key_up) checks for collision when the direction key is held down and then stops character movement when True.

此功能的第二部分是另一个功能(on_key_up),该功能可将字符稍微移出碰撞区,因此他可以自由移动。

The second part of this is another function (on_key_up) that moves the character slightly out of the collision zone so he is free to move again. This is triggered when releasing the direction key.

问题是我需要访问角色与之碰撞的任何对象的height和width属性,所以我想知道是否有一种方法可以做到这一点。

The problem is that I need to access the height and width attributes of whichever object the character is colliding with so I was wondering if there is a way to do this.

我张贴了我所做的摘要,以便您可以看到我要进入的方向。

I posted a snippet of what I did so you can see the direction I'm trying to go in.

我在这里做什么错了?

def on_keyboard_up(self, keyboard, keycode):
        if keycode[1] == 'left':
            self.source = 'selectionscreen/faceleft.png'
                for i in listofwidgets:
                    if self.collide_widget(i):
                        self.x = ((listofwidgets(i).x + listofwidgets(i).width + .1)-self.x) + self.x


推荐答案

我认为您只需更改以下内容即可:

I think you need to simply change:

self.x =(((listofwidgets(i).x + listofwidgets(i).width + .1)-self.x)+ self.x

至:

self.x =(ix + i.width + .1)-self.x)+ self.x

listofwidgets(i)是一个函数调用,它可能应该是 listofwidgets [i] (如果不只是错字)

Also listofwidgets(i) would be a function call, it should probably be listofwidgets[i] (if it wasn't just a typo)

这篇关于如何检查我的角色与哪个对象碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆