-
Notifications
You must be signed in to change notification settings - Fork 10
/
Lesson-2.html
288 lines (224 loc) · 11.9 KB
/
Lesson-2.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!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">Drawing the Snake</h2>
<div class="section-block">
<p>As we mentioned earlier, the <b>Game state</b> is where the actual game play happens. This is also the place where we will draw the snake. Like we did with the Menu state, we need to register the <b>Game state</b> with the global game object in <b>main.js</b>.
<br>Here is how your code should look like:</p>
</div>
</section>
<!--//doc-section-->
<section id="gamesdev-section" class="doc-section">
<h2 class="section-title">main.js</h2>
<div id="step1" class="section-block">
<pre><code class="language-markup">
var game;
game = new Phaser.Game(600, 450, Phaser.AUTO, '');
game.state.add('Menu', Menu);
// Adding the Game state.
game.state.add('Game', Game);
game.state.start('Menu');
</code></pre>
</div>
<!--//section-block-->
</section>
</div>
<!--//content-inner-->
<div class="content-inner">
<section id="nextsteps-section" class="doc-section">
<h2 class="section-title">menu.js</h2>
<div class="section-block">
<p>We'll also want to add some code in <b>menu.js</b> to let us start the <b>Game</b> state when it is clicked. For this purpose, we will replace the sprite with a button. Adding a button is basically the same as adding a sprite, you only need to provide a function to be called when it is clicked.
<br>Here is the final <b>menu.js</b> code:</p>
<pre><code class="language-markup">
var Menu = {
preload : function() {
// Load all the needed resources for the menu.
game.load.image('menu', './assets/images/menu.png');
},
create: function () {
// Add menu screen.
// It will act as a button to start the game.
this.add.button(0, 0, 'menu', this.startGame, this);
},
startGame: function () {
// Change the state to the actual game.
this.state.start('Game');
}
};
</code></pre>
</div>
</section>
<!--//doc-section-->
<!--//doc-section-->
</div>
<div class="content-inner">
<section id="nextsteps-section" class="doc-section">
<h2 class="section-title">game.js</h2>
<div class="section-block">
<p>We can now proceed with coding the <b>Game</b> state and drawing the snake. The structure is similar to the one of the Menu state.</p>
<pre><code class="language-markup">
var snake, apple, squareSize, score, speed,
updateDelay, direction, new_direction,
addNew, cursors, scoreTextValue, speedTextValue,
textStyle_Key, textStyle_Value;
var Game = {
preload : function() {
// Here we load all the needed resources for the level.
// In our case, that's just two squares - one for the snake body and one for the apple.
game.load.image('snake', './assets/images/snake.png');
game.load.image('apple', './assets/images/apple.png');
},
create : function() {
// By setting up global variables in the create function, we initialise them on game start.
// We need them to be globally available so that the update function can alter them.
snake = []; // This will work as a stack, containing the parts of our snake
apple = {}; // An object for the apple;
squareSize = 15; // The length of a side of the squares. Our image is 15x15 pixels.
score = 0; // Game score.
speed = 0; // Game speed.
updateDelay = 0; // A variable for control over update rates.
direction = 'right'; // The direction of our snake.
new_direction = null; // A buffer to store the new direction into.
addNew = false; // A variable used when an apple has been eaten.
// Set up a Phaser controller for keyboard input.
cursors = game.input.keyboard.createCursorKeys();
game.stage.backgroundColor = '#061f27';
// Generate the initial snake stack. Our snake will be 10 elements long.
// Beginning at X=150 Y=150 and increasing the X on every iteration.
for(var i = 0; i < 10; i++){
snake[i] = game.add.sprite(150+i*squareSize, 150, 'snake'); // Parameters are (X coordinate, Y coordinate, image)
}
// Genereate the first apple.
this.generateApple();
// Add Text to top of game.
textStyle_Key = { font: "bold 14px sans-serif", fill: "#46c0f9", align: "center" };
textStyle_Value = { font: "bold 18px sans-serif", fill: "#fff", align: "center" };
// Score.
game.add.text(30, 20, "SCORE", textStyle_Key);
scoreTextValue = game.add.text(90, 18, score.toString(), textStyle_Value);
// Speed.
game.add.text(500, 20, "SPEED", textStyle_Key);
speedTextValue = game.add.text(558, 18, speed.toString(), textStyle_Value);
},
update: function() {
// The update function is called constantly at a high rate (somewhere around 60fps),
// updating the game field every time.
// We are going to leave that one empty for now.
},
generateApple: function(){
// Chose a random place on the grid.
// X is between 0 and 585 (39*15)
// Y is between 0 and 435 (29*15)
var randomX = Math.floor(Math.random() * 40 ) * squareSize,
randomY = Math.floor(Math.random() * 30 ) * squareSize;
// Add a new apple.
apple = game.add.sprite(randomX, randomY, 'apple');
}
};
</code></pre>
</div>
</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>