pygame精灵墙碰撞 [英] pygame sprite wall collision

查看:161
本文介绍了pygame精灵墙碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python和pygame开发平台游戏。整个代码可以在



我尝试了其他方法,例如使用速度来确定碰撞的事实,但这是可行的。到目前为止最好。如果您可以提供解决方案,将不胜感激。

解决方案

感谢用户使用树懒!他所链接的问题使我非常需要澄清。我花了一点时间,但我实现了它。我为碰撞创建了一个函数。

  def wallColl(self,xvel,yvel,colliders):
for collider在对撞机中:
如果pygame.sprite.collide_rect(self,collider):
如果xvel> 0:
self.rect.right = collider.rect.left
self.xvel = 0
如果xvel< 0:
self.rect.left = collider.rect.right
self.xvel = 0
如果yvel< 0:
self.rect.bottom = collider.rect.top
self.onGround =真
self.jumps = 3
self.yvel = 0
如果是yvel > 0:
self.yvel = 0
self.rect.top = collider.rect.bottom

然后在更新函数中调用它们。

  def update(self):
self .rect.x + = self.xvel
#self.walls是一个精灵数组。
self.wallColl(self.xvel,0,self.walls)

self.rect.y-= self.yvel
self.onGround =假
self .wallColl(0,self.yvel,self.walls)

self.wallCollisions()
if self.otherplayers!= None:
self.playerCollisions()

#重力
如果self.onGround == False:
self.yvel-=。0066 * self.mass

self.boundries(上限,下限,
self.down = False

我游戏中的实际使用使可用性成为可能接近完美。虽然不是100%,但这不是一个完美的答案。


I am working on a platform game in python and pygame. The entire code can be found at "https://github.com/C-Kimber/FBLA_Game". The issue I am having is with the collision between the player sprite and wall sprites, specifically the corners. When the player is pressing a x movement key and they jump, the player either does not move, or gets stuck. Here is the collision sample:

def wallCollisions(self):

    block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)

    for block in block_hit_list:


        if self.rect.bottom >= block.rect.top and self.rect.bottom <= block.rect.top + 15:  # Moving down; Hit the top side of the wall
            if self.rect.right > block.rect.left:
                self.rect.bottom = block.rect.top
                self.yvel = 0
                self.onGround = True
                self.jumps = 1
        elif self.rect.top <= block.rect.bottom and self.rect.top >= block.rect.bottom - 15:  # Moving up; Hit the bottom side of the wall
            self.rect.top = block.rect.bottom
            self.yvel = 0
        if self.rect.right >= block.rect.left and self.rect.right <= block.rect.left + 15:  # Moving right; Hit the left side of the wall
            if self.rect.bottom > block.rect.top+15:
                self.rect.right = block.rect.left#+1
                self.xvel = 0
        elif self.rect.left <= block.rect.right and self.rect.left >= block.rect.right - 15:  # Moving left; Hit the right side of the wall
            self.rect.left = block.rect.right#-1
            self.xvel = 0 = block.rect.right#-1
            self.xvel = 0

I've included images on what is happening and what I want.

I have attempted other methods, such as using velocity as determining factos for collision, but this is what is working the best so far. If you could provide a solution it would be greatly appreciated.

解决方案

Thank you to user sloth! The question he linked gave me some much needed clarity. It took me a bit but I implemented it. I created a function for the collision.

def wallColl(self, xvel, yvel, colliders):
    for collider in colliders:
        if pygame.sprite.collide_rect(self, collider):
            if xvel > 0:
                self.rect.right = collider.rect.left
                self.xvel = 0
            if xvel < 0:
                self.rect.left = collider.rect.right
                self.xvel = 0
            if yvel < 0:
                self.rect.bottom = collider.rect.top
                self.onGround = True
                self.jumps = 3
                self.yvel = 0
            if yvel > 0:
                self.yvel = 0
                self.rect.top = collider.rect.bottom

And then I call them in my update function.

def update(self):
    self.rect.x += self.xvel
    # self.walls is an array of sprites.
    self.wallColl(self.xvel, 0, self.walls) 

    self.rect.y -= self.yvel
    self.onGround = False
    self.wallColl(0, self.yvel, self.walls)

    self.wallCollisions()
    if self.otherplayers != None:
        self.playerCollisions()

    # Gravity
    if self.onGround == False:
        self.yvel-=.0066*self.mass

    self.boundries(highbound, lowbound, leftbound, rightbound)
    self.down = False

The actual useage in my game makes usability near perfect. Not 100% though, this is not a perfect answer.

这篇关于pygame精灵墙碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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