/
ap1k
/
PyGame_Practice
Обзор
Документация
Войти
/
ap1k
/
PyGame_Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/patterns/enemy_factory.py
39 строк
1 KB
Roman
The all game
27 май 2025, 15:26
27 май 2025, 15:26
4ea7de9
Код
Авторство
О чём код?
""" Enemy Factory pattern implementation Creates different types of enemies """ import random from src.entities.enemy import BasicEnemy, FastEnemy, TankEnemy class EnemyFactory: """ Factory class for creating different types of enemies Implements the Factory Method pattern """ @staticmethod def create_enemy(enemy_type=None, x=None, y=None): """ Create an enemy of the specified type or a random type if none is specified """ # If no enemy type is specified, choose a random one if enemy_type is None: enemy_type = random.choice(["basic", "fast", "tank"]) # If no position is specified, choose a random x position at the top of the screen if x is None: x = random.randint(0, 800 - 30) # 30 is the default enemy width # If no y position is specified, start at the top of the screen if y is None: y = -30 # Start above the screen # Create the appropriate enemy type if enemy_type == "basic": return BasicEnemy(x, y) elif enemy_type == "fast": return FastEnemy(x, y) elif enemy_type == "tank": return TankEnemy(x, y) else: # Default to basic enemy if an invalid type is specified return BasicEnemy(x, y)