Results 1 to 15 of 15

Thread: Review my code //it has comments + further ideas

  1. #1

    Review my code //it has comments + further ideas

    So I created this game, it's kinda fun (not really). Just kind of having trouble with coming up with ideas to make it better other than graphically. Basically you are a little archer who shoots an arrow at zombies who cross the screen. If you don't get em then your gotAway counter goes up. Anyway the only thing I have been thinking that would make it better would be for the enemies to take multiple hits before they are removed. Anyway take a look and let me know what you think.
    PHP Code:
    /*-----------------------------------------
         ****** t0W's ARROW SHOOTER! ******
    *-----------------------------------------*/


    var heroHealth 100// heros starting health
    var enemyHealth 0//will be used to make enemies take multiple hits, not yet in effect

    var num 1//arrows shot, starting at 1
    var 0//enemy spawned, starting at 0

    var gotAway 0//zombies that got away, displayed in text ( see (4)ACTION)
    var zombieK 0//number of zombies killed, displayed in text ( see (4)ACTION)

    /*-----------------------------------------
    * (1) BEGIN HERO
    *-----------------------------------------*/

    //Hero's default stance (when not shooting an arrow)
    heroDefault = function()
    {
        
    hero.armLoose._visible true;
        
    hero.bowString2._visible false;
        
    hero.armAttack._visible true;
        
    hero.bowString._visible true;

    }
    //hero should be in default state at start, so call it now
    heroDefault();

    //Hero's stance when shooting
    heroAttack = function()
    {
        
    hero.armAttack._visible false;
        
    hero.armLoose._visible true;
        
    hero.bowString._visible false;
        
    hero.bowString2._visible true;
    }

    //Character movement (WASD, Arrow keys) & make sure he stays in bounds
    hero.onEnterFrame = function() 
    {
        
    //left
        
    if (Key.isDown(Key.LEFT) || Key.isDown(65))
        {
            
    this._x -= 10;
        }
        
    //left boundary
        
    if (this._x<30
        {
            
    this._x 30;
        }
        
    //right     
        
    if (Key.isDown(Key.RIGHT) || Key.isDown(68)) 
        {
            
    this._x += 10;
        }
        
    //right boundary
        
    if (this._x>Stage.width
        {
            
    this._x Stage.width;
        }
        
    //up     
        
    if (Key.isDown(Key.UP) || Key.isDown(87)) 
        {
            
    this._y -= 10;
        }
        
    //upper boundary
        
    if (this._y<90
        {
            
    this._y 90;
        }
        
    //down     
        
    if (Key.isDown(Key.DOWN) || Key.isDown(83)) 
        {
            
    this._y += 10;
        }
        
    //lower boundary
        
    if (this._y>Stage.height-50
        {
            
    this._y Stage.height-50;
        }
        
    //angle the hero towards mouse position                                             
        
    var x:Number _xmouse-this._x;
        var 
    y:Number _ymouse-this._y;
        var 
    angleRad:Number Math.atan2(yx);
        var 
    angleDeg:Number angleRad/Math.PI*180;
        
    this._rotation angleDeg;
    }
    /*-----------------------------------------
    * END HERO
    *-----------------------------------------*/

    /*------------------------------------------------------------------------------
    * (2) BEGIN ENEMY
    * Note: Enemies death occurs in the shootArrow function (see (3) ARROWS )
    *-------------------------------------------------------------------------------*/
    var enemyArray = new Array(); //stores movie clip instances of enemies as they spawn

    //Enemy function
    newEnemy = function () 
    {
        
    //create a new movieClip instance of enemy when this function is called
        
    var enemy _root.attachMovie("angryEyes""zombie"+i_root.getNextHighestDepth());
        
    enemy._y 80+random(475); //enemy start position (y axis)
        
    enemy._x 800//enemy start position (x axis)
        
        
    enemy.onEnterFrame = function() 
        {
            
    //enemy moves slightly upwards if player is above them
            
    if (this._y>_root.hero._y
            {
                
    this._y -= 1;
            }
            
    //enemy moves slightly downwards if player is below them  
            
    if (this._y<_root.hero._y
            {
                
    this._y += 1;
            }
            
    //enemy's movement speed left
            
    this._x -= 5+random(5); 
            
            
    //If enemy and hero (hero's hat) touch each other
            
    if (this.hitTest(_root.hero.heroHat)) 
            {
                
    //Decrease health and make sure the health cannot be less than 0
                
    if (heroHealth>=0
                {
                    
    heroHealth--; //take away 1 health for every frame that the hero is touching an enemy
                    
    _root.healthBar._x 18//healthbar location on the x axis (so that it stays in one place when health changes)
                    
    _root.healthBar._width -= 2.37//2.37 is 1/100th the length of the health bar, it decreases with heroHealth
                
    }
                
    //Make sure health cannot be less than 0
                
    if (heroHealth<=0
                {
                    
    heroHealth 0;
                }
                
    //Enemies push hero back, but not out of stage                 
                
    if (_root.hero._x>=30
                {
                    
    _root.hero._x -= 12;
                }
            }
            
    //Give enemies some random movement on the y-axis (makes them stagger) and keep them in bounds
            
    if (this._y 80 && this._y Stage.height-50
            {
                
    this._y += -2.5+random(5);
            }
            
    //Upper boundary, keeps enemies below hero's status bar
            
    if (this._y 80)
            {
                
    this._y 80;
            }
            
    //Lower boundary
            
    if (this._y Stage.height-50
            {
                
    this._y Stage.height-50;
            }
            
    //*Note: left and right boundaries are enemies' exit and spawn points respectively
            //Remove enemies if they go out of stage and increase gotAway counter                  
            
    if (wall.wall1.hitTest(this)) 
            {
                
    removeMovieClip(this);
                
    gotAway++;
                
    this.addListener(gotAway); //variable reused in a text string in (4)ACTION, listener is needed
            
    }
        }
        
    //increment zombie value for creating unique instances and add enemy to array
        
    i++;
        
    addEnemy enemyArray.push(enemy);
    }


    /*-----------------------------------------
    * END ENEMY 
    *-----------------------------------------*/


    /*-----------------------------------------
    * (3) BEGIN ARROWS
    *-----------------------------------------*/
    shootArrow = function () 
    {
        
    //create a new movieClip instance of bullet (an arrow, which is a reserved term by flash)
        
    var bullet _root.attachMovie("bullet""bullet"+num_root.getNextHighestDepth());
        var 
    point = {x:this.hero.gunTip._xy:this.hero.gunTip._y};//gunTip is a nested invisible movie clip in hero
        
    this.hero.localToGlobal(point);
        
    bullet._x point.x;
        
    bullet._y point.y;
        
    bullet._rotation _root.hero._rotation//bullet rotates based on hero rotation
        //the arrow on the hero movieClip is a graphic that's hidden when shootArrow is called in (4) ACTION
        
    heroAttack(); //changes graphics
        
        
    bullet.onEnterFrame = function() 
        {
            
    bullet._x += Math.cos(bullet._rotation*(Math.PI/180))*35//x-axis speed based on rotation
            
    bullet._y += Math.sin(bullet._rotation*(Math.PI/180))*35//y-axis speed based on rotation
            
            //remove arrows when they go off screen so they don't fly forever and bog down system
            //instead of using StageWidth and Height there is a movieClip (wall) further out so that you can't see arrows dissapear
            
    if (wall.wall1.hitTest(bullet) || wall.wall2.hitTest(bullet) || wall.wall3.hitTest(bullet) || wall.wall4.hitTest(bullet)) 
            {
                
    removeMovieClip(this);
            }
            
            
    //Loop through enemy array and check for hit between arrows (x,y) and zombies                   
            
    for (i=0i<_root.enemyArray.lengthi++) 
            {
                
    //Check for hit
                
    if (_root.enemyArray[i].hitTest(this._xthis._ytrue)) 
                {
                    
    //Remove arrow if it hits
                    
    removeMovieClip(this);
                    
    //Steal 1 health when you shoot an enemy,
                    
    if (heroHealth<100
                    {
                        
    heroHealth++;
                        
    _root.healthBar._width += 2.37;
                    }
                    
    //enemyHealth--; //not yet in effect
                    
    tutorial.text enemyArray[i]; //just for testing, displays which zombie was hit in middle text on status bar
                    
                    
                    
    if (enemyHealth<=0)//this always evaluates true because enemy health is not yet in effect, set to 0 at top
                    
    {
                        
    //create a new movieClip instance of deadEnemy in the last place that our enemy was when he got hit
                        
    var deadEnemy _root.attachMovie("deadEnemy""deadEnemy"+num_root.getNextHighestDepth(), {_x:enemyArray[i]._x_y:enemyArray[i]._y});
                        
    //remove the enemy instance that was hit
                        
    removeMovieClip(enemyArray[i]);
                        
                        
    zombieK++; //increment the number displayed in text for zombies killed
                        
    deadEnemy.blink.selectable false;
                        
    deadEnemy.blink.text " "//Display text as enemy dies, such as +1 or "ouch" etc
                    
    }
                }
            }
            
    num++; //unique identifier for each arrow
        
    }
    }
    /*-----------------------------------
    * END ARROWS
    *-----------------------------------*/

    /*----------------------------------------------
    * ..... (4) ACTION
    *----------------------------------------------*/

    //Spawn new Enemy at predetermined interval in milliseconds (2nd parameter)
    setInterval(newEnemy800); //800ms, interval works independent of the flash timeline - frames per second

    //Shoot an arrow
    onMouseDown = function () 
    {
        
    shootArrow();
    }
    //Return to default stance after shooting
    onMouseUp = function ()
    {
        
    heroDefault();
    }

    //Dynamic text that is displayed on the status bar, may include more than text in the future
    onEnterFrame = function () 
    {
        
    //text isn't selectable so you dont accidentally click it when moving around and shooting
        
    zombieKilled.selectable false;
        
    zombieExit.selectable false;
        
    heroHealthText.selectable false;
        
    tutorial.selectable false;
        
        
    //Displays your health left over the health bar
        
    heroHealthText.text "Health +"+heroHealth;
        
    //if only one zombie killed it says zombie rip instead of zombies rip, so english correct 
        
    if (zombieK == 1)
        {
            
    zombieKilled.text zombieK+" Zombie RIP!";
        }
        if (
    zombieK>1
        {
            
    zombieKilled.text zombieK+" Zombies RIP!";
        }
        
    //Displays how many enemies escaped your clutches
        
    if (gotAway>0
        {
            
    zombieExit.text gotAway+" ESCAPED!";
        }
    };

    /*------------------------------------------------
    *  END ACTION
    */ 
    Feel free to rip on it. Be honest.
    Last edited by t0w; May 9th, 2008 at 07:58 PM. Reason: code highlighting

  2. #2
    well im not exactly knowledgable in php but heres one idea if you want suggestions for improvement put up the game like an swf or html its alot harder to see the game when your looking at if statements

  3. #3
    hmm from the code it looks like a simple top-down shootemup game. I like the way all the codes are in one place, this is a really good practice in coding. Also you have set up a effect that not only the hero looses health but is also pushed back by the zombies, this will look real cool in actual play. But i dont see much of a AI for the zombies...i know they dont have brains, but if you add a bit of AI, like obstacles/path-finding, and a bit of dodging then this will be super. Obstacles make room for some strategies, and this increases gameplay value.
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  4. #4
    7
    posts
    Registered User
    its a good script dude

  5. #5
    Quote Originally Posted by bluemagica View Post
    hmm from the code it looks like a simple top-down shootemup game. I like the way all the codes are in one place, this is a really good practice in coding. Also you have set up a effect that not only the hero looses health but is also pushed back by the zombies, this will look real cool in actual play. But i dont see much of a AI for the zombies...i know they dont have brains, but if you add a bit of AI, like obstacles/path-finding, and a bit of dodging then this will be super. Obstacles make room for some strategies, and this increases gameplay value.
    Hey thanks, that's a damn good idea. Sorry I didn't check this sooner, I've been debating diving into C++ and Visual studio 2008... and then I start up COD4 or TF2. Heh.

    I'm definately going to give that some thought. Too bad I am terrible at graphics because that would make it all the more entertaining. I may upload the game soon but it doesn't have a fail state yet just the basic mechanics. Let me know if you want to take a look.

    Oh yeah and I switched alot of the if statements out for ternary operators. Much more condensed.


    Oh yeah.. That isn't php, it's actionscript. I just used php for highlighting purposes because I don't know what the bb code is for actionscript highlighting.
    [off topic](But I do know php to some extent)[/off topic]
    Last edited by t0w; May 14th, 2008 at 08:23 PM.

  6. #6
    Perhaps you can make enemies that have to be hit in certain weak spots to do damage or at certain times (animation frames).

    Keep up the good work, look forward to the polished release!

  7. #7
    I think graphic brings the game to next level... but your game play and concept should be solid to begin with. At least that's what I tell myself.. haha.


    You can have variation of enemies (different behaviour/AI). In my last year's TOJAM attempt, we had two enemies.. one dumb one and another that actually followed the player when it's near a player.

    Another idea is that you can make change in variation of weapons. Instead of single type of bullet, you can have one that goes into 3 directions...wave (sine/cosine)

    Or.. may be even Manic shooter type weapons.. haha.. that's my dream.
    Last edited by misterooga; May 15th, 2008 at 02:50 PM.

  8. #8
    I'd like to see it working. By the code and the suggestions (with which i strongly agree), it seems quite a good game. But I'd still like to see it, even if it was an "ugly gfx" version.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  9. #9
    Quote Originally Posted by bluemagica View Post
    I like the way all the codes are in one place, this is a really good practice in coding.
    I may be misunderstanding you, but since when was having all the code in one place good practice in coding?! It seems to me to be the opposite of good practice and essentially going against the principles of OOP.

    Not that that's a problem with a project of this size, but if it gets any larger maintainability will suffer if everything is in one file.

  10. #10
    Quote Originally Posted by Iamthejuggler View Post
    I may be misunderstanding you, but since when was having all the code in one place good practice in coding?! It seems to me to be the opposite of good practice and essentially going against the principles of OOP.

    Not that that's a problem with a project of this size, but if it gets any larger maintainability will suffer if everything is in one file.
    Well, by "in one place", i meant in one place within the movie...when people work in the Flash IDE, they tend to put their code in every nook-and-cranny they find. You will often see that, instead of the timeline, people are putting code in a mc, which is multiple levels down.And this is a problem in later part.

    You used the term "in one file", but while in flash, do we actually use "files" or as files for coding? keeping code in one place dosent mean just stacking it all together in a heap...if you would just brake your code into samller chunks like functions, and then keep it together, you can have very high "maintainability".

    When you do make multiple files for your program, you are actually just putting this chunks separately, nothing else....but when you code, it helps a lot if you have "what you coded earlier" infront of you. depending on how You keep code in a place, means the difference between a mess and a one-glimpse code.
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  11. #11
    I agree that code shouldn't be all over the flash file, but encapsulation (i.e. splitting the code into separate AS files) does make more complex programming a lot easier. Having all the code in front of you does make it easier to quickly check on things, but try and do that when the code starts breaking into the 1000+ lines and you really see the benefit of having classes as separate AS files. I guess it depends on the size of the project though. And by maintainability i mean if you have separate classes, properly set up, and you need to fix a bug on a certain class you can literally ignore the rest of the project and just edit one small and easy to read file. This makes bug-fixing and versioning loads easier. This becomes even more apparent when you need to work in a professional environment when there will almost always be more than one person working together on a project.

  12. #12
    yep you are right, but that kind of project in flash is pretty rare! but anyhow, i have started learning as3, and it seems, people will now learn to make manageable code in the timeline finally...(or maybe they will make more of a mess..who knows?)
    MY BLOG

    I need a SPRITER who can do pixel arts for an arcade fighter project. If you can help out, please pm me.
    I also need someone who can write simple xml files.


  13. #13
    Quote Originally Posted by bluemagica View Post
    yep you are right, but that kind of project in flash is pretty rare!
    Heh, i disagree. I'd say 90% of the games you see posted online are using OOP structures and methods.

    I'm working on a fairly simply platformer and already have 10+ classes and close to 1k lines of code.
    Last edited by substance; May 19th, 2008 at 12:37 PM.

  14. #14

    Okay I am uploading a version of the game. Let me mention again that this is simply the mechanics at their utmost basic form. In a different version I put a fail state in and start page and all that (but it was very plain and the game should be further along before that is done imo). I'm still building on it.

    I would really love to work with someone who is good with graphics to make a full out good top down shooter. Scope can be increased it would be really fun to work with someone on it. LMK!

    Enjoy!
    Attached Files Attached Files

  15. #15
    Sorry to bump this, but I was wondering if anyone had any ideas about making more of an abstract, art-type, top down shooter. Or any graphics tips. Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012