问题描述
我正在尝试使下降的rect与两个静态rect发生冲突,但它仅与列表中的第二个发生冲突。我怎样才能做到这一点?我尝试使用rect.coliderect,但它使rects重叠。但是我不希望它们重叠。我什至使用rect.bottom = tile.top,但是没有用。请帮忙
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,50)
pygame.draw.rect(dis,(200,400,1)
fix = pygame.Rect(200,(300,350,1)
fix2 = pygame.Rect(300,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()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)