Suffering the robots

by mutantleg
A half baked remake(sort of) of Robots http://en.wikipedia.org/wiki/Robots_%28computer_game%29

Controls: Arrow keys to move,
robots only move if you do
they get eliminated if they step on mines or collide each other
Press any other key to wait a turn
♥0 | Line 222 | Modified 2013-01-23 20:18:39 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2tUm
 */

package {
    import flash.text.TextField;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public var msg:TextField;
        public var wave:int = 1;
        public var cellx:int = 8;
        public var celly:int = 8;
        public var px:int = 16;
        public var py:int = 16;
        
        public var vecThing:Vector.<wThing>;
        
        public function FlashTest() {
            
            msg = new TextField();
            addChild(msg);
            msg.text = "Wave 1";
            
            restart();
            
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
            stage.addEventListener(KeyboardEvent.KEY_UP, kup);
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public function restart():void
        {
            graphics.clear();
            
            vecThing = new Vector.<wThing>(0,false);
            
            px = 24;
            py = 24;
            
            var i:int;
            var a:wThing;
            var rad:int;
            var ang:Number;
            for (i = 0; i < 48; i++)
            {
                rad = Math.random() * 48;
                rad += 6;
                ang = Math.random() * 6.28;
                
                a = new wThing(
                px+Math.cos(ang)*rad,
                 py+Math.sin(ang)*rad,
                  1+Math.random()*2);
                  
                  a.state = Math.random()*2;
                  
                  
                vecThing.push(a); 
                 
            }//nexti
            
            for (i = 0; i < maxBot; i++)
            {
                rad = Math.random() * 16;
                rad += 24;
                ang = Math.random() * 6.28;
                
                a = new wThing(
                px+Math.cos(ang)*rad,
                 py+Math.sin(ang)*rad,
                  0);
                  
                  a.state = Math.random()*2;
                  
                  
                vecThing.push(a); 
                
            }//nexti
            
            
        }//restart
        
        public var maxBot:int = 10;
        public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
        public var bMove:Boolean = false;
        public var gameState:int = 0;
        
        public function kup(e:KeyboardEvent):void
        {
            vecKey[e.keyCode] = false;
        }//kup
        
        public function kdown(e:KeyboardEvent):void
        {
            bMove = true;
            vecKey[e.keyCode] = true;
        }//kdown
        
        public var del:int = 0;
        
        public function onEnter(e:Event):void
        {
            
            
            
            graphics.clear();
            
            graphics.lineStyle(1,0);
            
            
            if (gameState == 1)
            {
               // graphics.drawRect(32,32, 400-64,400-64);
               // graphics.drawRect(64,64, 400-128,400-128);
                graphics.drawRect(225-del*4,225-del*4,del*8,del*8);
                graphics.drawRect(225-(60-del)*4,225-(60-del)*4,(60-del)*8,(60-del)*8);
               
                graphics.drawRect(225-del*2,225-del*2,del*4,del*4);
                graphics.drawRect(225-(60-del)*2,225-(60-del)*2,(60-del)*4,(60-del)*4);
               
               
                 del += 2;
                 if (del >= 60)
                 {
                     del = 0;
                     gameState = 0;
                     maxBot += 5;
                     if (maxBot >= 100) { maxBot = 100;}
                     else { wave += 1; }
                     msg.text = "Wave "+wave;
                     restart();
                     return;
                 }//endif2
             
             return;   
            }//endifstate
            
            //draw player
            graphics.drawCircle(px*cellx, py*celly, cellx);
            graphics.moveTo(px*cellx-cellx,py*celly-celly);
            graphics.lineTo(px*cellx+cellx, py*celly+celly);
            graphics.moveTo(px*cellx+cellx,py*celly-celly);
            graphics.lineTo(px*cellx-cellx, py*celly+celly);
            
            
            var i:int;
            var num:int;
            var a:wThing;
            var k:int;
            var b:wThing;
            
            num = vecThing.length;
            for (i = 0; i < num; i++)
            {
                a = vecThing[i];
                if (a.dead > 0) { continue; }
               
               //draw robot
               if (a.team == 0)
               {
                graphics.drawRect(a.cx*cellx-(cellx*0.5),a.cy*celly-(celly*0.5),cellx, celly);
               }
               else if (a.team == 1) //draw mine
               {
                 graphics.drawCircle(a.cx*cellx,a.cy*celly,cellx-4);
                 graphics.drawCircle(a.cx*cellx,a.cy*celly,cellx-1);  
               }
               else if (a.team == 2) //draw scrap
               {
                 graphics.drawCircle(a.cx*cellx,a.cy*celly, cellx-2);  
               }
                
            }//nexti
            
            
            if (gameState == 2)
            {
                del += 1;
                graphics.drawCircle(px*cellx, py*celly, del*4);
                graphics.drawCircle(px*cellx, py*celly, del*2);
                
                if (del >= 30)
                {
                 del = 0;
                 gameState = 0;
                 restart();   
                 }
             
             return;   
            }
            
            
            var numBot:int;
            numBot = 0;
            
                if (vecKey[Keyboard.UP]) { py -= 1; bMove =true;}
                else if (vecKey[Keyboard.DOWN]) { py += 1; bMove=true;}
                else if (vecKey[Keyboard.LEFT]) { px -= 1; bMove=true;}
                else if (vecKey[Keyboard.RIGHT]) { px += 1; bMove=true;}
            
            //update if player moved
            if (bMove)
            {
                bMove = false;
            
            /*    
                if (vecKey[Keyboard.UP]) { py -= 1;}
                else if (vecKey[Keyboard.DOWN]) { py += 1;}
                else if (vecKey[Keyboard.LEFT]) { px -= 1;}
                else if (vecKey[Keyboard.RIGHT]) { px += 1;}
 */
             
                num = vecThing.length;
                for (i = 0; i < num; i++)
                {
                    a = vecThing[i];
                    if (a.dead > 0) 
                    {
                        vecThing[i] = vecThing[num-1];
                        vecThing.pop();
                        i -= 1;
                        num = vecThing.length;
                        
                         continue; 
                    }
                    
                       
                        if (a.cx == px && a.cy == py)
                        {
                            gameState = 2;
                            // restart(); 
                          return; 
                        }
                
                    if (a.team == 0)
                    {
                        numBot += 1;
                
                        if (a.state == 0)
                        {
                        if (a.cy < py) { a.cy += 1; }
                        else if (a.cy >py) { a.cy -= 1;}
                        }
                        
                        if (a.state == 1)
                        {
                        if (a.cx < px) {a.cx +=1;}
                        else if (a.cx > px) { a.cx -= 1;}
                        }
                        
                        a.state = 1 - a.state;
                     
                        if (a.cx == px && a.cy == py)
                        {
                            gameState = 2;
                            // restart(); 
                          return; 
                        }
                    
                        for (k = 0; k < num; k++)
                        {
                            b = vecThing[k];
                            if (i == k) { continue; }
                            if (b.dead > 0) { continue; }
                            
                            if (a.cx == b.cx && a.cy == b.cy)
                            {
                              if (b.team == 0)
                              {
                                 a.team = 2;
                                 //a.dead = 1;
                                 b.dead = 1;
                              }else
                              { a.dead = 1;}
                              if (b.team == 1) {b.dead= 1;}
                             break;
                            }//endif
                        }//nextk
                    }//endifteam
                }//nexti        
                
                if (numBot <= 0) { gameState = 1; }
                
            }//endifmove
            
            bMove = false;
        }//onenter
        
    }//classend
}

internal class wThing
{
    public var cx:int = 0;
    public var cy:int = 0;
    public var team:int = 0;
    public var dead:int = 0;
    public var state:int = 0;
    
    public function wThing(x:int, y:int, t:int)
    {
     cx = x;
     cy = y;
     team = t;   
    }
    
}//wbot