GLBasic forum

Main forum => Tutorials => Topic started by: Brick Redux on 2013-Aug-20

Title: Surrounded by Zombies
Post by: Brick Redux on 2013-Aug-20
Heres something I knocked up in 2 hours that was inspired from an old CPC type-in game.  The CPC version had you avoiding smily faces that homed in on you in 8x8 pixel steps. 

My code gives you pixel by pixel approaching hordes that rotate to always face you.  The code could be adapted to display a tank or car very easily.

For fun Ive given the player a gun so that you can shoot them all down.  The sprites included have no transparency folks. 

Hope someone finds it useful.


LEFT/RIGHT cursors to rotate the player. UP cursor to move forward & SPACE to fire.

Code: GLBasic [Select]
////////////////////////
//                         //
//       GRUNTS    //
//                        //
///////////////////////

// Avoid the Zombies,their dying to meet you...yawn.

GOSUB setup

WHILE TRUE

        move_the_grunts()

        move_the_player()

        shoot()

SHOWSCREEN

WEND


FUNCTION move_the_player:

IF KEY(203) // Cursor LEFT.

        INC angle

ELSEIF KEY(205) // Cursor RIGHT.

        DEC angle

ENDIF

IF KEY(200) // Cursor UP

        DEC player_x,SIN(angle)
        DEC player_y,COS(angle)

ENDIF

IF KEY(57) AND player_shot=0 // Space bar = FIRE

        player_shot=1 // New shot initialised.
        oldangle=angle // Get vector angle for the new bullet.

        sx=player_x+SIN(oldangle) // Assign the above into
        sy=player_y+COS(oldangle) // the bullets x & y projectory.

ENDIF
ROTOSPRITE 1,player_x,player_y,angle

ENDFUNCTION



FUNCTION shoot:

IF player_shot=1 // Gun has just fired.

        DEC sx,shot_speed*SIN(oldangle) // Move x & y along "oldangle" vector.

        DEC sy,shot_speed*COS(oldangle)

                INC shot_timer // Check how long the bullets been moving.

                        IF shot_timer=shot_distance

                                player_shot=0 // Remove the bullet.

                                shot_timer=0 // Reset the timer.

                        ENDIF

        PRINT "@",sx,sy // Draw the Shot on screen.

ENDIF

ENDFUNCTION



FUNCTION move_the_grunts:

SMOOTHSHADING 0

        FOR a=0 TO 18

        IF grunts[a].hit=0 // If not been shot yet.

                LookX = player_x - grunts[a].x // Calc the angle
        LookY = player_y - grunts[a].y // so that each grunt
        LookAtPlayer = -ATAN(LookY, LookX)-90 // faces the player.
       
                        ROTOSPRITE 0,grunts[a].x,grunts[a].y,LookAtPlayer
     
     INC grunts[a].timer
     
        IF grunts[a].timer=grunts[a].limit // Grunt must move...urgggh!
           
                                IF grunts[a].x<player_x
               
                                        INC grunts[a].x,.1
               
                                ELSEIF grunts[a].x>player_x
               
                                        DEC grunts[a].x,.1
                               
                                ENDIF
               
                                IF grunts[a].y<player_y
               
                                        INC grunts[a].y,.1
               
                                ELSEIF grunts[a].y>player_y
               
                                        DEC grunts[a].y,.1
               
                                ENDIF

                                        grunts[a].timer=0
                       
                ENDIF
               
                // Check if a grunt has been hit.
               
                IF BOXCOLL(sx,sy,10,10,grunts[a].x,grunts[a].y,10,10)=1
                grunts[a].hit=1 // Turn the grunt off.
                player_shot=0 // Reset
                ENDIF

ENDIF

NEXT


ENDFUNCTION


SUB setup:

GETSCREENSIZE screenx,screeny

TYPE oinks
        x
        y
        timer // Determines when the grunt can move...
        limit // ...by reaching a time limit.
        hit // Is the grunt dead (erm its already un-dead) or alive.
        talk // display a line of text.
ENDTYPE

GLOBAL grunts[] AS oinks ; DIM grunts[20]

FOR a=0 TO 18

        grunts[a].x=RND(screenx) ; grunts[a].y=RND(screeny)
        grunts[a].timer=0 ; grunts[a].limit=5+RND(5) ; grunts[a].hit=0

NEXT

// Player data.  Not kept inside a TYPE, this is so that readers can better understand how each component works.(if needed.)

        player_x=100 // XPOS.
        player_y=100 // YPOS
        player_speed=1 // INCREASE TO MOVE QUICKER
        player_shot=0 // SHOT NOT-FIRED/FIRED FLAG
        shot_timer=0 // COUNTS UNTIL IT REACHS "SHOT_DISTANCE"
        shot_distance=150 // THE LIMIT A SHOT CAN TRAVEL.
        shot_speed=2 //
        angle=0 // USED BY THE PLAYER AND BULLET.

// CREATE A SIMPLE PLAYER SPRITE (SEEN FROM ABOVE)

        DRAWRECT 0,2,24,10,RGB(0,100,255);DRAWRECT 8,0,8,8,RGB(255,255,100)
        GRABSPRITE 1,0,0,24,24

// And a grunt sprite.

        DRAWRECT 0,2,24,10,RGB(0,50,255);DRAWRECT 8,0,8,8,RGB(0,255,0)
        GRABSPRITE 0,0,0,24,24
       
ENDSUB



 
Title: Re: Surrounded by Zombies
Post by: fuzzy70 on 2013-Aug-20
Thanks for sharing that Brick, Nice bit of "Quick" fun  :good:

Lee
Title: Re: Surrounded by Zombies
Post by: mentalthink on 2013-Aug-20
Thanks this can be something like gauntlet?¿, modifing something...  :good:
Title: Re: Surrounded by Zombies
Post by: erico on 2013-Aug-25
gave a go, really great, could also be adapted for a twin shooter. :good: