Skip to content

Commit e4783c0

Browse files
nschonniteoli2003rubiesonthesky
authored
chore: run Prettier on games (mdn#19943)
* chore: run Prettier on games * fix: use prettier-ignore for explicit semicolon * Update files/en-us/games/tutorials/2d_breakout_game_pure_javascript/build_the_brick_field/index.md Co-authored-by: rubiesonthesky <[email protected]> * chore: use Prettier bracketSameLine Co-authored-by: Jean-Yves Perrier <[email protected]> Co-authored-by: rubiesonthesky <[email protected]>
1 parent 6e578cf commit e4783c0

File tree

71 files changed

+797
-510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+797
-510
lines changed

.prettierrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bracketSameLine": true
3+
}

files/en-us/games/anatomy/index.md

+63-52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
- Main Loop
88
- requestAnimationFrame
99
---
10+
1011
{{GamesSidebar}}
1112

1213
This article looks at the anatomy and workflow of the average video game from a technical point of view, in terms of how the main loop should run. It helps beginners to modern game development understand what is required when building a game and how web standards like JavaScript lend themselves as tools. Experienced game programmers who are new to web development could also benefit, too.
@@ -55,13 +56,14 @@ But do not immediately assume animations require frame-by-frame control. Simple
5556

5657
There are two obvious issues with our previous main loop: `main()` pollutes the `{{ domxref("window") }}` object (where all global variables are stored) and the example code did not leave us with a way to _stop_ the loop unless the whole tab is closed or refreshed. For the first issue, if you want the main loop to just run and you do not need easy (direct) access to it, you could create it as an Immediately-Invoked Function Expression (IIFE).
5758

59+
<!-- prettier-ignore-start -->
5860
```js
5961
/*
60-
* Starting with the semicolon is in case whatever line of code above this example
61-
* relied on automatic semicolon insertion (ASI). The browser could accidentally
62-
* think this whole example continues from the previous line. The leading semicolon
63-
* marks the beginning of our new line if the previous one was not empty or terminated.
64-
*/
62+
* Starting with the semicolon is in case whatever line of code above this example
63+
* relied on automatic semicolon insertion (ASI). The browser could accidentally
64+
* think this whole example continues from the previous line. The leading semicolon
65+
* marks the beginning of our new line if the previous one was not empty or terminated.
66+
*/
6567

6668
;(() => {
6769
function main() {
@@ -73,22 +75,24 @@ There are two obvious issues with our previous main loop: `main()` pollutes the
7375
main(); // Start the cycle
7476
})();
7577
```
78+
<!-- prettier-ignore-end -->
7679

7780
When the browser comes across this IIFE, it will define your main loop and immediately queue it for the next frame. It will not be attached to any object and `main` (or `main()` for methods) will be a valid unused name in the rest of the application, free to be defined as something else.
7881

7982
> **Note:** In practice, it is more common to prevent the next `requestAnimationFrame()` with an if-statement, rather than calling `cancelAnimationFrame()`.
8083
8184
For the second issue, stopping the main loop, you will need to cancel the call to `main()` with `{{ domxref("window.cancelAnimationFrame()") }}`. You will need to pass `cancelAnimationFrame()` the ID token given by `requestAnimationFrame()` when it was last called. Let us assume that your game's functions and variables are built on a namespace that you called `MyGame`. Expanding our last example, the main loop would now look like:
8285

86+
<!-- prettier-ignore-start -->
8387
```js
8488
/*
85-
* Starting with the semicolon is in case whatever line of code above this example
86-
* relied on automatic semicolon insertion (ASI). The browser could accidentally
87-
* think this whole example continues from the previous line. The leading semicolon
88-
* marks the beginning of our new line if the previous one was not empty or terminated.
89-
*
90-
* Let us also assume that MyGame is previously defined.
91-
*/
89+
* Starting with the semicolon is in case whatever line of code above this example
90+
* relied on automatic semicolon insertion (ASI). The browser could accidentally
91+
* think this whole example continues from the previous line. The leading semicolon
92+
* marks the beginning of our new line if the previous one was not empty or terminated.
93+
*
94+
* Let us also assume that MyGame is previously defined.
95+
*/
9296

9397
;(() => {
9498
function main() {
@@ -100,6 +104,7 @@ For the second issue, stopping the main loop, you will need to cancel the call t
100104
main(); // Start the cycle
101105
})();
102106
```
107+
<!-- prettier-ignore-end -->
103108

