首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇形怪状的穿墙虫,碰撞系统游戏

奇形怪状的穿墙虫,碰撞系统游戏
EN

Stack Overflow用户
提问于 2016-04-06 14:14:02
回答 1查看 58关注 0票数 0
代码语言:javascript
复制
def checkCollision(self):
        for entity in self.Entities:
            ox = entity.x
            oy = entity.y
            for block in self.Blocks:

                if block.y < entity.y and entity.y < block.y + block.size:
                    if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x):
                        print ("Hit Bottom")
                        entity.tpEntity(ox, oy+0.5)
                elif entity.y < block.y and block.y < entity.y+entity.height:
                    if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x):
                        print ("Hit Top")
                        entity.tpEntity(ox, oy-self.gravity)
                elif entity.x < block.x and block.x < entity.x+entity.width:
                    if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height):
                        print("Hit Left")
                        entity.tpEntity(ox-0.5,oy)
                elif entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x:
                    if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height):
                        print("Hit Right")
                        entity.tpEntity(ox+0.5,oy)

我有一个奇怪的错误,如果我走到墙上,它总是注册它,好像它在上面。有人能向我解释一下为什么会发生这种情况吗?我应该如何解决这个问题?谢谢您:)

编辑: self.gravity变量是行星/地图上的重力,因为它在每个行星/地图上应该是不同的。默认情况下(此处)为0.2。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-07 04:02:58

我假设实体和块是类?如果使用pygame.Rect对象设置每个对象,并在实体移动时对其进行更新,则可以通过以下操作简化此操作:

代码语言:javascript
复制
def checkCollision(self):
    for entity in self.Entities:
        ox = entity.x
        oy = entity.y
        for block in self.Blocks:
            if entity.rect.colliderect(block.rect):

然后测试x和y的位置来确定反射。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36453773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档