2 Replies - 1512 Views - Last Post: 10 February 2011 - 10:29 AM

#1 Broly  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 16-December 04

Snake

Posted 10 April 2005 - 01:54 PM

Description: It's simple : paste the code in your movie and call the "snake()" function.
If you want, you can customize some aspect of the game following the istructions in the first lines.This snippet create a complete "Snake" game in a Flash movie
All the game is made in
actionscript, so it is quite light (2-3 kb)
and easily customizable
You can customize almost all the game :)

You can see an example of the result at the URL http://www.brolyweb....nippetBeta.html
// SNAKE snippet
// Author : Broly
// Contact : [email protected]
// Made in April 2005 for the "forums.dreamincode.net" contest this.spring('05')

//********* DESCRIPTION ******** //
// This snippet create a complete 
// "Snake" game in a Flash movie
// All the game is made in
// actionscript, so it is quite light
// and easily customizable 
// You can customize almost all the game :)
//***************************** //

// General parameteres
speed = 1 // Snake speed factor
levels = 5 // How many levels the game'll have
level_needed_points = [4,9,15,22,30] // How many points the player must have to pass the level
diffs = [2,3,5,7,8] // Set how many parts will be added to the snake when he eats a fruit
score_inc=1 // How many points the player obtain when the snake eta a fruit

// Set the style for the game over text.
// You can add more options, refers to TextFormat in flash help
// to see what are the available options
over_text = "Sorry, game over.nPress a key to restart"
over_style = new TextFormat()
over_style.align = "center"
over_style.bold = true
over_style.color = 0x000099
over_style.size = 32

// Set the style for the score text
// As for the game over text, you can add more options
score_style = new TextFormat()
score_style.bold = true
score_style.font = "Verdana"
score_style.color = 0xFFFFFF

// Set the style for the score text
// As for the game over text, you can add more options
level_style = new TextFormat()
level_style.bold = true
level_style.font = "Verdana"
level_style.color = 0xFFFFFF

// Set the style for the "level end" text
// As for the game over text, you can add more options
levelend_text = "You passed the level.nPress any key to start next level"
levelend_style = new TextFormat()
levelend_style.bold = true
levelend_style.font = "Verdana"
levelend_style.color = 0x000099
levelend_style.align = "center"
levelend_style.size = 20

// Graphic parameters
border = 1
bordercolor = 0x000000
borderalpha = 100
player_border = 1
player_bordercolor = 0x000000
player_borderalpha = 100
player_color = 0x000099
player_alpha = 80
player_size = 10
fruit_color = 0xFF0000
panel_color = 0x333333
panel_alpha = 60

// The game functions, stop modify
function Snake(){ 
x=0
function initLevel(level_n){
     lastpressed = null
     Key.removeListener(lv)
    lvend.removeTextField()
     over.removeTextField()
     started = false
     delete player.onEnterFrame
     player.removeMovieClip()
     fruit.removeMovieClip()
     path = []
     x = 0
     for(mc in _root){
          if(_root[mc]._name.substr(0,1) == "d"){
               removeMovieClip(_root[mc])
          }
     }
     diff = diffs[level_n-1]
     pnts = level_needed_points[level_n-1]
     level_val = level_n
     level.text = "Level : " + level_val
     level.setTextFormat(level_style)
     level._x = 20
    level._y = sth - level._height - 10
     createPlayer()
     createFruit()
}
function restart(){
     score_val = 0
     score.text = "Score : " + score_val
     score.setTextFormat(score_style)
     initLevel(1)
}
function gameOver(){
     Key.removeListener(key_mov)
     lv = new Object()
     lv.onkeydown = function(){
          pr = Key.getCode()-37
          if(Math.abs(pr)>3){
          restart()
          }
     }
     Key.addListener(lv)
     delete player.onEnterFrame
     createTextField('over',100,0,0,0,0)
     over.autoSize = true
     over.selectable = false
     over.text = over_text
     over.setTextFormat(over_style)
     over._x = sw/2 - over._width/2
     over._y = sh/2 - over._height/2
}
function levelEnd(){
     Key.removeListener(key_mov)
     createTextField('lvend',100,0,0,0,0)
     lv = new Object()
     lvend.autoSize = true
     lvend.selectable = false
     if(level_val == levels){
     lvend.text = "CONGRATULATIONS! YOU FINISHED THE GAME!nPress a key to restart"
     lv.onkeydown = function(){
          pr = Key.getCode()-37
          if(Math.abs(pr)>3){
          restart()
          }
     }
     }else{
     lv.onkeydown = function(){
          pr = Key.getCode()-37
          if(Math.abs(pr)>3){
          level_val++
          initLevel(level_val)
          }
     }
     lvend.text = levelend_text
     }
     Key.addListener(lv)
     delete player.onEnterFrame
     lvend.setTextFormat(levelend_style)
     lvend._x = sw/2 - lvend._width/2
     lvend._y = sh/2 - lvend._height/2
}

function createFruit(){ // Fruit creation
player.duplicateMovieClip('fruit',-6)
   col = new Color(fruit)
   col.setRGB(fruit_color)
   possX = sw/(player_size*speed)-1
   possY = sh/(player_size*speed)-1
   fruit._x = Math.round(Math.random()*possX)*(player_size*speed)
   fruit._y = Math.round(Math.random()*possY)*(player_size*speed)
   for(f=-1;f<x;f++){
          if(fruit._x == eval('d'+f)._x && fruit._y == eval('d'+f)._y){
               fruit.removeMovieClip()
               createFruit()
          }
          }
}
// Stage creation
// We create two borders (up and left), then duplicate them
// to made the down and right borders. 
sw = Stage.width
sh = Stage.height-40
sth = Stage.height
createEmptyMovieClip('borderup',-1)
createEmptyMovieClip('bordersx',-2)
borderup.lineStyle(border,bordercolor,borderalpha)
bordersx.lineStyle(border,bordercolor,borderalpha)
borderup.lineTo(sw,0)
bordersx.lineTo(0,sh)
borderup.duplicateMovieClip('borderdown',-3)
borderdown._y = sh
bordersx.duplicateMovieClip('borderdx',-4)
borderdx._x = sw-1
// Create the info panel, with score, level and more info
createEmptyMovieClip('panel',-10)
panel._y = sh
panel.lineStyle(border,bordercolor,borderalpha)
panel.beginFill(panel_color,panel_alpha)
panel.lineTo(sw,0)
panel.lineTo(sw,40)
panel.lineTo(0,40)
panel.lineTo(0,0)
panel.endFill()
createTextField('score',-5,0,0,30,20)
score.selectable = false
score.autoSize = true
score_val = 0
score.text = "Score : " + score_val
score.setTextFormat(score_style)
score._x = sw - score._width - 30
score._y = Stage.height - score._height - 10
createTextField('level',-7,0,0,30,20)
level.autoSize = true
level.selectable = false
// Create the player
function createPlayer(){
createEmptyMovieClip('player',1)
player.lineStyle(player_border,player_bordercolor,player_borderalpha)
player.beginFill(player_color,player_alpha)
player.lineTo(player_size,0)
player.lineTo(player_size,player_size)
player.lineTo(0,player_size)
player.lineTo(0,0)
player.endFill()
Key.addListener(key_mov)
player.onEnterFrame = function(){
     if(started){
     path.unshift([player._x + incrX,player._y + incrY])
     this._x = path[0][0]
     this._y = path[0][1]
     if(this._x<0 || this._x >= sw || this._y<0 || this._y>=sh){
          gameOver()
     }
     for(l=-1;l<x;l++){
          if(this._x == eval('d'+l)._x && this._y == eval('d'+l)._y && l>0){
               gameOver()
               Key.removeListener(key_mov)
          }
     }
     if(this._x == fruit._x && this._y == fruit._y){
     createFruit()
     score_val+=score_inc
     score.text = "Score : " + score_val
     score.setTextFormat(score_style)
     for(n=0;n<diff;n++){
     player.duplicateMovieClip('d'+x,x+2)
     eval('d'+x).n = x
     eval('d'+x).onEnterFrame = function(){
          this._x = path[this.n][0]
          this._y = path[this.n][1]
     }
      x++
      }
      if(score_val == pnts){
          delete this.onEnterFrame
          levelEnd()
          
     }
     }
     }
}
}
// Create the listener that move the snake
key_mov = new Object()
key_mov.onkeydown = function(){
     pressed = Key.getCode()
     if(pressed != lastpressed-2 && pressed != lastpressed+2){
          incrX = incrY = 0
          pressed == 37 ? incrX = - player_size*speed : pressed == 38 ? incrY = - player_size*speed : pressed == 39 ? incrX = + player_size*speed : pressed == 40 ? incrY = + player_size*speed : null
          lastpressed = pressed
          path.unshift([player._x + incrX,player._y + incrY])
          path.splice(x)
          }
     started = true
}

initLevel(1)
}

// Call the function that create the game
Snake()


Is This A Good Question/Topic? 0
  • +

Replies To: Snake

#2 adam.dod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 30-May 09

Re: Snake

Posted 01 June 2009 - 06:11 AM

TY MAN!
Was This Post Helpful? 0
  • +
  • -

#3 afshan_09_09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 10-February 11

Re: Snake

Posted 10 February 2011 - 10:29 AM

can you explain 159-166 lines # possX = sw/(player_size*speed)-1 # possY = sh/(player_size*speed)-1 # fruit._x = Math.round(Math.random()*possX)*(player_size*speed) # fruit._y = Math.round(Math.random()*possY)*(player_size*speed) # for(f=-1;f
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1