• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Creating A Very Simple Snake Game Using jQuery

Alwen89

Anime Studio Historian
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
159
Likes
83
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
In this tutorial we will try to make a very simple game called Snake. Snake is the most popular games in the late 1997 that comes from the Nokia 6110 Phone. This game become a favorite past time for all of us a long time ago, even our grand parents played this awesome game. So for today we will try to replicate the game, but this time we will create it through HTML & jQuery Script. Let's get started. Before we proceed I hope that you already have the jQuery plugin, if there's not I already attached the jQuery plugin inside the zip file.

The Mark Up
The very first step is we're going to create the mark up. This page will display the game through the web browser. To create the file open any kind of text editor(notepad++, etc) then copy/paste the code that I provided below.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <title

    >

    Snake Game</

    title

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. <meta

    charset

    =

    "UTF-=8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  7. </

    head

    >
  8. <body

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "container-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  12. </

    div

    >
  13. </

    nav>
  14. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  15. <div

    class

    =

    "col-md-6 well"

    >
  16. <h3

    class

    =

    "text-primary"

    >

    Creating A Simple Snake Game</

    h3

    >
  17. <hr

    style

    =

    "bolder-top:1px dotted #000;"

    /

    >
  18. <center

    ><canvas id

    =

    "canvas"

    width

    =

    "450"

    height

    =

    "400"

    ></

    canvas</

    center

    >

    //this is where the game will be display
  19. </

    div

    >

  20. </

    body

    >
  21. <script

    src

    =

    "js/jquery-3.1.1.js"

    ></

    script

    >
  22. <script

    src

    =

    "js/snake.js"

    ></

    script

    >
  23. </

    html

    >

Save & name it 'index.html'

Creating the snake
The next step is we're going to create the snake, open your text editor and copy/paste this script.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. var

    canvas =

    $(

    '#canvas'

    )

    [

    0

    ]

    ;
  3. var

    context =

    canvas.getContext

    (

    '2d'

    )

    ;
  4. var

    width =

    $(

    '#canvas'

    )

    .width

    (

    )

    ;
  5. var

    height =

    $(

    '#canvas'

    )

    .height

    (

    )

    ;
  6. var

    cell_width =

    10

    ;
  7. var

    run;
  8. var

    snake_food;
  9. var

    score;
  10. var

    snake_array;

  11. function

    create_snake(

    )

    {
  12. var

    snake_size =

    3

    ;

    //the size of the snake is 3
  13. snake_array =

    [

    ]

    ;

    //creating the array of snake
  14. for

    (

    var

    m =

    0

    ;

    m <

    snake_size;

    m++

    )

    {
  15. snake_array.push

    (

    {

    x:

    45

    ,

    y:

    14

    }

    )

    ;

    //this will handle the position of the snake
  16. }
  17. }
  18. }

    )

    ;

Save this file and name it 'snake.js'

Creating the snake food
After creating the body of snake, we will now create the food of the snake. Just add this script inside the javascript code
  1. function

    create_food(

    )

    {
  2. snake_food =

    {
  3. x:

    Math

    .round

    (

    Math

    .random

    (

    )

    *

    (

    width -

    cell_width)

    /

    cell_width)

    ,

    //this will display the food randomly based on the x and y ordinates of the canvas
  4. y:

    Math

    .round

    (

    Math

    .random

    (

    )

    *

    (

    height -

    cell_width)

    /

    cell_width)
  5. }

    ;
  6. }

Creating the stage of the game
This will be the background of the game, just simply add this script to your javascript code.
  1. function

    stage_color(

    )

    {
  2. context.fillStyle

    =

    "orange"

    ;
  3. context.fillRect

    (

    0

    ,

    0

    ,

    width,

    height)

    ;
  4. context.strokeStyle

    =

    "#000000"

    ;
  5. context.strokeRect

    (

    0

    ,

    0

    ,

    width,

    height)

    ;
  6. }

Adding the sprite of the snake
This will be the sprite of the snake. Just simply add the script to your javascript code
  1. function

    snake_body(

    x,

    y)

    {
  2. context.fillStyle

    =

    "#ffffff"

    ;
  3. context.fillRect

    (

    x *

    cell_width,

    y *

    cell_width,

    cell_width,

    cell_width)

    ;
  4. context.strokeStyle

    =

    "#000000"

    ;
  5. context.strokeRect

    (

    x *

    cell_width,

    y *

    cell_width,

    cell_width,

    cell_width)

    ;
  6. }

Creating the collision of object to object
This will handle the collision of the snake when it hit its own body.
  1. function

    collision(

    x,

    y,

    array)

    {
  2. for

    (

    var

    i =

    0

    ;

    i <

    array.length

    ;

    i++

    )

    {
  3. if

    (

    array[

    i]

    .x

    ==

    x &&

    array[

    i]

    .y

    ==

    y)

    {

    //this will check if the snake hit its own body, and will return true
  4. return

    true

    ;
  5. }
  6. }
  7. return

    false

    ;

    //if not return false
  8. }

Creating the movement of snake
This will be the handler for the movement of the snake within the game.
  1. $(

    document)

    .keydown

    (

    function

    (

    e)

    {
  2. var

    key =

    e.which

    ;
  3. if

    (

    key ==

    "40"

    &&

    run !=

    "up"

    )

    {

    //check if the key you pressed is arrow down
  4. run =

    "down"

    ;

    //return the string down
  5. }
  6. else

    if

    (

    key ==

    "39"

    &&

    run !=

    "left"

    )

    {

    //check if the key you pressed is arrow right
  7. run =

    "right"

    ;

    //return the string right
  8. }
  9. else

    if

    (

    key ==

    "38"

    &&

    run !=

    "down"

    )

    {

    //check if the key you pressed is arrow up
  10. run =

    "up"

    ;

    //return the string up
  11. }
  12. else

    if

    (

    key ==

    "37"

    &&

    run !=

    "right"

    )

    {

    //check if the key you pressed is arrow left
  13. run =

    "left"

    ;

    return

    the string left
  14. }
  15. }

    )

    ;

Creating the config of the game
This will handle the logic of the game on how the food will be eaten by the snake, and then make its body increase.
  1. function

    config(

    )

    {
  2. stage_color(

    )

    ;

    //display the stage of the game

  3. var

    pop_x =

    snake_array[

    0

    ]

    .x

    ;

    //assigned the x ordinate of the canvas
  4. var

    pop_y =

    snake_array[

    0

    ]

    .y

    ;

    // assigned the y ordinate of the canvas

  5. switch

    (

    run)

    {

    //check the variable run
  6. case

    "right"

    :

    //if run == right
  7. pop_x++;

    //the movement of the snake will be right
  8. break

    ;
  9. case

    "left"

    :

    //if run == left
  10. pop_x--;

    //the movement of the snake will be left
  11. break

    ;
  12. case

    "down"

    :

    //if run == down
  13. pop_y++;

    //the movement of the snake will be down
  14. break

    ;
  15. case

    "up"

    :

    //if run == up
  16. pop_y--;

    //the movement of the snake will be up
  17. break

    ;
  18. }

  19. if

    (

    pop_x ==

    -

    1

    ||

    pop_x ==

    width /

    cell_width ||

    pop_y ==

    -

    1

    ||

    pop_y ==

    height /

    cell_width ||

    collision(

    pop_x,

    pop_y,

    snake_array)

    )

    {

    //check if the snake collide within the boundary of the canvas or the snake collide with its own body
  20. start(

    )

    ;

    //if true the game will restart
  21. return

    ;
  22. }

  23. if

    (

    pop_x ==

    snake_food.x

    &&

    pop_y ==

    snake_food.y

    )

    {

    //check if the snake collide with the food
  24. var

    snake_tail =

    {

    x:

    pop_x,

    y:

    pop_y}

    ;

    //if true assign the variable for the snake tail
  25. score +=

    3

    ;

    //add a score
  26. create_food(

    )

    ;

    //create another food randomly
  27. }

    else

    {
  28. var

    snake_tail =

    snake_array.pop

    (

    )

    ;

    /

    if

    false

    will create the variable for

    the snake tail
  29. snake_tail.x

    =

    pop_x;

    //assign variable for the x ordinate of snake
  30. snake_tail.y

    =

    pop_y;

    //assign variable for the y ordinate of snake
  31. }

  32. snake_array.unshift

    (

    snake_tail)

    ;

    //if the condition is true it will add the value of the food inside the snake_array

  33. for

    (

    var

    i =

    0

    ;

    i <

    snake_array.length

    ;

    i++

    )

    {

    //if there's a new valued inside the snake_array it will increment
  34. var

    c =

    snake_array[

    i]

    ;

    //assigned the incremented valued
  35. snake_body(

    c.x

    ,

    c.y

    )

    ;

    //then will increase the size of the snake
  36. }

  37. snake_body(

    snake_food.x

    ,

    snake_food.y

    )

    ;

    //increase the snake body in the correct position
  38. var

    score_text =

    "Score: "

    +

    score;

    //assign the variable of score
  39. context.fillText

    (

    score_text,

    5

    ,

    10

    )

    ;

    //display the score
  40. }

