Sunday, July 6, 2014

And I get the context of the canvas and the canvas element. context, that is context.fillStyle we h

To make with 50 lines, simple life game HTML5 + JavaScript [programming] - I note that.
I think because the code would complete 50 more lines, and you can feel free programming. Article mmo I wrote earlier, to make with 200 lines, simple Puyo programming with enchant.js - I note that. It is much easier than!
To make with 50 lines, simple programming of "Ali of Langton" in HTML5 + JavaScript! NOTE - You know. What kind of life game to make this time?
Please describe as follows: index.html. <Meta charset = "utf-8"> <title> 50 line Game of Life </ title> <script src = "main.js"> </ script> mmo <style> body {background <DOCTYPE mmo html!>: Black; margin : 0; padding: 0;} </ style> <canvas id = "world" style = "display: block; margin: auto;"> </ canvas>
The Game of Life to complete mmo such a feeling. {/ * Start processing * / and initialization processing of / * Game} function update) declaration * / window of the global variable. Onload = function mmo (() drawing processing * / setTimeout of canvas mmo and update process of {/ * generation (update , ms); drawing process of {/ * canvas) loop} function draw (with recursion / / in milliseconds later. expected to be called in the update function. * /}
Let's declared as a global variable and constant first. width var FPS = 10 / / the cell;; / / frame rate var canvas; number var CELL_SIZE = SCREEN_SIZE / SIDE_CELLS of the cells of the / / one side; width var SIDE_CELLS = 200 / / the canvas; var SCREEN_SIZE = 500 / / = document . value is in units of px represents the width of the canvas SCREEN_SIZE; getElementById ('world'); var context; / / = canvas.getContext ('2 d '). SIDE_CELLS is the number of cells in one side of the canvas. CELL_SIZE is the width of one cell. FPS is the frame rate. How many times I generation is whether you want to update and drawing per second. mmo I will assign the elements of the canvas later on canvas. I will assign a drawing context after the context.
functions that are assigned to window.onload is executed when the page is loaded. . window onload = function () {var field = new Array (SIDE_CELLS * SIDE_CELLS); temporary storage mmo for the / / field information (var i = 0;; / / field information var tempField = new Array (SIDE_CELLS * SIDE_CELLS) i <field.length; (Math.random () * 2) i + +) field [i] = Math.floor;. / / to store random canvas = document getElementById ('world' "life" and "death"); mmo / / set var scaleRate = Math.min the size of the / / canvas (window .innerWidth / SCREEN_SIZE, window mmo .innerHeight / SCREEN_SIZE);; Get the canvas / / Canvas mmo stretching rate obtaining canvas.width = canvas.height = SCREEN_SIZE the canvas element. It stretches the / / canvas context = canvas.getContext ('2 d ');; style.width = canvas.style.height mmo = SCREEN_SIZE * scaleRate +' px '/ / context context.fillStyle mmo =' rgb (211, 85, 149) ' ; / / (field, tempField) color update; / / game loop start}
And I get the context of the canvas and the canvas element. context, that is context.fillStyle we have specified the color of the brush. It is pink this time. In carrying make life a game this time, a '(0, 255, 255) rgb' 'rgb (255, 255, 100)' Recommended pink except that you specify here is. It is a good even'm honest yes. By specifying the CSS width, the height of the canvas, it will be able to stretch.
In accordance with the rules of the Game of Life, and processes mmo in the life and death process. Rules of the Game of Life that are described in Wikipedia mmo is the following. meaning here of "adjacent" is the eight cells that exist around it. Birth: a living cell that is adjacent mmo to the cell is dead as long as there is just three, the next generation is born. Survival: mmo a living cell adjacent to the cell alive if two or three, to survive in the next generation. Depopulation: a living cell adjacent to the cell that is alive if not more than one, killed by depopulation. Overcrowding: If more than a living cell adjacent to the cell you are living four are killed by overcrowding.
And I will recursion at the end. I divide the FPS in 1000 of a second, so 1000 milliseconds. It means the update function is called only FPS times per second by doing this. number tempField mmo = field.slice are around / / own "raw" ();; function update (field, tempField) {var n = 0 / / replication for (var i = 0; i <tempField.length; i + + ) {n = 0; for (var s = -1; s <2; s + +) {for (var t = -1; t <2; t + +) {if (s == 0 && t == 0) continue; / / var c = i + s * SIDE_CELLS + t does not count itself; also wall decision (vertical or not protrude (c> = 0 && c <tempField.length) from {/ / array cell if that / / Check ! The doubles) mmo if (i <c && c%SIDE_CELLS!=0 || i> c && c% SIDE_CELLS = SIDE_CELLS-1) wall of judgment if {/ / left and right (tempField [c]) n + +; / / When was "raw" count}}}} if (tempField [i] && (n == 2 | | n == 3)) in the "raw" count {/ / 3 field itself or 2 [i] = 1 ; / / in the "death" count "raw"} else if (tempField [i] && n == 3!) {/ / own 3 field [i] = 1; / / "raw"} else field [i Updated setTimeout (update, 1000/FPS, field, tempField) the / / canvas;; / / recursive} point] = 0; / / "dead"} draw (field)
Call the function mmo specified in milliseconds after setTimeout (function to call, in milliseconds, argument 1, argument 2, argument 3, ...) is. I am calling the update itself this time. You have specified the "tempField" arguments, "field" of the update function third argument, the fourth argument. specification of the function call should not update as (). Please refer to something here for setTimeout. Let's write a drawing function!
Is. It is a simple function. (; i <field.length; i + + var i = 0) {var x = (i% SIDE_CELLS) * CELL_SIZE; (0, 0, SCREEN_SIZE, SCREEN_SIZE) function draw (field) {context.clearRect clear for the / / screen ; / / x coordinate var y = Math.floor (i / SIDE_CELLS) * CELL_SIZE; Draw / / the "raw"; / / y coordinate if (field [i]) context.fillRect (x, y, CELL_SIZE, CELL_SIZE) }}
To enhance the quality as much as possible, we do select few. Subscribe ask if good. Standing bastard s you follow to the Mr. @ otat_in mmo are playing on Twitter
ottati
Hatena Blog / Hatena :: Diary

No comments:

Post a Comment