In the spirit of Terry's Tutorials - GUARDIAN Alien Space Ship Game
#1
When I build a program, I do so by making subs that can often act independently. It's a very easy way to join things together for bigger project. It also makes debugging a whole lot easier.

So I thought, since Terry provides excellent tutorials for newbies to fast track coding, I would join in the spirit of that perspective and put up some little project which shows step-wise development.

Alien Space Ship is an ASCII  -<>- that, for starters, that flies randomly throughout the screen. The code is adaptable to various screen sizes. I'll start with a single ship for now. I'll show how to make a married ship, later.

Stage 1)

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$));
        ELSE
            a_y = (bottom - top) \ 2: a_x = (right - left) \ 2 ' Center sreen.
        END IF

        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 THEN LOCATE y_restore, x_restore: EXIT SUB ' Just hover if direction was not changed.
            SELECT CASE ran ' Get changes in column and row coordinates.
                CASE 1: ran_y = -1: ran_x = 0
                CASE 2: ran_y = -1: ran_x = 1
                CASE 3: ran_y = 0: ran_x = 1
                CASE 4: ran_y = 1: ran_x = 1
                CASE 5: ran_y = 1: ran_x = 0
                CASE 6: ran_y = 1: ran_x = -1
                CASE 7: ran_y = 0: ran_x = -1
                CASE 8: ran_y = -1: ran_x = -1
            END SELECT
            oldran = ran ' Remember last direction.
        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 ship.
            olda_y = a_y: olda_x = a_x ' Remember these coordinates to erase 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 a way to start a ship on the screen from the left or right side, determined by initial right or left direction, at a somewhat random vertical starting point. Hey, if you develop a shooter game, it's not much of a challenge if you know in advance where the enemy vessel will appear.

Pete out.
Reply


Messages In This Thread
In the spirit of Terry's Tutorials - GUARDIAN Alien Space Ship Game - by Pete - 10-04-2022, 08:29 PM



Users browsing this thread: 6 Guest(s)