Apr-19-2024, 03:34 AM
(This post was last modified: Apr-19-2024, 03:34 AM by attractalderman.)
It's not necessary, in my opinion, to compute every ray point. As each point is calculated, I would review it and halt when a collision is found. basket random
def illuminate_block(self, blocks, ray_point): """ Find first block that collides with ray_point, Returns block or None """ for block in blocks: if block[1].rect.collidepoint(ray_point): block[1].illuminated = True return block return None def cast_rays(self): cx = self.rect.centerx cy = self.rect.centery blocks = [] for b in self.game.level.block_dict.values(): b.illuminated = False # Cull the block list here so it doesn't have to happen for each ray point. if abs(b[0][0] - cx) < 200 and abs(b[0][1] - cy) < 200: blocks.append[b] for ray in range(0, 360, 20): # Do expensive sin and cos once per ray sin_ray = math.sin(ray) cos_ray = math.cos(ray) for depth in range(20, 100, 5): ray_point = (cx - sin_ray * depth, cy + cos_ray * depth) if self.illuminate_block(blocks, ray_point) is not None: breakThere are pygame routines that can do the task for you if they are sprites.