def __init__(self, game_level, main_window, menu_widget): self.is_paused = False super().__init__() self.animation_manager = AnimationManager() self.ball_pixmap_provider = BallPixmapProvider() self.main_window = main_window self.menu_widget = menu_widget self.angle_provider = AngleProvider() self.game_level = game_level self.balls_float_animation = 0 self.pausa_opacity = 0 self.buttons = [ MenuButton(200, 400, "resources/kadilo.png", self.back), MenuButton(200, 600, "resources/zavarudo.png", self.pause) ] self.task_manager = TaskManager() self.x = 1 self.y = 1 self.is_meta_menud = False self.game_state = GameState(game_level, RandomColorManager(time.time())) self.game_state.set_animation_manager(self.animation_manager) self.grid = QGridLayout() self.canvas = QPixmap(800, 800) self.label = QLabel() self.label.setPixmap(self.canvas) tracker = MouseTracker(self.label) tracker.positionChanged.connect(self.on_position_changed) self.grid.addWidget(self.label) self.setLayout(self.grid) self.animation_manager.add_animation(TipAnimation()) self.timer = QTimer() self.timer.timeout.connect(self.handle_timer) self.timer.start(40)
class GameState: """Инкапсулирует всю игровую логику""" def __init__(self, game_level, random_color_manager): self.angle = 0 self.random_color_manager = random_color_manager self.animation_manager = AnimationManager() self.game_level = game_level self.balls = [] self.frog_operator = FrogOperator(self) self.task_manager = TaskManager() self.score = 0 self.lost = False self.game_ended_win = False self.balls_conveyor = BallsConveyor(self, game_level) def set_animation_manager(self, animation_manager): self.animation_manager = animation_manager def tick(self): if (len(self.balls_conveyor.balls_list) == 0 and self.balls_conveyor.no_balls_remain): self.game_ended_win = True self.task_manager.task_tick() self.balls_conveyor.tick() self.tick_flying_balls() self.frog_operator.down_cooldowns() self.balls_conveyor.place_balls() def tick_flying_balls(self): for i in self.balls: if i.must_been_deleted: self.balls.remove(i) for i in self.balls: i.tick() self.balls_conveyor.try_to_inplace_ball(i) if i.x > 800 or i.y > 800 or i.x < 0 or i.y < 0: i.must_been_deleted = True def shot_a_ball(self, angle): self.frog_operator.shot_a_ball(angle) def swap_balls(self): self.frog_operator.swap_balls() def change_balls(self): self.frog_operator.change_balls() def add_task(self, task): self.task_manager.add_task(task)
def __init__(self, game_level, random_color_manager): self.angle = 0 self.random_color_manager = random_color_manager self.animation_manager = AnimationManager() self.game_level = game_level self.balls = [] self.frog_operator = FrogOperator(self) self.task_manager = TaskManager() self.score = 0 self.lost = False self.game_ended_win = False self.balls_conveyor = BallsConveyor(self, game_level)
def main(): # LOGGING SETTINGS logging.basicConfig(stream=sys.stdout, level=logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) log = logging.getLogger() log.handlers.clear() log.addHandler(ch) log.info("Start application") # RABBIT MQ PRODUCER-CONSUMER SETTINGS consumer_config = make_config(os.environ['RABBIT_IN_HOST'], int(os.environ['RABBIT_IN_PORT']), os.environ['RABBIT_IN_QUEUE'], os.environ['RABBIT_IN_LOGIN'], os.environ['RABBIT_IN_PASS']) producer_config = make_config(os.environ['RABBIT_OUT_HOST'], int(os.environ['RABBIT_OUT_PORT']), os.environ['RABBIT_OUT_QUEUE'], os.environ['RABBIT_OUT_LOGIN'], os.environ['RABBIT_OUT_PASS']) # producer_config = make_config("localhost", 5672, "WriteQ", "guest", "guest") # consumer_config = make_config("localhost", 5672, "ReadQ", "guest", "guest") producer = RMQProducer(producer_config) task_manager = TaskManager(producer) consumer = RMQConsumer(consumer_config, task_manager) consumer.start_consuming()
import sys def make_config(host, queue, login, password): config = dict() config['host'] = host config['queue'] = queue config['login'] = login config['password'] = password return config # LOGGING SETTINGS logging.basicConfig(stream=sys.stdout, level=logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) log = logging.getLogger() log.handlers.clear() log.addHandler(ch) log.info("Start application") # RABBIT MQ PRODUCER-CONSUMER SETTINGS producer_config = make_config("10.90.138.229", "queue2", "guest", "guest") consumer_config = make_config("10.90.138.176", "queue1", "guest", "guest") producer = RMQProducer(producer_config) task_manager = TaskManager(producer) consumer = RMQConsumer(consumer_config, task_manager) consumer.start_consuming()
def startTaskManager(): taskManager = TaskManager(FIFO_PATH) taskManager.start()
class GameWidget(QWidget): """Виджет самой игры""" positionChanged = pyqtSignal(QPoint) def __init__(self, game_level, main_window, menu_widget): self.is_paused = False super().__init__() self.animation_manager = AnimationManager() self.ball_pixmap_provider = BallPixmapProvider() self.main_window = main_window self.menu_widget = menu_widget self.angle_provider = AngleProvider() self.game_level = game_level self.balls_float_animation = 0 self.pausa_opacity = 0 self.buttons = [ MenuButton(200, 400, "resources/kadilo.png", self.back), MenuButton(200, 600, "resources/zavarudo.png", self.pause) ] self.task_manager = TaskManager() self.x = 1 self.y = 1 self.is_meta_menud = False self.game_state = GameState(game_level, RandomColorManager(time.time())) self.game_state.set_animation_manager(self.animation_manager) self.grid = QGridLayout() self.canvas = QPixmap(800, 800) self.label = QLabel() self.label.setPixmap(self.canvas) tracker = MouseTracker(self.label) tracker.positionChanged.connect(self.on_position_changed) self.grid.addWidget(self.label) self.setLayout(self.grid) self.animation_manager.add_animation(TipAnimation()) self.timer = QTimer() self.timer.timeout.connect(self.handle_timer) self.timer.start(40) def pause(self): self.is_paused = not self.is_paused if self.is_paused: self.task_manager.add_task(UpPausaTask(self)) else: self.task_manager.add_task(DownPausaTask(self)) def draw_meta_menus(self): if self.is_meta_menud: self.draw_buttons() def change_meta_menud_state(self): self.is_meta_menud = not self.is_meta_menud def add_balls_float_parameter(self): self.balls_float_animation += 0.2 def handle_timer(self): self.task_manager.task_tick() if self.game_state.game_ended_win: self.end_game_win() if self.game_state.lost: self.end_game_lose() if not self.is_paused: self.add_balls_float_parameter() # visual effect self.game_state.tick() self.angle = self.angle_provider.get_angle(self.x, self.y) self.draw_game_state() def left_click(self): self.main_window.setFocus() self.game_state.shot_a_ball(self.angle) def set_pausa_opacity(self, pausa_opacity): self.pausa_opacity = pausa_opacity def right_click(self): self.game_state.swap_balls() def wheel_click(self): self.game_state.change_balls() def mousePressEvent(self, a0: QMouseEvent) -> None: if self.is_meta_menud: for i in self.buttons: if i.is_pressed: i.on_click() if a0.button() == 1: self.left_click() elif a0.button() == 2: self.right_click() elif a0.button() == 4: self.wheel_click() def draw_game_state(self): self.label.setPixmap(QPixmap(self.game_level.map_resource)) self.qp = QPainter(self.label.pixmap()) self.qp.setFont(QFont("arial", 22)) self.draw_central_frog() self.draw_conveyor_balls() self.draw_flying_balls() self.draw_score() self.animation_manager.draw_animations(self.qp) self.draw_meta_menus() self.draw_pausa() self.qp.end() self.update() def draw_buttons(self): for i in self.buttons: x, y, w, h = i.get_geometry() self.qp.drawPixmap(x, y, w, h, QPixmap(i.get_pixmap())) self.qp.drawPixmap(x, y, w, h, QPixmap(i.text_resource)) def draw_score(self): self.qp.setPen(QColor(0, 0, 0)) x, y = self.game_level.score_position self.qp.drawText(x, y, str(self.game_state.score)) def draw_flying_balls(self): bpp = self.ball_pixmap_provider for i in self.game_state.balls: if i.must_been_deleted: continue self.qp.drawPixmap(i.x - 21, i.y - 21, 42, 42, bpp.get_pixmap(i.color)) def draw_conveyor_balls(self): bpp = self.ball_pixmap_provider for i in self.game_state.balls_conveyor.get_balls_list(): x, y = self.game_state.balls_conveyor.get_ball_position(i) self.qp.drawPixmap(x - i.diameter / 2, y - i.diameter / 2, i.diameter, i.diameter, bpp.get_pixmap(i.color)) def draw_pausa(self): if not self.is_paused: return self.qp.setOpacity(self.pausa_opacity) self.qp.drawPixmap(150, 100, 312 * 1.5, 180 * 1.5, QPixmap("resources/pausa_png.png")) self.qp.setOpacity(1) def draw_central_frog(self): bpp = self.ball_pixmap_provider self.qp.setPen(QPen(QColor("black"), 3)) x, y = self.game_level.frog_position self.qp.drawPixmap(x - 35, y - 50, QPixmap("resources/frog.png")) p = self.game_state.frog_operator.balls_swap_parameter p2 = self.game_state.frog_operator.balls_swap_parameter2 p3 = 1 - self.game_state.frog_operator.balls_swap_parameter3 self.qp.drawPixmap( x + 40 * p, y - 60 * p, p3 * (42 - 21 * p), p3 * (42 - 21 * p), bpp.get_pixmap(self.game_state.frog_operator.first_ball_color)) self.qp.drawPixmap( x + 40 - 80 * p, y - 60 + 10 * math.sin(self.balls_float_animation), p3 * 21, p3 * 21, bpp.get_pixmap(self.game_state.frog_operator.second_ball_color)) self.qp.drawPixmap( x - 40 + 40 * p2, y - 60 + 60 * p2 + 10 * math.sin(self.balls_float_animation), p3 * (21 - 21 * p + 42 * p2), p3 * (21 - 21 * p + 42 * p2), bpp.get_pixmap(self.game_state.frog_operator.third_ball_color)) if self.game_state.frog_operator.change_balls_cooldown > 0: self.qp.drawText( x, y + 80, str( int(self.game_state.frog_operator.change_balls_cooldown / 25))) self.qp.drawLine(x + 15, y + 15, x + 15 + 100 * math.cos(self.angle), y + 15 + 100 * math.sin(self.angle)) def back(self): self.main_window.setCentralWidget(type(self.main_window)()) def end_game_win(self): self.main_window.setCentralWidget( EndGameWinWidget(self.main_window, self.menu_widget, self.game_state.score, self.game_level.number)) def end_game_lose(self): self.main_window.setCentralWidget( EndGameLoseWidget(self.main_window, self.menu_widget)) @pyqtSlot(QPoint) def on_position_changed(self, pos): for i in self.buttons: x, y, w, h = i.get_geometry() if x < pos.x() < x + w and y < pos.y() < y + h: i.is_pressed = True else: i.is_pressed = False self.x = pos.x() - 415 self.y = pos.y() - 415