首页 > 解决方案 > 玩家触碰宝宝时如何添加碰撞

问题描述

我有一个游戏,我希望游戏在玩家触摸婴儿时加一分。我知道如何添加分数,但我不知道如何让系统了解它们之间存在冲突。

这是我的代码的主要部分:

while Level1:
    thing_rect = pygame.Rect(random.randrange(display_width), -60, 40, 40)
    thing_speed = 2
    while lives == 3:
        for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return
        # adds the images and movement        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_Pl = x_Pl - 40
            elif event.key == pygame.K_RIGHT:
                x_Pl = x_Pl + 40
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_Pl = x_Pl + 0
    gameDisplay.blit(Level1_Back, (0,0) )
    smallText_2 = pygame.font.Font("LSANS.ttf", 20)
    textSurf, textRect = text_objects("Use the arrow keys to move", smallText_2, black)
    textRect.center = ( (730+(180/2)), (200+(50/2)) )
    gameDisplay.blit(textSurf, textRect)
    gameDisplay.blit(lives_3, (10, 10) )
    gameDisplay.blit(Character_1, (x_Pl, 275))
    thing_rect.y += thing_speed
    if thing_rect.y > display_height:
        thing_rect.y = 0 - thing_rect.height
        thing_rect.x = random.randrange(display_width)

    gameDisplay.blit(Baby_1, thing_rect)
    pygame.display.update()

谢谢大家的帮助。(如果您需要更多代码,请告诉我)

标签: pythonpygame

解决方案


您将需要一个类似这样的 if 语句: if thing_rectX and thing_rectY >= Baby_1X and baby_1Y: touch_baby(paramaters) 显然,您必须创建一个名为 touch_baby 的函数和一些变量来存储婴儿的 X 和 Y 位置以及 thing_rect,但仅此而已。我希望这回答了你的问题。:)


推荐阅读