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_files = test_*.py
|
||||||
python_classes = Test*
|
python_classes = Test*
|
||||||
python_functions = 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
|
@ -1,4 +1,5 @@
|
|||||||
pygame==2.5.2
|
pygame==2.5.2
|
||||||
numpy==1.24.3
|
numpy==1.24.3
|
||||||
black==23.12.1 # for code formatting
|
black==23.12.1 # for code formatting
|
||||||
pytest==7.4.3 # for testing
|
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
|
height: Game area height
|
||||||
occupied_positions: List of positions (typically snake body positions) where food shouldn't spawn
|
occupied_positions: List of positions (typically snake body positions) where food shouldn't spawn
|
||||||
"""
|
"""
|
||||||
# Calculate valid grid positions
|
# Calculate all valid grid positions
|
||||||
valid_x = range(0, width, self.block_size)
|
valid_positions = [
|
||||||
valid_y = range(0, height, self.block_size)
|
(x, y)
|
||||||
|
for x in range(0, width, self.block_size)
|
||||||
# Create list of all possible positions
|
for y in range(0, height, self.block_size)
|
||||||
all_positions = [
|
|
||||||
(x, y) for x in valid_x for y in valid_y
|
|
||||||
if (x, y) not in occupied_positions
|
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
|
# No valid positions (snake fills screen) - game should be won
|
||||||
return
|
return
|
||||||
|
|
||||||
# Choose random position
|
# Choose random position from valid positions
|
||||||
self.position = random.choice(all_positions)
|
self.position = random.choice(valid_positions)
|
||||||
|
|
||||||
def draw(self, screen: pygame.Surface) -> None:
|
def draw(self, screen: pygame.Surface) -> None:
|
||||||
"""Draw the food on the screen"""
|
"""Draw the food on the screen"""
|
||||||
|
@ -6,11 +6,12 @@ import os
|
|||||||
# Add src directory to Python path
|
# Add src directory to Python path
|
||||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
def pytest_configure(config):
|
||||||
def pygame_init():
|
"""Initialize pygame once before any tests run"""
|
||||||
"""Initialize pygame for all tests"""
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
yield
|
|
||||||
|
def pytest_unconfigure(config):
|
||||||
|
"""Clean up pygame after all tests are done"""
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
Loading…
Reference in New Issue
Block a user