Python Forum

Full Version: raycasting error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im not sure what is happening to my code (im trying to make a raycaster) but it keeps displaying this error.
Error:
IndexError: list index out of range on line 97 in main.py
Here is my code
import turtle
import math
import pygame
pygame.init()
pygame.display.set_mode()
events = pygame.event.get()

colors = {1: "Blue", 2: "Mediumblue", 3: "Red", 4: "Crimson"}

# Instructions:
# UP = forward
# LEFT = left
# RIGHT = right
# DOWN = backwards
# Do not run into the walls partly because I haven't coded the collision
# detection and also partly because it displays an error right after


screen = turtle.Screen()
screen.bgcolor("White")

turtx = 0
turty = 0
plrx = 10
plry = 10
plrA = 45

nX = 0
nY = 0

draw = turtle.Turtle()
draw.penup()
draw.pensize(6)
draw.speed(0)
draw.hideturtle()

angle = -35
distance = 0

lis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
lis2 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

mymap = [
[ 1, 1, 1, 1, 1, 1,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]]

while True:
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LEFT:
        plrA = plrA - 15
        break
      if event.key == pygame.K_RIGHT:
        plrA = plrA + 15
        break
      if event.key == pygame.K_UP:
        plrx = plrx + (math.sin(math.radians(90 - plrA)) * 10)
        plry = plry + (math.cos(math.radians(90 - plrA)) * 10)
        break
      if event.key == pygame.K_DOWN:
        plrx = plrx - (math.sin(math.radians(90 - plrA)) * 10)
        plry = plry - (math.cos(math.radians(90 - plrA)) * 10)
        break
      
  angle = -35
  for i in range(70):
    turtx = plrx
    turty = plry
    distance = 0
    print turtx
    for m in range(150):
      if (mymap[math.floor(turtx)][math.floor(turty)] == 1):
        break
      nX = abs(math.floor(turtx + 1))
      nY = abs(math.floor(turty + 1))
      lis2[i] = 0
      if (nX < nY):
        turtx = nX
        turty = turty + (math.tan(math.radians(90 - angle)) * nX)
      elif (nY < nX):
        turty = nY
        turtx = turtx + (nY / math.tan(math.radians(90 - angle)))
      else:
        turtx = nX
        turty = turty + (math.tan(math.radians(90 - angle)) * nX)
        
      distance = math.sqrt(((turtx - plrx) ** 2) + ((turty - plry) ** 2))
    
    
    lis[i] = distance * math.sin(math.radians(90 - angle))
    angle += 1
  draw.setx(-200)
  draw.clear()
  for i in range(70):
    draw.tracer(0, 0)
    draw.sety(200)
    draw.pendown()
    draw.color("Grey")
    draw.sety(2000 / lis[i])
    draw.color(colors.get(lis2[i]))
    draw.sety(-2000 / lis[i])
    draw.color("Saddlebrown")
    draw.sety(-200)
    draw.penup()
    draw.setx(draw.xcor() + 5)
    draw.color("Black")
  draw.update()
Are you posting your real code, or different code than produced that error? The line number in the error doesn't match that line in the code you posted.

You should provide the real code for the error as well as the traceback, which will have the function call stack which causes the result.
Tip. Instead of really longs lines of zero.
lis = []
for i in range(96):
    lis.append(0)
Even shorter.
lis = [0 for i range(96)]
Looks like mymap very last line is missing a 1.
Even shorter
lis = [0]*96
thank you i will try that