Creating the configuration of the game
This will handle the initialize of the game
  1. start(

    )

    ;

    //this will start running the game

  2. function

    start(

    )

    {
  3. run =

    "left"

    ;

    // this is the default movement of the snake
  4. create_snake(

    )

    ;

    //will create the food
  5. create_food(

    )

    ;

    //will create the snake
  6. score =

    0

    ;

    //display the default score

  7. if

    (

    typeof

    game_start !=

    "undefined"

    )

    clearInterval(

    game_start)

    ;

    //check if the loop of the game is undefined and initialize the game simultaneously
  8. game_start =

    setInterval(

    config,

    60

    )

    ;

    //this the fps of the game when running
  9. }

This is the full code of the snake.js
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. var

    canvas =

    $(

    '#canvas'

    )

    [

    0

    ]

    ;
  3. var

    context =

    canvas.getContext

    (

    '2d'

    )

    ;
  4. var

    width =

    $(

    '#canvas'

    )

    .width

    (

    )

    ;
  5. var

    height =

    $(

    '#canvas'

    )

    .height

    (

    )

    ;
  6. var

    cell_width =

    10

    ;
  7. var

    run;
  8. var

    snake_food;
  9. var

    score;
  10. var

    snake_array;



  11. function

    create_snake(

    )

    {
  12. var

    snake_size =

    3

    ;
  13. snake_array =

    [

    ]

    ;
  14. for

    (

    var

    m =

    0

    ;

    m <

    snake_size;

    m++

    )

    {
  15. snake_array.push

    (

    {

    x:

    45

    ,

    y:

    14

    }

    )

    ;
  16. }
  17. }

  18. function

    create_food(

    )

    {
  19. snake_food =

    {
  20. x:

    Math

    .round

    (

    Math

    .random

    (

    )

    *

    (

    width -

    cell_width)

    /

    cell_width)

    ,
  21. y:

    Math

    .round

    (

    Math

    .random

    (

    )

    *

    (

    height -

    cell_width)

    /

    cell_width)
  22. }

    ;
  23. }

  24. function

    config(

    )

    {

  25. stage_color(

    )

    ;

  26. var

    pop_x =

    snake_array[

    0

    ]

    .x

    ;
  27. var

    pop_y =

    snake_array[

    0

    ]

    .y

    ;

  28. switch

    (

    run)

    {
  29. case

    "right"

    :
  30. pop_x++;
  31. break

    ;
  32. case

    "left"

    :
  33. pop_x--;
  34. break

    ;
  35. case

    "down"

    :
  36. pop_y++;
  37. break

    ;
  38. case

    "up"

    :
  39. pop_y--;
  40. break

    ;
  41. }


  42. if

    (

    pop_x ==

    -

    1

    ||

    pop_x ==

    width /

    cell_width ||

    pop_y ==

    -

    1

    ||

    pop_y ==

    height /

    cell_width ||

    collision(

    pop_x,

    pop_y,

    snake_array)

    )

    {
  43. start(

    )

    ;
  44. return

    ;
  45. }

  46. if

    (

    pop_x ==

    snake_food.x

    &&

    pop_y ==

    snake_food.y

    )

    {
  47. var

    snake_tail =

    {

    x:

    pop_x,

    y:

    pop_y}

    ;
  48. score +=

    3

    ;
  49. create_food(

    )

    ;
  50. }

    else

    {
  51. var

    snake_tail =

    snake_array.pop

    (

    )

    ;
  52. snake_tail.x

    =

    pop_x;
  53. snake_tail.y

    =

    pop_y;

  54. }

  55. snake_array.unshift

    (

    snake_tail)

    ;

  56. for

    (

    var

    i =

    0

    ;

    i <

    snake_array.length

    ;

    i++

    )

    {
  57. var

    c =

    snake_array[

    i]

    ;
  58. snake_body(

    c.x

    ,

    c.y

    )

    ;
  59. }

  60. snake_body(

    snake_food.x

    ,

    snake_food.y

    )

    ;

  61. var

    score_text =

    "Score: "

    +

    score;
  62. var

    title =

    "Sourcecodester"
  63. context.fillText

    (

    score_text,

    5

    ,

    10

    )

    ;
  64. context.fillText

    (

    title,

    0

    ,

    height-

    2

    )

    ;
  65. }

  66. function

    stage_color(

    )

    {
  67. context.fillStyle

    =

    "orange"

    ;
  68. context.fillRect

    (

    0

    ,

    0

    ,

    width,

    height)

    ;
  69. context.strokeStyle

    =

    "#000000"

    ;
  70. context.strokeRect

    (

    0

    ,

    0

    ,

    width,

    height)

    ;

  71. }

  72. function

    snake_body(

    x,

    y)

    {
  73. context.fillStyle

    =

    "#ffffff"

    ;
  74. context.fillRect

    (

    x *

    cell_width,

    y *

    cell_width,

    cell_width,

    cell_width)

    ;
  75. context.strokeStyle

    =

    "#000000"

    ;
  76. context.strokeRect

    (

    x *

    cell_width,

    y *

    cell_width,

    cell_width,

    cell_width)

    ;
  77. }

  78. function

    collision(

    x,

    y,

    array)

    {
  79. for

    (

    var

    i =

    0

    ;

    i <

    array.length

    ;

    i++

    )

    {
  80. if

    (

    array[

    i]

    .x

    ==

    x &&

    array[

    i]

    .y

    ==

    y)

    {
  81. return

    true

    ;
  82. }
  83. }
  84. return

    false

    ;
  85. }

  86. $(

    document)

    .keydown

    (

    function

    (

    e)

    {
  87. var

    key =

    e.which

    ;
  88. if

    (

    key ==

    "40"

    &&

    run !=

    "up"

    )

    {
  89. run =

    "down"

    ;
  90. }
  91. else

    if

    (

    key ==

    "39"

    &&

    run !=

    "left"

    )

    {
  92. run =

    "right"

    ;
  93. }
  94. else

    if

    (

    key ==

    "38"

    &&

    run !=

    "down"

    )

    {
  95. run =

    "up"

    ;
  96. }
  97. else

    if

    (

    key ==

    "37"

    &&

    run !=

    "right"

    )

    {
  98. run =

    "left"

    ;
  99. }
  100. }

    )

    ;

  101. start(

    )

    ;

  102. function

    start(

    )

    {
  103. run =

    "left"

    ;
  104. create_snake(

    )

    ;
  105. create_food(

    )

    ;
  106. score =

    0

    ;

  107. if

    (

    typeof

    game_start !=

    "undefined"

    )

    clearInterval(

    game_start)

    ;
  108. game_start =

    setInterval(

    config,

    60

    )

    ;
  109. }
  110. }

    )

    ;

There you have it we create a simple snake game. I hope that this tutorial help you to your programming carrier, and enhance your skill on looping. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,512

356,407

356,429

Top
Raidforums