104109
We now have a variable declared in our `MyGame` namespace, which we call `stopMain`, that contains the ID returned from our main loop's most recent call to `requestAnimationFrame()`. At any point, we can stop the main loop by telling the browser to cancel the request that corresponds to our token.
105110

@@ -134,15 +139,16 @@ const tNow = window.performance.now();
134139

135140
Back to the topic of the main loop. You will often want to know when your main function was invoked. Because this is common, `window.requestAnimationFrame()` always provides a `DOMHighResTimeStamp` to callbacks as an argument when they are executed. This leads to another enhancement to our previous main loops.
136141

142+
<!-- prettier-ignore-start -->
137143
```js
138144
/*
139-
* Starting with the semicolon is in case whatever line of code above this example
140-
* relied on automatic semicolon insertion (ASI). The browser could accidentally
141-
* think this whole example continues from the previous line. The leading semicolon
142-
* marks the beginning of our new line if the previous one was not empty or terminated.
143-
*
144-
* Let us also assume that MyGame is previously defined.
145-
*/
145+
* Starting with the semicolon is in case whatever line of code above this example
146+
* relied on automatic semicolon insertion (ASI). The browser could accidentally
147+
* think this whole example continues from the previous line. The leading semicolon
148+
* marks the beginning of our new line if the previous one was not empty or terminated.
149+
*
150+
* Let us also assume that MyGame is previously defined.
151+
*/
146152

147153
;(() => {
148154
function main(tFrame) {
@@ -155,6 +161,7 @@ Back to the topic of the main loop. You will often want to know when your main f
155161
main(); // Start the cycle
156162
})();
157163
```
164+
<!-- prettier-ignore-end -->
158165

159166
Several other optimizations are possible and it really depends on what your game attempts to accomplish. Your game genre will obviously make a difference but it could even be more subtle than that. You could draw every pixel individually on a canvas or you could layer DOM elements (including multiple WebGL canvases with transparent backgrounds if you want) into a complex hierarchy. Each of these paths will lead to different opportunities and constraints.
160167

@@ -168,15 +175,16 @@ You will need to make hard decisions about your main loop: how to simulate the a
168175

169176
If your game can hit the maximum refresh rate of any hardware you support then your job is fairly easy. You can update, render, and then do nothing until VSync.
170177

178+
<!-- prettier-ignore-start -->
171179
```js
172180
/*
173-
* Starting with the semicolon is in case whatever line of code above this example
174-
* relied on automatic semicolon insertion (ASI). The browser could accidentally
175-
* think this whole example continues from the previous line. The leading semicolon
176-
* marks the beginning of our new line if the previous one was not empty or terminated.
177-
*
178-
* Let us also assume that MyGame is previously defined.
179-
*/
181+
* Starting with the semicolon is in case whatever line of code above this example
182+
* relied on automatic semicolon insertion (ASI). The browser could accidentally
183+
* think this whole example continues from the previous line. The leading semicolon
184+
* marks the beginning of our new line if the previous one was not empty or terminated.
185+
*
186+
* Let us also assume that MyGame is previously defined.
187+
*/
180188

181189
;(() => {
182190
function main(tFrame) {
@@ -189,6 +197,7 @@ If your game can hit the maximum refresh rate of any hardware you support then y
189197
main(); // Start the cycle
190198
})();
191199
```
200+
<!-- prettier-ignore-end -->
192201

193202
If the maximum refresh rate cannot be reached, quality settings could be adjusted to stay under your time budget. The most famous example of this concept is the game from id Software, RAGE. This game removed control from the user in order to keep its calculation time at roughly 16ms (or roughly 60fps). If computation took too long then rendered resolution would decrease, textures and other assets would fail to load or draw, and so forth. This (non-web) case study made a few assumptions and tradeoffs:
194203

@@ -229,34 +238,35 @@ A separate update and draw method could look like the following example. For the
229238

