Fix food spawn test and optimize test execution
This commit is contained in:
1 parent
cb626dfe7d
commit
fcab6ec766
4 files changed
+20
-16
No files matched your search
+8
-10
@@ -17,22 +17,20 @@ class Food:
|
||||
height: Game area height
|
||||
occupied_positions: List of positions (typically snake body positions) where food shouldn't spawn
|
||||
"""
|
||||
# Calculate valid grid positions
|
||||
valid_x = range(0, width, self.block_size)
|
||||
valid_y = range(0, height, self.block_size)
|
||||
|
||||
# Create list of all possible positions
|
||||
all_positions = [
|
||||
(x, y) for x in valid_x for y in valid_y
|
||||
# Calculate all valid grid positions
|
||||
valid_positions = [
|
||||
(x, y)
|
||||
for x in range(0, width, self.block_size)
|
||||
for y in range(0, height, self.block_size)
|
||||
if (x, y) not in occupied_positions
|
||||
]
|
||||
|
||||
if not all_positions:
|
||||
if not valid_positions:
|
||||
# No valid positions (snake fills screen) - game should be won
|
||||
return
|
||||
|
||||
# Choose random position
|
||||
self.position = random.choice(all_positions)
|
||||
# Choose random position from valid positions
|
||||
self.position = random.choice(valid_positions)
|
||||
|
||||
def draw(self, screen: pygame.Surface) -> None:
|
||||
"""Draw the food on the screen"""
|
||||
|
||||
Reference in new issue
Block a user