This class generates food for the snake to eat.
| 8 | import colors |
| 9 | |
| 10 | class Food(Turtle): |
| 11 | """ This class generates food for the snake to eat. """ |
| 12 | def __init__(self): |
| 13 | super().__init__() |
| 14 | self.shape("circle") |
| 15 | self.penup() |
| 16 | self.shapesize(stretch_len=0.7, stretch_wid=0.7) |
| 17 | self.color(colors.FOOD_COLOR) |
| 18 | self.speed("fastest") |
| 19 | |
| 20 | def refresh(self, left_wall, right_wall, bottom_wall, top_wall): |
| 21 | """Moves the food to a new random position within the provided game boundaries.""" |
| 22 | # Add a margin so food doesn't spawn exactly on the edge |
| 23 | margin = 20 |
| 24 | random_x = random.randint(int(left_wall) + margin, int(right_wall) - margin) |
| 25 | random_y = random.randint(int(bottom_wall) + margin, int(top_wall) - margin) |
| 26 | self.goto(random_x, random_y) |
| 27 |