Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In the spirit of Terry's Tutorials - GUARDIAN Alien Space Ship Game
#3
Next we will start the alien ship at a random upper location in either the right or left side of the screen. Right or left is determined by the inital direction, so if the ship is traveling right, it will enter from the left. How about if up or down is the initial direction. Well, we disallowed this as part of an addition to and existing statement that exits the sub if either of those directions get selected.

Code: (Select All)
IF ran = oldran OR olda_y = 0 AND olda_x = 0 AND ran = 1 OR olda_y = 0 AND olda_x = 0 AND ran = 5 THEN

The other code added to achieve the side entry is:

Code: (Select All)
            IF olda_y = 0 AND olda_x = 0 THEN ' New alien space ship enters the screen.
                a_y = (bottom - top) \ 2: a_x = (right - left) \ 2 ' Center sreen.
                a_y = (bottom - top) \ 4 + RND * (bottom - top) \ 4
                IF ran < 5 THEN ' Determine side of entry from initial direction.
                    a_x = left + 1 ' Enter from the left side and go right.
                ELSE
                    a_x = right - LEN(alien$) ' Enter from the right side and go left.
                END IF
            END IF


Stage 2)

Code: (Select All)
DIM SHARED top, bottom, left, right
a = 120: b = 42
WIDTH a, b
_SCREENMOVE 0, 0
top = 3: bottom = _HEIGHT: left = 0: right = _WIDTH
msg$ = "Alien space ship movement demo."
LOCATE 1, (right - left) \ 2 - LEN(msg$) \ 2
PRINT msg$;
LOCATE 1, 2: PRINT STRING$(_WIDTH, "_");

DO
    _LIMIT 30
    alien_move
LOOP

SUB alien_move:
    STATIC a_y, a_x, olda_y, olda_x, alien$, inertia, ran, ran_y, ran_x, oldran, z5
    IF ABS(z5 - TIMER) > .1 THEN ' Time delay.
        y_restore = CSRLIN: x_restore = POS(0) ' Restore column and row upon exit.
        IF alien$ = "" THEN alien$ = "-<>-"
        IF olda_y <> 0 AND olda_x <> 0 THEN LOCATE olda_y, olda_x: PRINT SPACE$(LEN(alien$));

        IF inertia = 0 THEN
            inertia = INT(RND * (bottom - top) / 2) + 1 ' How many moves to go in any one direction.
            ran = INT(RND * 8) + 1 ' Choose 1 of 8 possible directions.

            IF ran = oldran OR olda_y = 0 AND olda_x = 0 AND ran = 1 OR olda_y = 0 AND olda_x = 0 AND ran = 5 THEN
                LOCATE y_restore, x_restore: EXIT SUB ' Just hover if direction was not changed on existing alien space ship or if a new alien space ship is entering from the sides and up or down was generated.
            END IF
            SELECT CASE ran ' Get changes in column and row coordinates.
                CASE 1: ran_y = -1: ran_x = 0 '  Up.
                CASE 2: ran_y = -1: ran_x = 1 '  Up and right.
                CASE 3: ran_y = 0: ran_x = 1 '   Right.
                CASE 4: ran_y = 1: ran_x = 1 '   Down and right.
                CASE 5: ran_y = 1: ran_x = 0 '   Down.
                CASE 6: ran_y = 1: ran_x = -1 '  Down and left.
                CASE 7: ran_y = 0: ran_x = -1 '  Left.
                CASE 8: ran_y = -1: ran_x = -1 ' Up and left.
            END SELECT

            IF olda_y = 0 AND olda_x = 0 THEN ' New alien space ship enters the screen.
                a_y = (bottom - top) \ 2: a_x = (right - left) \ 2 ' Center sreen.
                a_y = (bottom - top) \ 4 + RND * (bottom - top) \ 4
                IF ran < 5 THEN ' Determine side of entry from initial direction.
                    a_x = left + 1 ' Enter from the left side and go right.
                ELSE
                    a_x = right - LEN(alien$) ' Enter from the right side and go left.
                END IF
            END IF
            oldran = ran ' Remember last direction. Another line uses this to disallow any RND that chooses the same direction twice.
        ELSE
            inertia = inertia - 1 ' Count down the number of moves in any one direction. When zero, switch direction.
        END IF
        a_y = a_y + ran_y: a_x = a_x + ran_x * 2 ' Next move coordinates. I use * 2 for horizontal movement to match the 16x8 pixel height over width factor.
        IF a_y < top OR a_y > bottom OR a_x <= left OR a_x + LEN(alien$) > right THEN
            olda_x = 0: olda_y = 0: inertia = 0: oldran = 0 ' Out of bounds and off the screen.
        ELSE
            LOCATE a_y, a_x: PRINT alien$; ' Move alien space ship.
            olda_y = a_y: olda_x = a_x ' Remember these coordinates to erase alien space ship on next loop.
        END IF
        z5 = TIMER
        LOCATE y_restore, x_restore ' Restore entry column and row positions.
    END IF
END SUB

The next stage will demonstrate how to use arrays to put two or more ships on the screen.

Pete
Reply


Messages In This Thread
RE: In the spirit of Terry's Tutorials - Alien Space Ship - by Pete - 10-04-2022, 10:21 PM



Users browsing this thread: 4 Guest(s)