setDimension($w+150, $h+150); $movie->setBackground(133, 133, 133); $movie->setRate(12); //log is mytrace and game over $log_var = new SWFTextField(SWFTEXTFIELD_HTML | SWFTEXTFIELD_NOSELECT | SWFTEXTFIELD_NOEDIT); $f = new SWFFont("../MING/Serif.fdb"); $log_var->setName("log"); $log_var->setFont($f); $log_var->setHeight(8); $log_var->setColor(255, 255, 255); $log_var->setBounds(450, 450); $log = $movie->add($log_var); $log->moveTo(4, 5); $log->setDepth(100); //for keeping score $score_var = new SWFTextField(); $f = new SWFFont("../MING/Serif.fdb"); $score_var->setName("score"); $score_var->setFont($f); $score_var->setHeight(8); $score_var->setColor(255, 255, 255); $score_var->setBounds(450, 450); $score = $movie->add($score_var); $score->moveTo(0, $h+8); $score->setDepth(101); //the black border $boundary = new SWFShape(); $boundary->setLine(10, 0, 0, 0); $boundary->movePenTo(0, 0); $boundary->drawLine($w+$speed, 0); $boundary->drawLine(0, $h+$speed); $boundary->drawLine(-($w+$speed), 0); $boundary->drawLine(0, -($h+$speed)); $bounds = new SWFSprite(); $bounds->add($boundary); //if the snake hits the border - death ensues $bounds->add(new SWFAction(" this.onEnterFrame=function(){ if(_root.char._x <= 0 || (_root.char._x+_root.char._width) >= ".($w+$speed)." || _root.char._y <= 0 || (_root.char._y + _root.char._height) >= ".($h+$speed)."){ _root.die(_root.score_num); } };")); $bounds->nextFrame(); $b = $movie->add($bounds); $b->setDepth(4); $as = " //main script function mytrace(x) { _root.log += x + '\n'; }; //when the snake eats the food he grows function addPiece(){ var ob = _root.char; thex = _root['piece'+(ob.num-1)].x; they = _root['piece'+(ob.num-1)].y; the_parent = _root['piece'+(ob.num-1)]; _root.attachMovie('piece', ('piece'+ob.num), ob.num, {_x : thex, _y : they, parent: the_parent, count: ob.num}); ob.num++; }; //this is for moving each piece //it was hard to get each piece to follow the guy in front of him //this is what i came up with MovieClip.prototype.piece_move= function(){ this.x = this._x; this.y = this._y; this._x = this.parent.x; this._y = this.parent.y; diffx = this.x - this._x; diffy = this.y - this._y; if(diffx > 0 && diffy == 0)this.direction = 'left'; if(diffx < 0 && diffy == 0)this.direction = 'right'; if(diffx == 0 && diffy > 0)this.direction = 'up'; if(diffx == 0 && diffy < 0)this.direction = 'down'; }; //this is the move function for the main snake piece MovieClip.prototype.move= function(){ switch(this.direction){ case 'left': this._x-=char.speed; break; case 'right': this._x+=char.speed; break; case 'down': this._y+=char.speed; break; case 'up': this._y-=char.speed; break; }; }; //key listener function detectKeys() { var ob = _root.char; var keyPressed = false; if (Key.isDown(Key.RIGHT) && ob.direction!='left' && ob.direction != 'right') { ob.direction = 'right'; } else if (Key.isDown(Key.LEFT) && ob.direction!='right'&& ob.direction != 'left') { ob.direction = 'left'; } else if (Key.isDown(Key.UP) && ob.direction!='down'&& ob.direction != 'up') { ob.direction = 'up'; } else if (Key.isDown(Key.DOWN) && ob.direction!='up'&& ob.direction != 'down') { ob.direction = 'down'; } }; //each time the food gets eaten, replace it somewhere inside the boundary //this still isnt right - sometimes the snake gets generated on the line of the boundary function placeFood(){ var sf = _root.food; sf._visible = false; sf._x = Math.floor( (Math.random() * (".($w-10)."-sf._width))+(10+sf._width)); sf._y = Math.floor( (Math.random() * (".($h-10)."-sf._height))+(10+sf._height)); sf._visible = true; }; //onLoad function //called after spacebar is hit to reset as well function load(){ _root.attachMovie(\"char\", \"char\", 3); char.count=0; char.speed = ".$speed."; char.start = 5; char.num=char.start; char.x = 50; char.y = 50; char._x = char.x; char._y = char.y; char.direction = ''; // when the game starts - the snake has 2 pieces the char piece (main piece) and this first piece thex = char._x - char._width; they = char._y+10; the_parent = _root.char; _root.attachMovie(\"piece\", \"piece\"+char.num, char.num, {_x: thex, _y: they, direction: char.direction, parent: the_parent, count: char.num}); char.num++; _root.score_num = 0; _root.score = 'score: '+ _root.score_num + '\nhigh score: ' + _root.high_score; //place the food _root.attachMovie(\"food\", \"food\", 1); placeFood(); _root.death = false; log = ''; }; function die(sco){ if(_root.high_score < sco){ _root.high_score = sco; } _root.char._visible = false; _root.food._visible = false; for(i=char.start; i<=char.num; i++){ _root['piece'+i]._visible = false; } _root.log = '
death!\n\n\n\n\n\n\n\n\n\n\n\nhit space bar to try again
'; if (Key.isDown(Key.SPACE)) { _root.load(); } }; _root.onload = load(); _root.onEnterFrame = function(){ if(_root.death == false){ char.x = char._x; char.y = char._y; _root.detectKeys(); if(_root.char.direction.length > 0) char.move(); for(i=char.start; iadd($c); //this as is to test the snake doesn't run into itself $snake_piece->add(new SWFAction(" this.onenterframe=function(){ if(this.count > 6 && this._visible == true){ var ob = _root.char; if(this.hitTest(ob._x, ob._y)){ //hackage so you can get right up close to the last line if( ((this.direction == 'right' && ob.direction != 'left') || (this.direction == 'left' && ob.direction != 'right') || (this.direction == 'down' && ob.direction != 'up') || (this.direction == 'up' && ob.direction != 'down')) ){ _root.die(score_num); } } } };")); $snake_piece->nextFrame(); $snake = new SWFSprite(); $c1 = $snake->add($snake_piece); //if the snake eats food increment the score $snake->add( new SWFAction( " this.onEnterFrame=function (){ if(this.hitTest(_root.food)){ _root.score_num +=5; if(_root.high_score < _root.score_num) _root.high_score = _root.score_num; _root.score = 'score: '+ _root.score_num + '\nhigh score: '+_root.high_score; _root.placeFood(); _root.addPiece(); } };")); $snake->nextFrame(); $food = new SWFSprite(); $f = drawFilledSquare(0, 0, 5, 5, .1, 255, 0, 0); $f1 = $food->add($f); $food->nextFrame(); $movie -> addExport($snake, "char"); $movie -> addExport($food, "food"); $movie -> addExport($snake_piece, "piece"); $movie->add( new SWFAction($as) ); $movie->save("snake.swf"); echo "it worked"; ?>