如何开始调试我的Python Raycaster?

时间:2018-03-13 13:54:14

标签: python python-3.x pygame raycasting

我很抱歉让这个标题模糊不清,但老实说我不知道​​如何描述它的表现。

我的代码几乎是

上示例代码的直接端口

www.lodev.org/cgtutor/raycasting.html

我的完整代码位于https://pastebin.com/tK1CNFps

这是该计划的主要部分。

zde = 0.0000001 #Add to prvent division by zero
res = 2

HEIGHT = 600
WIDTH = 1000

xPos = 10.0
yPos = 5.0
xDir = -1
yDir = 0
xPlane = 0
yPlane = 0.66


for column in range(0,WIDTH,res):
    xCamera = 2 * column / float(WIDTH) - 1.0
    xRayDir = xDir + xPlane * xCamera
    yRayDir = yDir + yPlane * xCamera

    xMap = int(xPos)
    yMap = int(yPos)

    xSideDistance = 0.0
    ySideDistance = 0.0
    perpWallDistance = 0.0

    xDeltaDistance = abs(1/(xRayDir + zde))
    yDeltaDistance = abs(1/(yRayDir + zde))

    xStep = 0
    yStep = 0

    hit = False
    side = 0

    if (xRayDir < 0):
        xStep = -1
        xSideDistance = (xPos - xMap) * xDeltaDistance
    else:
        xStep = -1
        xSideDistance = (xMap + 1.0 - xPos) * xDeltaDistance

    if (xRayDir < 0):
        yStep = -1
        ySideDistance = (yPos - yMap) * yDeltaDistance
    else:
        yStep = 1;
        ySideDistance = (yMap + 1.0 - yPos) * yDeltaDistance

    hit = False
    while not hit:
        if (xSideDistance < ySideDistance):
          xSideDistance += xDeltaDistance
          xMap += xStep
          side = 'x'

        else:
          ySideDistance += yDeltaDistance
          yMap += yStep
          side = 'y'

        if worldMap[xMap][yMap] != 0 :
            hit = True

    if (side == 0):
        perpWallDist = (xMap - xPos + (1 - xStep) / 2) / (xRayDir + zde)
    else:
        perpWallDist = (yMap - yPos + (1 - yStep) / 2) / (yRayDir + zde)

    lineHeight = int(HEIGHT / (perpWallDist + zde))

    drawStart = -lineHeight / 2 + HEIGHT / 2

    if (drawStart < 0):
        drawStart = 0

    drawEnd = lineHeight / 2 + HEIGHT / 2

    if (drawEnd >= HEIGHT):
        drawEnd = HEIGHT - 1

    wallcolours = [ [255,255,255], [150,0,0], [0,150,0], [0,0,150] ]
    colour = wallcolours[worldMap[xMap][yMap]]

    if side == 'y':
        colour = [i/2 for i in colour]

    pygame.draw.line(screen, colour, (column,drawStart), (column, drawEnd), res)

我的截图:https://imgur.com/a/OzB3Z

类似的工作程序的屏幕截图:https://imgur.com/a/76lPq

欢迎任何帮助或建议,因为我甚至不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

我发现了这三个错误(注释行)。如果您解决了这些问题,游戏应该可以正常运行。

if xRayDir < 0:
    xStep = -1
    xSideDistance = (xPos - xMap) * xDeltaDistance
else:
    xStep = 1  # Set xStep to 1 not -1.
    xSideDistance = (xMap + 1.0 - xPos) * xDeltaDistance

if yRayDir < 0:  # xRayDir changed to yRayDir.
    yStep = -1
    ySideDistance = (yPos - yMap) * yDeltaDistance
else:
    yStep = 1;
    ySideDistance = (yMap + 1.0 - yPos) * yDeltaDistance
if side == 'x':  # 0 changed to 'x'. You set `side` to 'x' or 'y' earlier.
    perpWallDist = (xMap - xPos + (1 - xStep) / 2) / (xRayDir + zde)
else:
    perpWallDist = (yMap - yPos + (1 - yStep) / 2) / (yRayDir + zde)