Python 从上掉落的矩形仅与列表中的一个矩形碰撞?
我试图使下降的矩形与两个静态矩形碰撞,但它只与列表中的第二个矩形碰撞。我该怎么做?我尝试使用rect.coliderect,但它会使rect重叠。但我不想让它们重叠。我甚至使用了rect.bottom=tile.top,但它不起作用。请帮忙Python 从上掉落的矩形仅与列表中的一个矩形碰撞?,python,game-physics,collision,rect,Python,Game Physics,Collision,Rect,我试图使下降的矩形与两个静态矩形碰撞,但它只与列表中的第二个矩形碰撞。我该怎么做?我尝试使用rect.coliderect,但它会使rect重叠。但我不想让它们重叠。我甚至使用了rect.bottom=tile.top,但它不起作用。请帮忙 import pygame dis = pygame.display.set_mode((500, 500)) clock = pygame.time.Clock() x = 200 y = 10
import pygame
dis = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
x = 200
y = 10
gra = 10
air_timer = 0
jumped = False
def collisions(rect, tiles):
collide = {
'top': False,
'bottom': False,
'left': False,
'right': False
}
for tile in tiles:
# if tile.top <= rect.top <= tile.bottom:
# if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
# collide["top"] = True
# else:
# collide["top"] = False
# else:
# collide["top"] = False
if tile.top <= rect.bottom <= tile.bottom:
if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
collide["bottom"] = True
else:
collide["bottom"] = False
else:
collide["bottom"] = False
# if tile.top <= rect.bottom <= tile.bottom:
# if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
# collide['bottom'] = True
# else:
# collide['bottom'] = False
# else:
# collide['bottom'] = False
#
# if tile.top <= rect.bottom <= tile.bottom:
# if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
# collide['bottom'] = True
# else:
# collide['bottom'] = False
# else:
# collide['bottom'] = False
return collide
run = True
while run:
clock.tick(30)
dis.fill((255, 255, 255))
y += gra
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(dis, (16, 50, 255), (x, y, 25, 50), 1)
fall = pygame.Rect(x, y, 25, 50)
pygame.draw.rect(dis, (16, 50, 255), (200, 400, 50, 50), 1)
fix = pygame.Rect(200, 400, 50, 50)
pygame.draw.rect(dis, (16, 50, 255), (300, 350, 50, 50), 1)
fix2 = pygame.Rect(300, 350, 50, 50)
fixed = [fix2, fix]
collision = collisions(fall, fixed)
if collision["bottom"]:
gra = 0
air_timer = 0
else:
gra = 10
air_timer += 1
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
x -= 5
if key[pygame.K_RIGHT]:
x += 5
if key[pygame.K_UP]:
if air_timer < 6:
gra = -30
elif air_timer >= 6:
gra = 10
pygame.display.update()
导入pygame
dis=pygame.display.set_模式((500500))
clock=pygame.time.clock()
x=200
y=10
gra=10
空气计时器=0
跳跃=错误
def冲突(矩形、平铺):
碰撞={
“顶部”:错误,
“底部”:错误,
“左”:假,
“对”:错误
}
对于瓷砖中的瓷砖:
#如果是瓦片,请点击顶部