• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Snake Game using HTML5 canvas and Pure Javascript

RamadhanHafiz

RAT Controller
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
73
Likes
143
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

It is important that the browser you use to play this game is accepting HTML5 canvas since where going to use this as the container of our game.

The original source code of this tutorial can be found using the youtube link below which I modify by adding score and highscore.

https://www.youtube.com/results?search_query=snake+game+using+angular+js
Full Source Code

Here's the full code for this simple snake game.

  1. <!

    DOCTYPE html>
  2. <

    html>
  3. <

    head>
  4. <

    title>

    Simple Snake Game</

    title>
  5. </

    head>
  6. <

    body>
  7. <

    p>

    Score:

    <

    span id=

    "score"

    >

    0

    </

    span></

    p>
  8. <

    p>

    High Score <

    span id=

    "highscore"

    >

    0

    </

    span></

    p>
  9. <

    canvas id=

    "gc"

    width=

    "400"

    height=

    "400"

    ></

    canvas>
  10. <

    script>
  11. window.onload

    =

    function

    (

    )

    {
  12. canv=

    document.getElementById

    (

    "gc"

    )

    ;
  13. ctx=

    canv.getContext

    (

    "2d"

    )

    ;
  14. document.addEventListener

    (

    "keydown"

    ,

    keyPush)

    ;
  15. setInterval(

    game,

    1000

    /

    15

    )

    ;
  16. scoreShow=

    document.getElementById

    (

    'score'

    )

    ;
  17. highscoreShow=

    document.getElementById

    (

    'highscore'

    )

    ;
  18. }
  19. px=

    py=

    10

    ;
  20. gs=

    tc=

    20

    ;
  21. ax=

    ay=

    15

    ;
  22. xv=

    yv=

    0

    ;
  23. trail=

    [

    ]

    ;
  24. tail =

    5

    ;
  25. score =

    0

    ;
  26. highscore =

    0

    ;
  27. function

    game(

    )

    {
  28. px+=

    xv;
  29. py+=

    yv;
  30. if

    (

    px<

    0

    )

    {
  31. px=

    tc-

    1

    ;
  32. }
  33. if

    (

    px>

    tc-

    1

    )

    {
  34. px=

    0

    ;
  35. }
  36. if

    (

    py<

    0

    )

    {
  37. py=

    tc-

    1

    ;
  38. }
  39. if

    (

    py>

    tc-

    1

    )

    {
  40. py=

    0

    ;
  41. }
  42. ctx.fillStyle

    =

    "black"

    ;
  43. ctx.fillRect

    (

    0

    ,

    0

    ,

    canv.width

    ,

    canv.height

    )

    ;

  44. ctx.fillStyle

    =

    "lime"

    ;
  45. for

    (

    var

    i=

    0

    ;

    i<

    trail.length

    ;

    i++

    )

    {
  46. ctx.fillRect

    (

    trail[

    i]

    .x

    *

    gs,

    trail[

    i]

    .y

    *

    gs,

    gs-

    2

    ,

    gs-

    2

    )

    ;
  47. if

    (

    trail[

    i]

    .x

    ==

    px &&

    trail[

    i]

    .y

    ==

    py)

    {
  48. gameOver(

    )

    ;
  49. }
  50. }
  51. trail.push

    (

    {

    x:

    px,

    y:

    py}

    )

    ;
  52. while(

    trail.length

    >

    tail)

    {
  53. trail.shift

    (

    )

    ;
  54. }

  55. if

    (

    ax==

    px &&

    ay==

    py)

    {
  56. score =

    score +

    10

    ;
  57. scoreShow.innerHTML

    =

    score;
  58. tail++;
  59. ax=

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    tc)

    ;
  60. ay=

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    tc)

    ;
  61. }
  62. ctx.fillStyle

    =

    "red"

    ;
  63. ctx.fillRect

    (

    ax*

    gs,

    ay*

    gs,

    gs-

    2

    ,

    gs-

    2

    )

    ;
  64. }
  65. function

    keyPush(

    evt)

    {
  66. switch

    (

    evt.keyCode

    )

    {
  67. case

    37

    :
  68. if

    (

    (

    xv!=

    1

    &&

    yv!=

    0

    )

    ||

    (

    xv==

    0

    &&

    yv==

    0

    )

    )

    {
  69. xv=-

    1

    ;

    yv=

    0

    ;
  70. }
  71. break

    ;
  72. case

    38

    :
  73. if

    (

    (

    xv!=

    0

    &&

    yv!=

    1

    )

    ||

    (

    xv==

    0

    &&

    yv==

    0

    )

    )

    {
  74. xv=

    0

    ;

    yv=-

    1

    ;
  75. }
  76. break

    ;
  77. case

    39

    :
  78. if

    (

    (

    xv!=-

    1

    &&

    yv!=

    0

    )

    ||

    (

    xv==

    0

    &&

    yv==

    0

    )

    )

    {
  79. xv=

    1

    ;

    yv=

    0

    ;
  80. }
  81. break

    ;
  82. case

    40

    :
  83. if

    (

    (

    xv!=

    0

    &&

    yv!=-

    1

    )

    ||

    (

    xv==

    0

    &&

    yv==

    0

    )

    )

    {
  84. xv=

    0

    ;

    yv=

    1

    ;
  85. }
  86. break

    ;
  87. }
  88. }
  89. function

    gameOver(

    )

    {
  90. tail =

    5

    ;
  91. if

    (

    score >

    highscore)

    {
  92. highscore =

    score;
  93. }
  94. score =

    0

    ;
  95. scoreShow.innerHTML

    =

    0

    ;
  96. highscoreShow.innerHTML

    =

    highscore;

  97. }
  98. </

    script>
  99. </

    body>
  100. </

    html>

That ends this tutorial. Happy Coding :)


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

452,292

323,526

323,535

Top