A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Snake

  1. #1
    Razor
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    721

    Snake

    I've always wondered how to make a snake game, but I never tried so this morning, seeing as I could either study for a chemistry exam I have tomorrow or try to make my first MX 2004 flash game, considering I got the trial version yesterday. Naturally I chose the game, heres the code(Game link at bottom) I just wanna know what you guys think of my coding rather then the game because this really only the second or third time I've ever used any Object Oriented type stuff and I want to know how I can improve:
    File(Snake.as):
    Code:
    class Snake extends MovieClip{
    	
    	private var snkinst:Number;
    	private var xv,yv,cspeed,ym,xm:Number;
    	public function Snake(passedinst:Number){
    		snkinst = passedinst;
    		cspeed = 10;
    	}
    	
    	public function moveNext(){
    		if(snkinst == 0){
    			_root["s"+snkinst].px = _root["s"+snkinst]._x;
    			_root["s"+snkinst].py = _root["s"+snkinst]._y;
    			_root["s"+snkinst]._x += cspeed*xv;
    			_root["s"+snkinst]._y += cspeed*yv;
     		}else{
    			xm = _root["s"+(snkinst-1)].px;
    			ym = _root["s"+(snkinst-1)].py;
    			_root["s"+snkinst].px = _root["s"+snkinst]._x;
    			_root["s"+snkinst].py = _root["s"+snkinst]._y;
    			_root["s"+snkinst]._x = xm;
    			_root["s"+snkinst]._y = ym;
    		}
    	}
    	
    	public function keyCheck(){
    		if(Key.isDown(Key.UP) && yv != 1){
    			yv = -1;
    			xv = 0;
    		}
    		if(Key.isDown(Key.LEFT) && xv != 1){
    			xv = -1;
    			yv = 0;
    		}
    		if(Key.isDown(Key.RIGHT) && xv != -1){
    			xv = 1;
    			yv = 0;
    		}
    		if(Key.isDown(Key.DOWN) && yv != -1){
    			yv = 1;
    			xv = 0;
    		}
    	}
    }
    First Frame of Flash:
    Code:
    var mySnake:Snake = new Snake(0);
    var csc:Number = 1;
    var pscore:Number = 0;
    _root.onLoad = function(){
    	_root.ball._x = random(490);
    	_root.ball._y = random(490);
    }
    _root.onEnterFrame = function(){
    	if(_root.s0._x > 500 or _root.s0._x < 0 or _root.s0._y > 500 or _root.s0._y < 0){
    		for(r=0;r<=csc;r++){
    			_root["s"+r].removeMovieClip();
    		}
    		gotoAndStop(2);
    	}
    	for(r=3;r<=csc;r++){
    		if(_root["s"+r].hitTest(_root.s0)){
    			for(q=0;q<=csc;q++){
    				_root["s"+q].removeMovieClip();
    			}
    			gotoAndStop(2);
    		}
    	}
    	if(_root.s0.hitTest(_root.ball)){
    		_root.ball._x = random(490);
    		_root.ball._y = random(490);
    		_root.attachMovie("snake","s"+csc,csc+130,"snake");
    		_root["s"+csc]._x = _root["s"+csc-1]._x;
    		_root["s"+csc]._y = _root["s"+csc-1]._y + 10; 
    		set("sn"+csc, new Snake(csc));
    		pscore++;
    		csc++;
    	}
    	mySnake.keyCheck();
    	mySnake.moveNext();
    	tscore = "Score: " + pscore;
    	for(i=1;i<=csc;i++){
    		_root["sn"+i].moveNext();
    	}
    }
    stop();
    The other frame is just the gameover one no real code in there so I left it out. You can play the flash here: My First Snake Game (Start the game and play it using any of the arrow keys)
    Last edited by chuckury; 06-19-2005 at 02:13 PM.

  2. #2
    Senior Member chriserrorplain's Avatar
    Join Date
    Aug 2002
    Location
    london village
    Posts
    623
    seems great to me, no bug to report and all the basics seem to be there and working.

    Chris

    [edit] sorry didnt read the bit about commenting the code not the game..ummm dont really have time to look through it all properly now, but it seems well structured [/edit]

  3. #3
    the usual
    Join Date
    Jul 2000
    Posts
    1,482
    I really don't see the point of the Snake class, it doesn't look like oop to me; it looks like you've just created the class for the sake of it; the only function that is of any use to it is moveNext - but this could quite simply be placed in the _root and you shouldn't be using _root["s"+snkinst]_x/_y; why not just use _x

    you don't really need the _root.onLoad function: _root.ball._x = random(490);_root.ball._y = random(490); will do

    to neaten the attaching code up you can use:

    var l = _root["s"+csc-1]
    _root.attachMovie("snake","s"+csc,csc+130,"snake", { _x : l._x, _y : l._y + 10 });

    when you extend the movieclip the class is initialised on the attachmovie command, the way you're using it, it doesn't look like it should be extending anythung

    hope that helps (and made some sort of sense)

  4. #4
    Razor
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    721
    Chris: Thanks for looking at it
    Trickman: Thanks, it all made sense until the last part, I dont really get what you mean by it shouldnt be extending anything just cause I use attachMovie, but the part that confuses me is that what with your saying it shouldnt work but it does. Thanks for looking through my code and the tips.

  5. #5
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    Worked relatively well. Some keypresses are not always captured though. Try turning twice very quickly. Sometimes only the last turn is performed.

    I would recommend using Key.getCode() rather than Key.isDown(). Also, implementing a queue for the key-presses will make sure all turns are performed. It could work like this: Push all keypresses to the end of an array and then pick (and remove) the keypress at the front of the array (if any) each frame and perform that. I have written a small tutorial that uses this method: http://www.strille.net/tutorials/snake/

  6. #6
    Razor
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    721
    Quote Originally Posted by strille
    Worked relatively well. Some keypresses are not always captured though. Try turning twice very quickly. Sometimes only the last turn is performed.

    I would recommend using Key.getCode() rather than Key.isDown(). Also, implementing a queue for the key-presses will make sure all turns are performed. It could work like this: Push all keypresses to the end of an array and then pick (and remove) the keypress at the front of the array (if any) each frame and perform that. I have written a small tutorial that uses this method: http://www.strille.net/tutorials/snake/
    I noticed that and I never thought of doing that I have something else new to try thanks strille.

  7. #7
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    I would suggest having the ball that appears as a tile, not a random location in the whole game..(Right now you can hit the same ball from normally 4 places, in my opinion it would be better if it was just 1).

    Other than that, looks good!

  8. #8
    Razor
    Join Date
    Aug 2002
    Location
    Canada
    Posts
    721
    I took everything you guys said and improved the game:
    -Unknown guy made it more tile based so you can only touch the object from one route
    -Have the keypresses in an array that I push and shift
    -Trickman the code u gave me to optimize with didn't work.

    Snake.as:
    Code:
    class Snake extends MovieClip{
    	private var keyPresses:Array = new Array();
    	private var snkinst:Number;
    	private var xv,yv,cspeed,ym,xm:Number;
    	private var ck:Object;
    	
    	public function Snake(passedinst:Number){
    		snkinst = passedinst;
    		cspeed = 10;
    	}
    	
    	public function moveNext(){
    		if(snkinst == 0){
    			_root["s"+snkinst].px = _root["s"+snkinst]._x;
    			_root["s"+snkinst].py = _root["s"+snkinst]._y;
    			_root["s"+snkinst]._x += cspeed*xv;
    			_root["s"+snkinst]._y += cspeed*yv;
     		}else{
    			xm = _root["s"+(snkinst-1)].px;
    			ym = _root["s"+(snkinst-1)].py;
    			_root["s"+snkinst].px = _root["s"+snkinst]._x;
    			_root["s"+snkinst].py = _root["s"+snkinst]._y;
    			_root["s"+snkinst]._x = xm;
    			_root["s"+snkinst]._y = ym;
    		}
    	}
    	
    	public function keyCheck(){
    		ck = keyPresses.shift();
    		if(ck == "up"){
    			yv = -1;
    			xv = 0;
    		}else if(ck == "down"){
    			yv = 1;
    			xv = 0;
    		}else if(ck == "left"){
    			yv = 0;
    			xv = -1;
    		}else if(ck == "right"){
    			yv = 0;
    			xv = 1;
    		}
    		if(Key.getCode() == Key.UP && yv != 1){
    			keyPresses.push("up");
    		}
    		if(Key.getCode() == Key.LEFT && xv != 1){
    			keyPresses.push("left");
    		}
    		if(Key.getCode() == Key.RIGHT && xv != -1){
    			keyPresses.push("right");
    		}
    		if(Key.getCode() == Key.DOWN && yv != -1){
    			keyPresses.push("down");
    		}
    	}
    }
    First Frame:
    Code:
    var mySnake:Snake = new Snake(0);
    var csc:Number = 1;
    var pscore:Number = 0;
    var bx,by:Number;
    
    bx = Math.ceil(random(49)*10);
    by = Math.ceil(random(49)*10);
    _root.ball._x = bx;
    _root.ball._y = by;
    
    _root.onEnterFrame = function(){
    	if(_root.s0._x > 500 or _root.s0._x < 0 or _root.s0._y > 500 or _root.s0._y < 0){
    		for(r=0;r<=csc;r++){
    			_root["s"+r].removeMovieClip();
    		}
    		gotoAndStop(2);
    	}
    	for(r=3;r<=csc;r++){
    		if(_root["s"+r].hitTest(_root.s0._x,_root.s0._y,true)){
    			for(q=0;q<=csc;q++){
    				_root["s"+q].removeMovieClip();
    			}
    			gotoAndStop(2);
    		}
    	}
    	if(_root.ball.hitTest(_root.s0._x,_root.s0._y,true)){
    		for(r=1;r<=csc;r++){
    			do{
    				bx = Math.ceil(random(49)*10);
    				by = Math.ceil(random(49)*10);
    				_root.ball._x = bx;
    				_root.ball._y = by;
    			}while(_root["s"+r].hitTest(_root.ball))
    		}
    		_root.attachMovie("snake","s"+csc,csc+130,"snake");
    		_root["s"+csc]._x = _root["s"+csc-1]._x;
    		_root["s"+csc]._y = _root["s"+csc-1]._y + 10; 
    		set("sn"+csc, new Snake(csc));
    		pscore++;
    		csc++;
    	}
    	mySnake.keyCheck();
    	mySnake.moveNext();
    	tscore = "Score: " + pscore;
    	for(i=1;i<=csc;i++){
    		_root["sn"+i].moveNext();
    	}
    }
    stop();
    Link: http://img198.echo.cx/my.php?image=main8xw.swf

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center