在Pygame for loops中,冲突检测不起作用

时间:2018-07-17 03:42:09

标签: python python-2.7 for-loop pygame collision-detection

很抱歉,如果这不是一个很笼统的问题,但我只是知道这是一件很具体的事情,应该行得通,

为什么绿色播放器碰到黄色框时无法检测到碰撞?

对于其他所有混乱情况,我们深表歉意,但是我找不到要删除的代码以使代码更小,同时仍然保留了核心游戏的内容。现在主要是准系统。 '导入pygame,sys,时间

pygame.init()

isRunning = True

windowWidth = 1280
windowHeight = 720

playerHeight = 64
playerWidth = 64
playerX = (windowWidth / 2) - (playerWidth / 2) + 100
playerY = (windowHeight / 2) - (playerHeight / 2)
playerXLate = playerX
playerYLate = playerY
playerSpeed = 4

blockHeight = 64
blockWidth = 64
blockX = 0
blockY = 0

number = 3

playerMovingForewards = False
playerMovingLeft = False
playerMovingBackwards = False
playerMovingRight = False

playerColor = (0, 255, 0)
blockColor = (255, 255, 0)
backgroundColor = (0, 0, 255)

window = pygame.display.set_mode((windowWidth, windowHeight))

while isRunning == True:

    Map = open('Map', 'r')
    mapY = Map.readlines()
    mapX = mapY[0].split()

    for y in range(len(mapY)):
            mapX = mapY[y]
            for x in range(len(mapX)):
                    if mapX[x] == "1":
                            if playerX + playerWidth >= (x * 64) and playerX <= (x * 64) + blockWidth and playerY + playerHeight >= (y * 64) and playerY <= (y * 64) + blockHeight:
                                    playerX = playerXLate
                                    playerY = playerYLate
                            pygame.draw.rect (window, (blockColor), ((x / 2) * 64, y * 64, blockWidth, blockHeight))

    Map.close()

    playerXLate = playerX
    playerYLate = playerY

    for event in pygame.event.get():

            if (event.type == pygame.QUIT):
                    isRunning = False
                    pygame.quit()
                    sys.exit()

            if(event.type == pygame.KEYDOWN):
                    if (event.key == pygame.K_w):
                            playerMovingForewards = True
                    if (event.key == pygame.K_a):
                            playerMovingLeft = True
                    if (event.key == pygame.K_s):
                            playerMovingBackwards = True
                    if (event.key == pygame.K_d):
                            playerMovingRight = True

            if(event.type == pygame.KEYUP):
                    if (event.key == pygame.K_w):
                            playerMovingForewards = False
                    if (event.key == pygame.K_a):
                            playerMovingLeft = False
                    if (event.key == pygame.K_s):
                            playerMovingBackwards = False
                    if (event.key == pygame.K_d):
                            playerMovingRight = False

    if playerMovingForewards == True:
            playerY = playerY + -playerSpeed
    if playerMovingLeft == True:
            playerX = playerX + -playerSpeed
    if playerMovingBackwards == True:
            playerY = playerY + playerSpeed
    if playerMovingRight == True:
            playerX = playerX + playerSpeed

    #Graphics (Start)

    pygame.draw.rect (window, (playerColor), (playerX, playerY, playerWidth, playerHeight))

    pygame.display.update()

    pygame.draw.rect (window, (0, 0, 0), (0, 0, windowWidth, windowHeight))

    pygame.draw.rect (window, (backgroundColor), (windowWidth - windowWidth, windowHeight - windowHeight, windowWidth, windowHeight))

    #Graphics (End)

    time.sleep(0.01)

pygame.quit()
sys.exit()

“地图”文件为:

1 1 1 1 0 0 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 2 0 0 1
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 0 0 1 1 1 1

1 个答案:

答案 0 :(得分:0)

好吧,所以我在进行检测,我发现必须将x变量除以2。我仍然不知道该怎么做,但由于某种原因,它确实可以。我认为它不应该这样工作,但是出于某种原因。