3D主机游戏距离检测无法正常工作



我正试图在python控制台中制作一款3D游戏。没有任何错误,但当我运行它时,它不会显示任何内容(这很好(,因为它在玩家视图中没有检测到任何块(#((不好(。

问题可能在78线附近,它总是True(在射线的第一次距离检查中(,我不知道为什么(这是我的问题(。我当然会添加玩家移动和更复杂的地图。此刻,玩家只是以硬编码的速度向右旋转。

import os
import time
import math
import threading

hardMap = [   #exists so I can edit it
["#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".",".","#"],
["#",".",".",".",".",".",".",".",".",".",".",".",".",".","#","#"],
["#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"]
]

gameMap = "".join(["".join(hardMap[n]) for n in range(len(hardMap))])
notDone = True
playerX = 2
playerY = 2
playerA = 0 #player angle --> +1 turn right; -1 turn left
fov = 4
fov = math.pi / fov
depth = 20
fps = 30
screenWidth = 120
screenHeight = 40
os.system(f'mode con: cols={screenWidth} lines={screenHeight}')
screen = [" " for n in range(screenHeight * screenWidth)]
mapWidth = len(gameMap[0])
mapHeight = len(gameMap)

def printScreen(string):
os.system('cls')
time.sleep(0.01)  # reduce flickering
for x in [string[i:i+screenWidth] for i in range(0,len(string),screenWidth)]:
print("".join(x))

while(notDone):

playerA = playerA + 0.01
startTime = time.time()
for x in range(screenWidth):
rayAngle = (playerA - fov / 2) + ((x / screenWidth) * fov)
distanceToWall = 0
hitWall = False
eyeX = math.sin(rayAngle)
eyeY = math.cos(rayAngle)
while not hitWall and distanceToWall < depth:
distanceToWall += 0.1

testX = int(playerX + eyeX * distanceToWall)
testY = int(playerY + eyeY * distanceToWall)

distanceBeforTheIf = distanceToWall

if testX < 0 or testX >= mapWidth or testY < 0 or testY>= mapHeight: #---------------THE PROBLEM------------
hitWall = True
distanceToWall = depth
with open("log.txt","a") as f:
f.write(f"angle: {playerA}ndistanceToWall:{distanceToWall}ndistanceBeforTheIf :{distanceBeforTheIf}")

elif gameMap[testY * mapWidth + testX] == "#":    
hitWall = True


ceiling = int((screenHeight / 2.0) - (screenHeight / distanceToWall))
floor = screenHeight - ceiling  
for y in range(screenHeight):

if distanceToWall <= depth / 4: shade = u"u2591"
elif distanceToWall < depth / 3: shade = u"u2592"
elif distanceToWall < depth / 2: shade = u"u2593"
elif distanceToWall < depth / 1: shade = u"u2588"
else: shade = " "

if y < ceiling:
screen[y * ceiling + x] = " "
elif y > ceiling and y <= floor:
screen[y * screenWidth + x] = shade
else:
screen[y * screenWidth + x] = " "
printScreen(screen)           
time.sleep(max(1./fps - (time.time() - startTime), 0))

现在可以工作了。hardMap以前被称为gameMap,但我更改了它,因为我想要一个1d数组,但仍然可以轻松更改映射。我只是改变了:mapWidth = len(gameMap[0]) mapHeight = len(gameMap)mapWidth = len(hardMap[0]) mapHeight = len(hardMap)

谢谢Random Davis!

最新更新