Skip to content

Improve performance for Food's random position initialization #4

@douglascdev

Description

@douglascdev

When a Food object is created, a random generation function is used to create it's position, excluding the snake's position from the possible generated positions:

self.rect = random_pos_rect(
    Game.DEFAULT_RECT, [sprite.rect for sprite in snake_group.sprites()]
)
  • Change the way the second argument is passed to this function, passing the group and checking for collisions with it instead, to improve performance.
  • Keep the full list of points on screen in memory for the function and only limit the width and height within the generator.
  • If the excluded_rects is empty, return a random item of the list without creating the rects_without_collision list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions