简体   繁体   English

3D 控制台游戏距离检测无法正常工作

[英]3D console game distance detection not working properly

I am trying to make a 3D game in a python console.我正在尝试在 python 控制台中制作 3D 游戏。 There aren't any errors but when I run it, it doesn't show anything (which is fine) because it doesn't detect any blocks (#) in the players view (not fine).没有任何错误,但是当我运行它时,它没有显示任何内容(这很好),因为它没有在玩家视图中检测到任何块(#)(不好)。

The problem is probably around line 78, which is always True (on the first distance check of the ray) and I don't know why (that's my problem).问题可能在第 78 行附近,它始终为True (在射线的第一次距离检查中),我不知道为什么(这是我的问题)。 I will of course add player-movement and a map that's more complex.我当然会添加玩家运动和更复杂的地图。 At the moment the player just rotates right at a hardcoded speed.目前,玩家只是以硬编码的速度向右旋转。

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))

It now works.它现在可以工作了。 The hardMap was previously called gameMap but I changed it due to wanting a 1d array but still an option to easily change the map. hardMap以前称为gameMap但由于想要一个 1d 数组但仍然可以轻松更改地图,因此我更改了它。 I just changed: mapWidth = len(gameMap[0]) mapHeight = len(gameMap) to mapWidth = len(hardMap[0]) mapHeight = len(hardMap)我刚刚改变了: mapWidth = len(gameMap[0]) mapHeight = len(gameMap)mapWidth = len(hardMap[0]) mapHeight = len(hardMap)

Thanks Random Davis!感谢随机戴维斯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM