检测玩家是否与对象底部发生碰撞(矩形)

时间:2015-12-27 18:38:07

标签: python pygame collision

我试图在pygame中创建一只鸟类观看游戏(例如热线迈阿密)。我已经陷入了碰撞检测。我试图看看球员是否在左侧,右侧,前方或后方击中了一个区域。我只是做了前面的'碰撞,但不起作用;玩家将首先击中该区域,然后停止,但是如果他发射箭头键,则可以移动到该区块。我知道问题是什么(在碰撞检测中的if else语句中)但是我不能在我的生活中找到解决方案。我尝试过的所有东西似乎都不起作用。救命!任何帮助将不胜感激,提前感谢! (顺便说一下这是我第一次关于堆栈溢出,很抱歉,如果我没有问好问题)(我删除了一些不必要的代码,如pygame.init()和变量)

def drawBlock(block_width,block_height,x,y):
    pygame.draw.rect(gameDisplay, red, [x,y,block_width,block_height])

def gameLoop(x,y):

    while not gameExit:


        level =  ["W","W","W","W","W","W","W","W","W","W","W","W",
                  "N","N","N","N","N","N","N","N","N","N","N","N","L",
                  "N","N","N","N","N","N","N","N","N","N","N","N","L",
                  "N","N","N","N","P","N","N","N","N","N","N","N","L",                                       
                  "N","N","N","N","N","N","N","N","N","N","N","N","L",                                     
                  "N","N","N","N","N","N","N","N","N","N","N","N","L",
                  "N","N","N","N","N","N","N","N","N","N","N","N","L",
                  "N","N","N","N","N","N","N","N","N","N","N","N","L",
                  "N","N","N","N","N","N","N","N","N","P","N","N","L",
                  "N","N","N","N","P","N","N","N","N","N","N","N","L",
                  "N","N","N","N","N","N","N","N","N","N","N","N","L","S"]    
#EVENT HANDLING

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if canMoveUp:
                    if event.key == pygame.K_UP:
                        y_change = -player_movement

                if canMoveDown:
                    if event.key == pygame.K_DOWN:
                        y_change = player_movement


                if event.key == pygame.K_LEFT:
                    x_change = -player_movement

                if event.key == pygame.K_RIGHT:
                    x_change = player_movement

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN: 
                    y_change = 0
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        pX += x_change
        pY += y_change

        gameDisplay.fill(black)


#CALCULATING THE SIZE OF THE BLOCKS SO THEY FIT W/ A VARIETY OF RESOLUTIONS


        if calculateBlockX:

            for element in level:
                if element == "W":
                    W_count += 1
                if element == "S":
                    block_width = (display_width/W_count) 
                    calculateBlockX = False

        if calculateBlockY:

            for element in level:
                if element == "L":
                    L_count += 1
                if element == "S":
                    block_height = (display_height/L_count)
                    calculateBlockY = False


#COUNTING HOW MANY BLOCKS THERE ARE IN THE LEVEL ARRAY (P)

        if P_countFunction:
            for element in level:
                if element == "P":
                    P_count += 1
                if element == "S":
                    print(P_count)
                    P_countFunction = False


#ALL THE X AND Ys OF ALL THE BLOCKS IN THE LEVEL, AND ACTUALLY DRAWING THEM

        blockXY = []

        for element in level:
            if element == "N":
                 x += block_width
            if element == "L":                                              
                 y += block_height
                 x = 0
            if element == "P": 
                drawBlock(block_width,block_height,x,y)                
                blockXY.append(x)
                blockXY.append(y)
                if appendBlockXY:
                    if len(collisionArray) > P_count:
                        del(collisionArray[P_count])
                        print(collisionArray)
                        appendBlockXY = False
                    collisionArray.append(blockXY)
                blockXY = []
                x += block_width
            if element == "S":
                y = 0

#COLLISION DETECTION              

        for XnY in collisionArray:

                if pX >= XnY[0] and pX <= XnY[0] + block_width and pY + playerSize <= XnY[1] or pX + playerSize >= XnY[0] and pX <= XnY[0] + block_width:

                    if pY - block_height == XnY[1]:
                        canMoveUp = False
                        y_change = 0

                    if pY - block_height != XnY[1]:
                        canMoveUp = True
                else:
                    if pY - block_height >= XnY[1]: 
                        canMoveUp = True



        #gameDisplay.blit(img,[pX,pY])

        pygame.draw.rect(gameDisplay, green, [pX,pY,playerSize,playerSize])

        clock.tick(60)

        pygame.display.update()


    pygame.quit()
    quit()

gameLoop(x,y)        

pygame.display.update()

1 个答案:

答案 0 :(得分:0)

您的碰撞检测的编写方式允许最后一次迭代覆盖所有早期检查。取而代之的是,在循环之前将canMoveUp设置为True,并且循环应该只将其设置为False或不管它。