230239
> **Warning:** This example, specifically, is in need of technical review.
231240
241+
<!-- prettier-ignore-start -->
232242
```js
233243
/*
234-
* Starting with the semicolon is in case whatever line of code above this example
235-
* relied on automatic semicolon insertion (ASI). The browser could accidentally
236-
* think this whole example continues from the previous line. The leading semicolon
237-
* marks the beginning of our new line if the previous one was not empty or terminated.
238-
*
239-
* Let us also assume that MyGame is previously defined.
240-
*
241-
* MyGame.lastRender keeps track of the last provided requestAnimationFrame timestamp.
242-
* MyGame.lastTick keeps track of the last update time. Always increments by tickLength.
243-
* MyGame.tickLength is how frequently the game state updates. It is 20 Hz (50ms) here.
244-
*
245-
* timeSinceTick is the time between requestAnimationFrame callback and last update.
246-
* numTicks is how many updates should have happened between these two rendered frames.
247-
*
248-
* render() is passed tFrame because it is assumed that the render method will calculate
249-
* how long it has been since the most recently passed update tick for
250-
* extrapolation (purely cosmetic for fast devices). It draws the scene.
251-
*
252-
* update() calculates the game state as of a given point in time. It should always
253-
* increment by tickLength. It is the authority for game state. It is passed
254-
* the DOMHighResTimeStamp for the time it represents (which, again, is always
255-
* last update + MyGame.tickLength unless a pause feature is added, etc.)
256-
*
257-
* setInitialState() Performs whatever tasks are leftover before the main loop must run.
258-
* It is just a generic example function that you might have added.
259-
*/
244+
* Starting with the semicolon is in case whatever line of code above this example
245+
* relied on automatic semicolon insertion (ASI). The browser could accidentally
246+
* think this whole example continues from the previous line. The leading semicolon
247+
* marks the beginning of our new line if the previous one was not empty or terminated.
248+
*
249+
* Let us also assume that MyGame is previously defined.
250+
*
251+
* MyGame.lastRender keeps track of the last provided requestAnimationFrame timestamp.
252+
* MyGame.lastTick keeps track of the last update time. Always increments by tickLength.
253+
* MyGame.tickLength is how frequently the game state updates. It is 20 Hz (50ms) here.
254+
*
255+
* timeSinceTick is the time between requestAnimationFrame callback and last update.
256+
* numTicks is how many updates should have happened between these two rendered frames.
257+
*
258+
* render() is passed tFrame because it is assumed that the render method will calculate
259+
* how long it has been since the most recently passed update tick for
260+
* extrapolation (purely cosmetic for fast devices). It draws the scene.
261+
*
262+
* update() calculates the game state as of a given point in time. It should always
263+
* increment by tickLength. It is the authority for game state. It is passed
264+
* the DOMHighResTimeStamp for the time it represents (which, again, is always
265+
* last update + MyGame.tickLength unless a pause feature is added, etc.)
266+
*
267+
* setInitialState() Performs whatever tasks are leftover before the main loop must run.
268+
* It is just a generic example function that you might have added.
269+
*/
260270

261271
;(() => {
262272
function main(tFrame) {
@@ -293,6 +303,7 @@ A separate update and draw method could look like the following example. For the
293303
main(performance.now()); // Start the cycle
294304
})();
295305
```
306+
<!-- prettier-ignore-end -->
296307

