Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raycasting error
#1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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,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, 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, 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, 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()
Reply
#2
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.
Reply
#3
Tip. Instead of really longs lines of zero.
1
2
3
lis = []
for i in range(96):
    lis.append(0)
Even shorter.
1
lis = [0 for i range(96)]
Looks like mymap very last line is missing a 1.
99 percent of computer problems exists between chair and keyboard.
Reply
#4
Even shorter
1
lis = [0]*96
Reply
#5
thank you i will try that
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Breaking Raycasting loop XavierPlatinum 5 4,831 Oct-03-2023, 02:29 AM
Last Post: squeamishfall
  Raycasting(again) walls with different heights robie972003 0 3,191 Mar-23-2019, 01:18 AM
Last Post: robie972003

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020