Update the army position based on the current size of the field.
(session, window, state)
| 559 | |
| 560 | |
| 561 | def move_army(session, window, state): |
| 562 | """Update the army position based on the current |
| 563 | size of the field.""" |
| 564 | speed = 30 // 25 * state["num_enemies"] |
| 565 | |
| 566 | flip = (state["tick"] % speed) == 0 |
| 567 | |
| 568 | if not flip: |
| 569 | return |
| 570 | else: |
| 571 | state["flip"] = not state["flip"] |
| 572 | |
| 573 | x_slide = 1 |
| 574 | |
| 575 | # get the lower/upper boundaries of the army |
| 576 | # along the X axis. |
| 577 | min_x, max_x = ( |
| 578 | session.query( |
| 579 | func.min(GlyphCoordinate.x), |
| 580 | func.max(GlyphCoordinate.x + GlyphCoordinate.width), |
| 581 | ) |
| 582 | .join(GlyphCoordinate.glyph.of_type(ArmyGlyph)) |
| 583 | .first() |
| 584 | ) |
| 585 | |
| 586 | if min_x is None or max_x is None: |
| 587 | # no enemies |
| 588 | return |
| 589 | |
| 590 | direction = state["army_direction"] |
| 591 | move_y = False |
| 592 | if direction == 0 and max_x + x_slide >= MAX_X: |
| 593 | direction = state["army_direction"] = 1 |
| 594 | move_y = True |
| 595 | elif direction == 1 and min_x - x_slide <= 0: |
| 596 | direction = state["army_direction"] = 0 |
| 597 | move_y = True |
| 598 | |
| 599 | for enemy_g in session.query(GlyphCoordinate).join( |
| 600 | GlyphCoordinate.glyph.of_type(ArmyGlyph) |
| 601 | ): |
| 602 | enemy_g.blank(window) |
| 603 | |
| 604 | if move_y: |
| 605 | enemy_g.y += 1 |
| 606 | elif direction == 0: |
| 607 | enemy_g.x += x_slide |
| 608 | elif direction == 1: |
| 609 | enemy_g.x -= x_slide |
| 610 | |
| 611 | |
| 612 | def move_player(session, window, state): |