0

I am working on a pygame 2d raycasting project and I am trying to have a bunch of lines that start at the mouse pos and all end at the edge of the screen, evenly spaced out. For some reason, when I create new instances of my line class and print out their 'self.end_point' they are all the same point, even though if I print out the end_point that I am passing into the init method, it is the values I want. Here is the code:

class Line():
    def __init__(self, start_pos, end_pos, color = 'black'):
        self.start_pos = start_pos
        self.end_pos = end_pos
        self.color = color

    def update(self, mx, my):
        self.start_pos = (mx, my)
        print(self.end_pos) # This does not print out the desired outcome

    def draw(self):
        self.rect = pygame.draw.line(display, self.color, self.start_pos, self.end_pos)

end_pos = [0, 0]
perimeter = (WINDOW_SIZE[0] + WINDOW_SIZE[1]) * 2
for line in range(NUM_LINES):
    if end_pos[0] < WINDOW_SIZE[0] and end_pos[1] == 0:
        end_pos[0] += perimeter/NUM_LINES
    if end_pos[1] < WINDOW_SIZE[1] and end_pos[0] >= WINDOW_SIZE[0]:
        end_pos[0] = WINDOW_SIZE[0]
        end_pos[1] += perimeter/NUM_LINES
    if end_pos[0] > 0 and end_pos[1] >= WINDOW_SIZE[1]:
        end_pos[1] = WINDOW_SIZE[1]
        end_pos[0] -= perimeter/NUM_LINES
    if end_pos[1] > 0 and end_pos[0] <= 0:
        end_pos[0] = 0
        end_pos[1] -= perimeter/NUM_LINES
    print(end_pos) # This prints out the desired outcome
    lines.append(Line(pygame.mouse.get_pos, end_pos))
Nobody
  • 33
  • 1
  • 1
  • 6
  • Could this be a side-effect of creating lines with a `start_pos` that's a reference the function `pygame.mouse.get_pos`, rather than the output of a call to that function `pygame.mouse.get_pos()` ? Although I wouldn't image that's a problem until `Line.draw()` is called before any call to `Line.update()`. – Kingsley Jul 29 '20 at 01:24
  • NUM_LINES is an int to represent how many lines there are. in this case its 200 – Nobody Jul 29 '20 at 01:24
  • Thanks for pointing out that typo that I didn't call get_pos, but it still didn't fix it – Nobody Jul 29 '20 at 01:26
  • The moderator was mistaken in thinking that this is a duplicate question. It isn't. Here your problem is that update() is changing the start position, but printing out the end position. This is why your position doesn't seem to change. You are printing out the wrong variable. – user10637953 Jul 29 '20 at 01:56
  • The print location is not the issue. When drawing the line, it doesn't draw at the correct endpoint, and I can print it at different places and it still returns the same thing – Nobody Jul 29 '20 at 02:09
  • Then, perhaps the mod was correct and this issue is with multiple list names pointing to the same object in memory. Without a working example to study, it's all just speculation on my part, and the thread has been locked by the mod. Read over the link the mod gave and go from there. Good luck my friend. – user10637953 Jul 29 '20 at 13:01
  • The moderator is correct. All the instances of `Line` are storing a reference to the same `end_pos` list and so when that sigle list gets updated they all get updated. You can easily resolve this by doing a copy when making the assignment, like this `self.start_pos = start_pos[:]` and `self.end_pos= end_pos[:]` – Glenn Mackintosh Jul 30 '20 at 17:54

0 Answers0