-
Notifications
You must be signed in to change notification settings - Fork 10
/
Lesson-4.html
269 lines (198 loc) · 10.2 KB
/
Lesson-4.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<head>
<title>Snake Game-Phaser</title>
<!-- Meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="favicon.ico">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- Global CSS -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">
<!-- Plugins CSS -->
<link rel="stylesheet" href="assets/plugins/font-awesome/css/font-awesome.css">
<link rel="stylesheet" href="assets/plugins/prism/prism.css">
<link rel="stylesheet" href="assets/plugins/elegant_font/css/style.css">
<!-- Theme CSS -->
<link id="theme-style" rel="stylesheet" href="assets/css/styles.css">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body class="body-green">
<div class="page-wrapper">
<!-- ******Header****** -->
<header id="header" class="header">
<div class="container">
<div class="branding">
<h1 class="logo">
<a href="index.html">
<span aria-hidden="true" class="icon_documents_alt icon"></span>
<span class="text-highlight">Game Development</span>
</a>
</h1>
</div>
<!--//branding-->
<ol class="breadcrumb">
<li><a href="index.html">Home</a></li>
<li class="active">Game Development</li>
</ol>
</div>
<!--//container-->
</header>
<!--//header-->
<div class="doc-wrapper">
<div class="container">
<div id="doc-header" class="doc-header text-center">
<h1 class="doc-title"><i class="icon fa fa-paper-plane"></i>Game Development</h1>
<div class="meta"><i class="fa fa-clock-o"></i> Snake Game using Phaser</div>
</div>
<div class="doc-body">
<div class="doc-content">
<div class="content-inner">
<section id="introduction-section" class="doc-section">
<h2 class="section-title">Collision Detection</h2>
<div class="section-block">
<p>A game in which the snake is roaming freely in the playing field is not much fun. We need to detect when the snake comes in contact with a wall, an apple or itself. This is called collision detection.<br></p>
<p>This is usually done by using a physics engine, of which the Phaser framework supports a few. But they are too complex for a simple game like this. We will instead do our own collision detection by comparing coordinates.<br></p>
<p>In the update function, after the code for moving the snake, we call a number of methods. They will compare coordinates to tell us if a collision has occurred.</p>
<pre><code class="language-markup">
update: function() {
// Snake movement
// ...
// End of snake movement
// Increase length of snake if an apple had been eaten.
// Create a block in the back of the snake with the old position of the previous last block
// (it has moved now along with the rest of the snake).
if(addNew){
snake.unshift(game.add.sprite(oldLastCellx, oldLastCelly, 'snake'));
addNew = false;
}
// Check for apple collision.
this.appleCollision();
// Check for collision with self. Parameter is the head of the snake.
this.selfCollision(firstCell);
// Check with collision with wall. Parameter is the head of the snake.
this.wallCollision(firstCell);
}
},
appleCollision: function() {
// Check if any part of the snake is overlapping the apple.
// This is needed if the apple spawns inside of the snake.
for(var i = 0; i < snake.length; i++){
if(snake[i].x == apple.x && snake[i].y == apple.y){
// Next time the snake moves, a new block will be added to its length.
addNew = true;
// Destroy the old apple.
apple.destroy();
// Make a new one.
this.generateApple();
// Increase score.
score++;
// Refresh scoreboard.
scoreTextValue.text = score.toString();
}
}
},
selfCollision: function(head) {
// Check if the head of the snake overlaps with any part of the snake.
for(var i = 0; i < snake.length - 1; i++){
if(head.x == snake[i].x && head.y == snake[i].y){
// If so, go to game over screen.
game.state.start('Game_Over');
}
}
},
wallCollision: function(head) {
// Check if the head of the snake is in the boundaries of the game field.
if(head.x >= 600 || head.x < 0 || head.y >= 450 || head.y < 0){
// If it's not in, we've hit a wall. Go to game over screen.
game.state.start('Game_Over');
}
}
</code></pre>
<p>When the snake collides with the apple we increase the score and the length of the snake. But when a collision with the wall or the snake body occurs, we should end the game. To do this, we need to make the <b>Game_Over</b> state. Again, we need to register it with main.js. Add this line near the bottom of that file:</p>
<p><b>main.js</b></p>
<pre><code class="language-markup">
game.state.add('Game_Over', Game_Over);
</code>
</pre>
<p>And the state itself:</p>
<p><b>game_over.js</b></p>
<pre><code class="language-markup">
var Game_Over = {
preload : function() {
// Load the needed image for this game screen.
game.load.image('gameover', './assets/images/gameover.png');
},
create : function() {
// Create button to start game like in Menu.
this.add.button(0, 0, 'gameover', this.startGame, this);
// Add text with information about the score from last game.
game.add.text(235, 350, "LAST SCORE", { font: "bold 16px sans-serif", fill: "#46c0f9", align: "center"});
game.add.text(350, 348, score.toString(), { font: "bold 20px sans-serif", fill: "#fff", align: "center" });
},
startGame: function () {
// Change the state back to Game.
this.state.start('Game');
}
};
</code>
</pre>
<P><b>That's it our Snake Game is Ready!!</b></P>
</div>
</section>
<!--//doc-section-->
<!--//doc-section-->
<!--//doc-section-->
</div>
<!--//content-inner-->
</div>
<!--//doc-content-->
<div class="doc-sidebar hidden-xs">
<nav id="doc-nav">
<ul id="doc-menu" class="nav doc-menu" data-spy="affix">
<li><a href="Game-Development.html">Intro & Setup</a></li>
<li><a href="Lesson-1.html">Loading an Image</a></li>
<li><a href="Lesson-2.html">Drawing the Snake</a></li>
<li><a href="Lesson-3.html">Movement and Control</a></li>
<li><a href="Lesson-4.html">Collision Detection</a></li>
</ul>
<!--//doc-menu-->
</nav>
</div>
<!--//doc-sidebar-->
</div>
<!--//doc-body-->
</div>
<!--//container-->
</div>
<!--//doc-wrapper-->
</div>
<!--//page-wrapper-->
<footer id="footer" class="footer text-center">
<div class="container">
<!--/* This template is released under the Creative Commons Attribution 3.0 License. Please keep the attribution link below when using for your own project. Thank you for your support. :) If you'd like to use the template without the attribution, you can check out other license options via our website: themes.3rdwavemedia.com */-->
<small class="copyright"><p class="license">Portions of this content are ©1998–2016 by individual mozilla.org contributors. Content available under a <a href="https://www.mozilla.org/foundation/licensing/website-content/">Creative Commons license</a>.</p></small>
</div>
<!--//container-->
</footer>
<!--//footer-->
<!-- Main Javascript -->
<script type="text/javascript" src="assets/plugins/jquery-1.12.3.min.js"></script>
<script type="text/javascript" src="assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/plugins/prism/prism.js"></script>
<script type="text/javascript" src="assets/plugins/jquery-scrollTo/jquery.scrollTo.min.js"></script>
<script type="text/javascript" src="assets/plugins/jquery-match-height/jquery.matchHeight-min.js"></script>
<script type="text/javascript" src="assets/js/main.js"></script>
</body>
</html>