Skip to content

Commit

Permalink
Added the snake_segments argument to the __init__ method of the `…
Browse files Browse the repository at this point in the history
…Food` class and updated the `generate` method to use it
  • Loading branch information
codez-bot[bot] authored Aug 17, 2023
1 parent 01dee43 commit 10f7c13
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions snake_game_py/food.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
from snake_game_py.snake import Point # Import Point from snake.py to avoid redundancy

class Food:
def __init__(self, snake_segments):
self.position = Point(0, 0)
self.snake_segments = snake_segments
self.generate()

def generate(self):
"""Generate food at a random position on the screen, ensuring it's not on the snake."""
while True:
x = random.randint(0, (SCREEN_WIDTH // BLOCK_SIZE) - 1)
y = random.randint(0, (SCREEN_HEIGHT // BLOCK_SIZE) - 1)
potential_position = Point(x, y)
if potential_position not in self.snake_segments:
self.position = potential_position
break

def draw(self, screen):
"""Draw the food on the screen."""
pygame.draw.rect(screen, (255, 0, 0),
(self.position.x * BLOCK_SIZE, self.position.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
class Food:
def __init__(self, snake_segments):
self.position = Point(0, 0)
self.snake_segments = snake_segments
self.generate()

def generate(self):
"""Generate food at a random position on the screen, ensuring it's not on the snake."""
while True:
x = random.randint(0, (SCREEN_WIDTH // BLOCK_SIZE) - 1)
y = random.randint(0, (SCREEN_HEIGHT // BLOCK_SIZE) - 1)
potential_position = Point(x, y)
if potential_position not in self.snake_segments:
self.position = potential_position
break

def draw(self, screen):
"""Draw the food on the screen."""
pygame.draw.rect(screen, (255, 0, 0),
(self.position.x * BLOCK_SIZE, self.position.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

0 comments on commit 10f7c13

Please sign in to comment.