pygame 矩形碰撞检测不起作用
pygame rectangle collision detection does not work
我正在尝试为我的游戏添加一个平台,这样一旦你击中它,玩家就会留在平台上直到它掉下来,在这种情况下它会撞到地面。跳跃有效但碰撞无效
我尝试在下面添加一些典型类型的碰撞检测器,但它们都有缺点
class Platform:
def __init__(self, x, y, background):
self.xpos = x
self.ypos = y
self.picture = background
self.picture = pygame.transform.scale(self.picture, (500, 100))
self.rect = self.picture.get_rect()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Player:
def __init__(self, x, y, pic_one, pic_two, pic_three, pic_four):
self.xpos = x
self.ypos = y
self.speed_y = 0
self.speed_x = 0
self.picture = pic_one
self.picture_one = pic_one
self.picture_two = pic_two
self.picture_three = pic_three
self.picture_four = pic_four
self.on_ground = True
self.picture = pygame.transform.scale(self.picture, (100, 100))
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
self.ypos += self.speed_y
GRAVITY = 0.9
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
if self.ypos >= 620:
self.ypos = 620
self.speed_y = 0
self.on_ground = True
if self.on_ground == True:
self.speed_y = 0
def jump(self):
if self.on_ground:
self.on_ground = False
self.speed_y = -20#Makes you move upwards
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
player_one = Player(30, 600, player_one_first_pic)
wood_platform = Platform(400, 400, platform_pic)
while True:
[...]
这是我尝试并使用过的典型碰撞类型,但 if 语句似乎从未发生过
if player_one.rect.bottom == wood_platform.rect.top:
player_one.on_ground = True
我试过的另一个检测矩形是否碰撞
即使矩形没有碰撞,if 语句也为 True
if player_one.is_collided_with(wood_platform):
player_one.on_ground = True
这个方法我不太习惯,因为它不够
if player_one.ypos == 600:
player_one.on_ground = True
我使用的方法的实际结果是玩家根本没有与木制平台发生碰撞
(如果您确实需要更多的理解,这里有一张图表:)
检查碰撞但似乎不起作用
if player_one.rect.y == wood_platform.rect.y:
print(True)
player_one.on_ground = True
on_platform = True
else:
on_platform = False
每次更新后都有变化
self.rect.x = self.xpos
self.rect.y = self.ypos
至于我的问题是您使用 self.xpos
、self.ypos
来移动对象并且您没有更改 self.rect
中的值(self.rect.x
、self.rect.y
) 但你使用 self.rect
来检查碰撞。
你必须设置
self.rect.x = self.xpos
self.rect.y = self.ypos
在检查碰撞之前。
而且您必须对所有移动的对象执行此操作。也许每次移动后都这样做更好。
您只能使用 self.rect.x
、self.rect.y
(而不是 self.xpos, self.ypos
),但它只能保留整数值。您在重力中使用浮点值,self.rect.x
、self.rect.y
会向下舍入位置并给出不那么流畅的动画。
但不使用浮动重力或浮动速度的物体只能使用 self.rect
来保持位置。
我在工作中经常遇到同样的问题。使用浮点数时检查数字是否相等可能会失败。你可以尝试改变这个
if player_one.rect.bottom == wood_platform.rect.top:
player_one.on_ground = True
对此:
margin = 0.001 # Whatever amount you feel comfy with.
if wood_platform.rect.top*(1-margin) <= player_one.rect.bottom <= wood_platform.rect.top*(1+margin):
player_one.on_ground = True
我正在尝试为我的游戏添加一个平台,这样一旦你击中它,玩家就会留在平台上直到它掉下来,在这种情况下它会撞到地面。跳跃有效但碰撞无效
我尝试在下面添加一些典型类型的碰撞检测器,但它们都有缺点
class Platform:
def __init__(self, x, y, background):
self.xpos = x
self.ypos = y
self.picture = background
self.picture = pygame.transform.scale(self.picture, (500, 100))
self.rect = self.picture.get_rect()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Player:
def __init__(self, x, y, pic_one, pic_two, pic_three, pic_four):
self.xpos = x
self.ypos = y
self.speed_y = 0
self.speed_x = 0
self.picture = pic_one
self.picture_one = pic_one
self.picture_two = pic_two
self.picture_three = pic_three
self.picture_four = pic_four
self.on_ground = True
self.picture = pygame.transform.scale(self.picture, (100, 100))
self.rect = self.picture.get_rect()
def update(self):
self.xpos += self.speed_x
self.ypos += self.speed_y
GRAVITY = 0.9
self.speed_y += GRAVITY # Accelerate downwards.
self.xpos += self.speed_x
self.ypos += self.speed_y
if self.ypos >= 620:
self.ypos = 620
self.speed_y = 0
self.on_ground = True
if self.on_ground == True:
self.speed_y = 0
def jump(self):
if self.on_ground:
self.on_ground = False
self.speed_y = -20#Makes you move upwards
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
def is_collided_with(self, sprite):
return self.rect.colliderect(sprite.rect)
player_one = Player(30, 600, player_one_first_pic)
wood_platform = Platform(400, 400, platform_pic)
while True:
[...]
这是我尝试并使用过的典型碰撞类型,但 if 语句似乎从未发生过
if player_one.rect.bottom == wood_platform.rect.top:
player_one.on_ground = True
我试过的另一个检测矩形是否碰撞 即使矩形没有碰撞,if 语句也为 True
if player_one.is_collided_with(wood_platform):
player_one.on_ground = True
这个方法我不太习惯,因为它不够
if player_one.ypos == 600:
player_one.on_ground = True
我使用的方法的实际结果是玩家根本没有与木制平台发生碰撞
(如果您确实需要更多的理解,这里有一张图表:)
if player_one.rect.y == wood_platform.rect.y:
print(True)
player_one.on_ground = True
on_platform = True
else:
on_platform = False
每次更新后都有变化
self.rect.x = self.xpos
self.rect.y = self.ypos
至于我的问题是您使用 self.xpos
、self.ypos
来移动对象并且您没有更改 self.rect
中的值(self.rect.x
、self.rect.y
) 但你使用 self.rect
来检查碰撞。
你必须设置
self.rect.x = self.xpos
self.rect.y = self.ypos
在检查碰撞之前。
而且您必须对所有移动的对象执行此操作。也许每次移动后都这样做更好。
您只能使用 self.rect.x
、self.rect.y
(而不是 self.xpos, self.ypos
),但它只能保留整数值。您在重力中使用浮点值,self.rect.x
、self.rect.y
会向下舍入位置并给出不那么流畅的动画。
但不使用浮动重力或浮动速度的物体只能使用 self.rect
来保持位置。
我在工作中经常遇到同样的问题。使用浮点数时检查数字是否相等可能会失败。你可以尝试改变这个
if player_one.rect.bottom == wood_platform.rect.top:
player_one.on_ground = True
对此:
margin = 0.001 # Whatever amount you feel comfy with.
if wood_platform.rect.top*(1-margin) <= player_one.rect.bottom <= wood_platform.rect.top*(1+margin):
player_one.on_ground = True