297308
Another alternative is to do certain things less often. If a portion of your update loop is difficult to compute but insensitive to time, you might consider scaling back its frequency and, ideally, spreading it out into chunks throughout that lengthened period. An implicit example of this was found over at The Artillery Blog for Artillery Games, where they [adjust their rate of garbage generation](https://web.archive.org/web/20161021030645/http://blog.artillery.com/2012/10/browser-garbage-collection-and-framerate.html) to optimize garbage collection. Obviously, cleaning up resources is not time sensitive (especially if tidying is more disruptive than the garbage itself).
298309

files/en-us/games/examples/index.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
- Games
88
- Web
99
---
10+
1011
{{GamesSidebar}}
1112

1213
This page lists a number of impressive web technology demos for you to get inspiration from, and generally have fun with. A testament to what can be done with JavaScript, WebGL, and related technologies. The first two sections list playable games, while the second is a catch-all area to list demos that aren't necessarily interactive/games.
@@ -52,10 +53,8 @@ This page lists a number of impressive web technology demos for you to get inspi
5253

5354
- [A Wizard's Lizard](http://www.wizardslizard.com/)
5455
- : Top down Zelda-esque exploration/RPG.
55-
56-
**[Bullet Force](https://www.crazygames.com/game/bullet-force-multiplayer)**
57-
3D multiplayer first-person shooter.
58-
56+
- [Bullet Force](https://www.crazygames.com/game/bullet-force-multiplayer)
57+
- : 3D multiplayer first-person shooter.
5958
- [Elliot Quest](https://elliotquest.com/)
6059
- : 8-bit graphic retro adventure game.
6160
- [RPG MO](https://data.mo.ee/index2.html)

files/en-us/games/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- JavaScript Games
1111
- Web
1212
---
13+
1314
{{GamesSidebar}}
1415

1516
Gaming is one of the most popular computer activities. New technologies are constantly arriving to make it possible to develop better and more powerful games that can be run in any standards-compliant web browser.

files/en-us/games/introduction/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
- Guide
88
- Mobile
99
---
10+
1011
{{GamesSidebar}}
1112

1213
The modern web has quickly become a viable platform not only for creating stunning, high quality games, but also for distributing those games.

files/en-us/games/introduction_to_html5_game_development/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
- HTML
88
- Mobile
99
---
10+
1011
{{GamesSidebar}}
1112

1213
## Advantages

files/en-us/games/publishing_games/game_distribution/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ tags:
1414
- Web Stores
1515
- distribution
1616
---
17+
1718
{{GamesSidebar}}
1819

1920
You've followed a [tutorial](/en-US/docs/Games/Tutorials/2D_Breakout_game_pure_JavaScript) or [two](/en-US/docs/Games/Tutorials/2D_breakout_game_Phaser) and created an HTML game — that's great! This article covers all you need to know about the ways in which you can distribute your newly created game into the wild. This includes hosting it yourself online, submitting it to open marketplaces, and submitting it to closed ones like Google Play or the iOS App Store.

files/en-us/games/publishing_games/game_monetization/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ tags:
1111
- iap
1212
- licensing
1313
---
14+
1415
{{GamesSidebar}}
1516

1617
When you've spent your time building a game, [distributing](/en-US/docs/Games/Publishing_games/Game_distribution) it and [promoting](/en-US/docs/Games/Publishing_games/Game_promotion) it you should consider earning some money out of it. If your work is a serious endeavour on the path to becoming an independent game developer able to make a living, read on and see what your options are. The technology is mature enough; now it's just about choosing the right approach.

files/en-us/games/publishing_games/game_promotion/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- blog
1111
- competitions
1212
---
13+
1314
{{GamesSidebar}}
1415

1516
Developing and publishing your game is not enough. You have to let the world know that you have something interesting available that people will enjoy playing. There are many ways to promote your game — most of them being free, so even if you're struggling to make a living as an indie dev with zero budget you can still do a lot to let people know about your great new game. Promoting the game helps a lot when [monetizing](/en-US/docs/Games/Publishing_games/Game_monetization) it later on too, so it's important to do it correctly.
@@ -26,7 +27,7 @@ You should definitely create your own website containing all the information abo
2627

2728
You should also blog about everything related to your gamedev activities. Write about your development process, nasty bugs you encounter, funny stories, lessons learned, and the ups and downs of being a game developer. Continually publishing information about your games will help educate others, increase your reputation in the community, and further improve SEO. A further option is to publish [monthly reports](https://end3r.com/blog/?s=monthly+report) that summarize all your progress — it helps you see what you've accomplished throughout the month and what's still left to do, and it keeps reminding people that your game is coming out soon — building buzz is always good.
2829

29-
While you can create your website from scratch, there are also tools that can help make the process easier. [ManaKeep](https://manakeep.com) is a website builder made for indie game developers and provides a great starting point to create your website. [Presskit()](https://dopresskit.com/) is a press kit builder that helps you create a press page to share with the media.
30+
While you can create your website from scratch, there are also tools that can help make the process easier. [ManaKeep](https://manakeep.com) is a website builder made for indie game developers and provides a great starting point to create your website. [Presskit()](https://dopresskit.com/) is a press kit builder that helps you create a press page to share with the media.
3031

3132
## Social media
3233

files/en-us/games/publishing_games/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ tags:
1010
- distribution
1111
- publishing
1212
---
13+
1314
{{GamesSidebar}}
1415

1516
HTML games have a huge advantage over native in terms of publishing and distribution — you have the freedom of distribution, promotion and monetization of your game on the Web, rather than each version being locked into a single store controlled by one company. You can benefit from the web being truly multiplatform. This series of articles looks at the options you have when you want to publish and distribute your game, and earn something out of it while you wait for it to become famous.

0 commit comments

Comments
 (0)