-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeJStrial.html
200 lines (143 loc) · 4.57 KB
/
snakeJStrial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!--This is my index.html file for my JavaScript and jQuery-based game project. Note: DON'T BREAK THE BUILD-->
<!--This file has been renamed to snakeJStrial for additional clarity for team projects. Note: DON'T BREAK THE BUILD-->
<!-- LEARNINGS AND REFERENCES FOR THIS CODE BASE CAME FROM CODEPEN, YOUTUBE, AND CODECADEMY.
CITATION AND ACADEMIC REFERENCING HAS BEEN FILED WITHIN THE README FILE OF THIS REPOSITORY-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" name="viewport" width="device-width, initial-scale=1;">
<title>My Snake Game on jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
/*This is the body background, make the background color yellow */
body {
background-color: #ffc728;
}
/*This is the canvas style in which the snak game will be played bounded by the dark grey border */
#myCanvas {
display: block;
margin: 30px auto;
border: 1px solid #222;
}
#theSnake{}
</style>
</head>
<body>
<!--The canvas width and height can be changed here, it works best as a square-->
<canvas id="myCanvas" width="600" height="600"></canvas>
</body>
</html>
<!--Insert JavaScript / jQuery here and transfer (if required) to separate JS file post-build and sandbox-->
<!--jQuery library linked to Google library-->
<script>
$(document).ready(function(){
var canvas = $("#myCanvas")[0];
var canvasContext = canvas.getContext("2d");
var canvasWidth = $("#myCanvas").width();
var canvasHeight = $("#myCanvas").height();
var cellWidth = 10;
var myDirections;
var myFood;
var myScore;
var myLevel;
//The body of the snake itself
var snake_body
function init()
{
myDirections = "right";
create_snake();
create_food();
myScore = 0;
myLevel = 1;
if(typeof game_loop != "undefined") clearInterval(game_loop);
game_loop = setInterval(paint, 100);
}
init();
function create_snake()
{
var length = 5;
snake_body = [];
for(var i = length-1; i>=0; i--)
{
snake_body.push({x:i, y:0});
}
}
function create_food()
{
myFood = {
x: Math.round(Math.random()*(canvasWidth-cellWidth)/cellWidth),
y: Math.round(Math.random()*(canvasHeight-cellWidth)/cellWidth),
};
}
function paint()
{
canvasContext.fillStyle = "white";
canvasContext.fillRect(0, 0, canvasWidth, canvasHeight);
canvasContext.strokeStyle = "black";
canvasContext.strokeRect(0, 0, canvasWidth, canvasHeight);
var nx = snake_body[0].x;
var ny = snake_body[0].y;
if(myDirections == "right") nx++;
else if(myDirections == "left") nx--;
else if(myDirections == "up") ny--;
else if(myDirections == "down") ny++;
if(nx == -1 || nx == canvasWidth/cellWidth || ny == -1 || ny == canvasHeight/cellWidth || check_collision(nx, ny, snake_body))
{
init();
return;
}
if(nx == myFood.x && ny == myFood.y)
{
var tail = {x: nx, y: ny};
myScore++;
create_food();
}
else
{
var tail = snake_body.pop();
tail.x = nx; tail.y = ny;
}
snake_body.unshift(tail);
for(var i = 0; i < snake_body.length; i++)
{
var c = snake_body[i];
paint_cell(c.x, c.y, "black");
}
paint_cell(myFood.x, myFood.y, "green");
var score_text = "Score: " + score;
var level_text = "Level: " + level;
canvasContext.fillText(score_text, 5, canvasHeight-5);
canvasContext.fillText(level_text, 60, canvasHeight-5);
}
function paint_cell(x, y, color)
{
canvasContext.fillStyle = color;
canvasContext.fillRect(x*cellWidth, y*cellWidth, cellWidth, cellWidth);
canvasContext.strokeStyle = "white";
canvasContext.strokeRect(x*cw, y*cellWidth, cellWidth, cellWidth);
}
function check_collision(x, y, array)
{
for(var i = 0; i < array.length; i++)
{
if(array[i].x == x && array[i].y == y)
return true;
}
return false;
}
})
$(document).keydown(function(e){
var key = e.which;
if(key == "37" && myDirections != "right") myDirections = "left";
else if(key == "38" && myDirections != "down") myDirections = "up";
else if(key == "39" && myDirections != "left") myDirections = "right";
else if(key == "40" && myDirections != "up") myDirections = "down";
})
</script>
<script>
var snake_body = document.getElementById('snake_body');
document.getElementById('snake_body').onclick = function(){
game_loop = setInterval(paint, 1000);
}
</script>
/* Debugging required. This game is just not working despite following the tutorials. I do not want to wholesale copy any code despite referencing it so I am going to return to this code base after completing Pong, which is seeing more success. */