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