Fix food spawn test and optimize test execution
This commit is contained in:
parent
cb626dfe7d
commit
fcab6ec766
@ -3,4 +3,8 @@ testpaths = tests
|
||||
python_files = test_*.py
|
||||
python_classes = Test*
|
||||
python_functions = test_*
|
||||
addopts = -v --tb=short
|
||||
addopts = -v --tb=short -n auto
|
||||
markers =
|
||||
slow: marks tests as slow (deselect with '-m "not slow"')
|
||||
integration: marks tests as integration tests
|
||||
unit: marks tests as unit tests
|
@ -2,3 +2,4 @@ pygame==2.5.2
|
||||
numpy==1.24.3
|
||||
black==23.12.1 # for code formatting
|
||||
pytest==7.4.3 # for testing
|
||||
pytest-xdist==3.5.0 # for parallel test execution
|
18
src/food.py
18
src/food.py
@ -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"""
|
||||
|
@ -6,11 +6,12 @@ import os
|
||||
# Add src directory to Python path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def pygame_init():
|
||||
"""Initialize pygame for all tests"""
|
||||
def pytest_configure(config):
|
||||
"""Initialize pygame once before any tests run"""
|
||||
pygame.init()
|
||||
yield
|
||||
|
||||
def pytest_unconfigure(config):
|
||||
"""Clean up pygame after all tests are done"""
|
||||
pygame.quit()
|
||||
|
||||
@pytest.fixture
|
||||
|
Loading…
Reference in New Issue
Block a user