├── Coin.js
├── index.html
├── Tile.js
├── Solid.js
├── Node.js
├── README.md
├── Brain.js
├── Dot.js
├── setting_level.js
├── Population.js
├── Player.js
├── sketch.js
└── libraries
└── p5.dom.js
/Coin.js:
--------------------------------------------------------------------------------
1 | class Coin {
2 | constructor(x,y){
3 | this.taken = false;
4 | this.pos = createVector(x,y);
5 | this.diameter = tileSize/2.0;
6 | }
7 |
8 | show(){
9 | if(!showedCoin && !this.taken){
10 | stroke(0);
11 | fill(255,230,230);
12 | ellipse(this.pos.x,this.pos.y,this.diameter);
13 | showedCoin = true;
14 | }
15 |
16 | }
17 |
18 | collides(ptl, pbr) {//player dimensions
19 | if(this.taken){ return false;}
20 |
21 | var topLeft = createVector(this.pos.x - this.diameter/2, this.pos.y-this.diameter/2);
22 | var bottomRight = createVector(this.pos.x + this.diameter/2, this.pos.y + this.diameter/2);
23 | if ((ptl.x
3 | An A.I. that learns to traverse a maze using the genetic algorithm.
4 |
5 | Using the genetic algorithm, the A.I. “learns” to go through the maze using the concepts of natural selection and survival of the fittest. The base of the natural selection is firstly a single generation of A.I.s (all randomly selected) and as they go through the maze, the fittest is selected by examining the shortest distance to checkpoint in least amount of moves. By increasing the number of moves available to them every five generations so that they master the moves more efficiently (incremental learning), they are able to ‘remember’ old directions and pass on genes of efficiency to later generations.
6 |
7 | **Note**: It takes about ~50 generations for the A.I. to reach the first checkpoint (when the evolution speed is set to 6), ~120 generations for the A.I. to reach the second, and so forth. The learning takes time but it eventually does win, never missing a checkpoint or dying in the face of enemies. Your own evolution took about 8 million years, so have patience with Adam A.I.
8 |
9 | ADAM A.I.
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
28 |
29 |
--------------------------------------------------------------------------------
/Tile.js:
--------------------------------------------------------------------------------
1 | class Tile{
2 |
3 | constructor(x,y){
4 | this.matrixPos = createVector(x,y);
5 | this.pixelPos = createVector(x*tileSize+xoff, y*tileSize+yoff);
6 | this.safe = false;
7 | this.goal = false;
8 | this.wall = false;
9 | this.edges = [];
10 |
11 | }
12 |
13 | show(){
14 | if ((this.matrixPos.x + this.matrixPos.y) % 2 ==0) {
15 | fill(230,230,255);
16 | } else {
17 | fill(230,230,255);
18 | }
19 | if (this.wall) {
20 | fill(230,230,255);
21 | }
22 | if (this.goal || this.safe) {
23 | fill(0, 0, 0);
24 | }
25 | noStroke();
26 | rect(this.pixelPos.x,this.pixelPos.y,tileSize,tileSize);
27 |
28 | }
29 |
30 | showEdges(){
31 | for (var i = 0; i< this.edges.length; i++) {
32 | stroke(0);
33 | strokeWeight(4);
34 | switch(this.edges[i]) {
35 | case 4:
36 | line(this.pixelPos.x, this.pixelPos.y, this.pixelPos.x+tileSize,this.pixelPos.y);
37 | break;
38 | case 1:
39 | line(this.pixelPos.x+tileSize, this.pixelPos.y, this.pixelPos.x+tileSize, this.pixelPos.y+tileSize);
40 | break;
41 | case 2:
42 | line(this.pixelPos.x, this.pixelPos.y+tileSize, this.pixelPos.x+tileSize, this.pixelPos.y+tileSize);
43 | break;
44 | case 3:
45 | line(this.pixelPos.x, this.pixelPos.y, this.pixelPos.x, this.pixelPos.y+tileSize);
46 | break;
47 | }
48 | }
49 | }
50 |
51 |
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/Solid.js:
--------------------------------------------------------------------------------
1 | class Solid {
2 |
3 | constructor(topL,botR){
4 | var lineWidth = 1;
5 | this.pos = createVector(topL.pixelPos.x-lineWidth, topL.pixelPos.y-lineWidth);
6 | this.w = botR.pixelPos.x - this.pos.x + lineWidth;
7 | this.h = botR.pixelPos.y - this.pos.y + lineWidth;
8 | this.bottomRight = createVector(this.pos.x + this.w, this.pos.y + this.h);
9 |
10 | }
11 |
12 |
13 | restrictMovement(tl, br, movement) {//player dimensions
14 | //add the x first
15 |
16 | var x = movement.x;
17 | var y = movement.y;
18 | //
19 | // movement.x = round(movement.x);
20 | // movement.y = round(movement.y);
21 | var ptl = createVector(tl.x+movement.x, tl.y);
22 | var pbr = createVector(br.x+movement.x, br.y);
23 |
24 | if ((ptl.x
10 |
The web is much more than just canvas and p5.dom makes it easy to interact 4 | * with other HTML5 objects, including text, hyperlink, image, input, video, 5 | * audio, and webcam.
6 | *There is a set of creation methods, DOM manipulation methods, and 7 | * an extended p5.Element that supports a range of HTML elements. See the 8 | * 9 | * beyond the canvas tutorial for a full overview of how this addon works. 10 | * 11 | *
Methods and properties shown in black are part of the p5.js core, items in 12 | * blue are part of the p5.dom library. You will need to include an extra file 13 | * in order to access the blue functions. See the 14 | * using a library 15 | * section for information on how to include this library. p5.dom comes with 16 | * p5 complete or you can download the single file 17 | * 18 | * here.
19 | *See tutorial: beyond the canvas 20 | * for more info on how to use this libary. 21 | * 22 | * @module p5.dom 23 | * @submodule p5.dom 24 | * @for p5.dom 25 | * @main 26 | */ 27 | 28 | (function (root, factory) { 29 | if (typeof define === 'function' && define.amd) 30 | define('p5.dom', ['p5'], function (p5) { (factory(p5));}); 31 | else if (typeof exports === 'object') 32 | factory(require('../p5')); 33 | else 34 | factory(root['p5']); 35 | }(this, function (p5) { 36 | 37 | // ============================================================================= 38 | // p5 additions 39 | // ============================================================================= 40 | 41 | /** 42 | * Searches the page for an element with the given ID, class, or tag name (using the '#' or '.' 43 | * prefixes to specify an ID or class respectively, and none for a tag) and returns it as 44 | * a p5.Element. If a class or tag name is given with more than 1 element, 45 | * only the first element will be returned. 46 | * The DOM node itself can be accessed with .elt. 47 | * Returns null if none found. You can also specify a container to search within. 48 | * 49 | * @method select 50 | * @param {String} name id, class, or tag name of element to search for 51 | * @param {String} [container] id, p5.Element, or HTML element to search within 52 | * @return {Object|p5.Element|Null} p5.Element containing node found 53 | * @example 54 | *
55 | * function setup() {
56 | * createCanvas(100,100);
57 | * //translates canvas 50px down
58 | * select('canvas').position(100, 100);
59 | * }
60 | *
62 | * // these are all valid calls to select()
63 | * var a = select('#moo');
64 | * var b = select('#blah', '#myContainer');
65 | * var c = select('#foo', b);
66 | * var d = document.getElementById('beep');
67 | * var e = select('p', d);
68 | *
114 | * function setup() {
115 | * createButton('btn');
116 | * createButton('2nd btn');
117 | * createButton('3rd btn');
118 | * var buttons = selectAll('button');
119 | *
120 | * for (var i = 0; i < buttons.length; i++){
121 | * buttons[i].size(100,100);
122 | * }
123 | * }
124 | *
126 | * // these are all valid calls to selectAll()
127 | * var a = selectAll('.moo');
128 | * var b = selectAll('div');
129 | * var c = selectAll('button', '#myContainer');
130 | * var d = select('#container');
131 | * var e = selectAll('p', d);
132 | * var f = document.getElementById('beep');
133 | * var g = select('.blah', f);
134 | *
203 | * function setup() {
204 | * createCanvas(100, 100);
205 | * createDiv('this is some text');
206 | * createP('this is a paragraph');
207 | * }
208 | * function mousePressed() {
209 | * removeElements(); // this will remove the div and p, not canvas
210 | * }
211 | *
243 | * var myDiv;
244 | * function setup() {
245 | * myDiv = createDiv('this is some text');
246 | * }
247 | *
248 | */
249 |
250 | /**
251 | * Creates a <p></p> element in the DOM with given inner HTML. Used
252 | * for paragraph length text.
253 | * Appends to the container node if one is specified, otherwise
254 | * appends to body.
255 | *
256 | * @method createP
257 | * @param {String} html inner HTML for element created
258 | * @return {Object|p5.Element} pointer to p5.Element holding created node
259 | * @example
260 | *
261 | * var myP;
262 | * function setup() {
263 | * myP = createP('this is some text');
264 | * }
265 | *
278 | * var mySpan;
279 | * function setup() {
280 | * mySpan = createSpan('this is some text');
281 | * }
282 | *
307 | * var img;
308 | * function setup() {
309 | * img = createImg('http://p5js.org/img/asterisk-01.png');
310 | * }
311 | *
352 | * var myLink;
353 | * function setup() {
354 | * myLink = createA('http://p5js.org/', 'this is a link');
355 | * }
356 | *
383 | * var slider;
384 | * function setup() {
385 | * slider = createSlider(0, 255, 100);
386 | * slider.position(10, 10);
387 | * slider.style('width', '80px');
388 | * }
389 | *
390 | * function draw() {
391 | * var val = slider.value();
392 | * background(val);
393 | * }
394 | *
397 | * var slider;
398 | * function setup() {
399 | * colorMode(HSB);
400 | * slider = createSlider(0, 360, 60, 40);
401 | * slider.position(10, 10);
402 | * slider.style('width', '80px');
403 | * }
404 | *
405 | * function draw() {
406 | * var val = slider.value();
407 | * background(val, 100, 100, 1);
408 | * }
409 | *
438 | * var button;
439 | * function setup() {
440 | * createCanvas(100, 100);
441 | * background(0);
442 | * button = createButton('click me');
443 | * button.position(19, 19);
444 | * button.mousePressed(changeBG);
445 | * }
446 | *
447 | * function changeBG() {
448 | * var val = random(255);
449 | * background(val);
450 | * }
451 | *
471 | * var checkbox;
472 | *
473 | * function setup() {
474 | * checkbox = createCheckbox('label', false);
475 | * checkbox.changed(myCheckedEvent);
476 | * }
477 | *
478 | * function myCheckedEvent() {
479 | * if (this.checked()) {
480 | * console.log("Checking!");
481 | * } else {
482 | * console.log("Unchecking!");
483 | * }
484 | * }
485 | *
533 | * var sel;
534 | *
535 | * function setup() {
536 | * textAlign(CENTER);
537 | * background(200);
538 | * sel = createSelect();
539 | * sel.position(10, 10);
540 | * sel.option('pear');
541 | * sel.option('kiwi');
542 | * sel.option('grape');
543 | * sel.changed(mySelectEvent);
544 | * }
545 | *
546 | * function mySelectEvent() {
547 | * var item = sel.value();
548 | * background(200);
549 | * text("it's a "+item+"!", 50, 50);
550 | * }
551 | *
601 | * var radio;
602 | *
603 | * function setup() {
604 | * radio = createRadio();
605 | * radio.option("black");
606 | * radio.option("white");
607 | * radio.option("gray");
608 | * radio.style('width', '60px');
609 | * textAlign(CENTER);
610 | * fill(255, 0, 0);
611 | * }
612 | *
613 | * function draw() {
614 | * var val = radio.value();
615 | * background(val);
616 | * text(val, width/2, height/2);
617 | * }
618 | *
620 | * var radio;
621 | *
622 | * function setup() {
623 | * radio = createRadio();
624 | * radio.option('apple', 1);
625 | * radio.option('bread', 2);
626 | * radio.option('juice', 3);
627 | * radio.style('width', '60px');
628 | * textAlign(CENTER);
629 | * }
630 | *
631 | * function draw() {
632 | * background(200);
633 | * var val = radio.value();
634 | * if (val) {
635 | * text('item cost is $'+val, width/2, height/2);
636 | * }
637 | * }
638 | *
729 | * function setup(){
730 | * var inp = createInput('');
731 | * inp.input(myInputEvent);
732 | * }
733 | *
734 | * function myInputEvent(){
735 | * console.log('you are typing: ', this.value());
736 | * }
737 | *
738 | * Creates a new <video> element that contains the audio/video feed 935 | * from a webcam. This can be drawn onto the canvas using video().
936 | *More specific properties of the feed can be passing in a Constraints object. 937 | * See the 938 | * W3C 939 | * spec for possible properties. Note that not all of these are supported 940 | * by all browsers.
941 | *Security note: A new browser security specification requires that getUserMedia, 942 | * which is behind createCapture(), only works when you're running the code locally, 943 | * or on HTTPS. Learn more here 944 | * and here.
945 | * 946 | * @method createCapture 947 | * @param {String|Constant|Object} type type of capture, either VIDEO or 948 | * AUDIO if none specified, default both, 949 | * or a Constraints object 950 | * @param {Function} callback function to be called once 951 | * stream has loaded 952 | * @return {Object|p5.Element} capture video p5.Element 953 | * @example 954 | *
955 | * var capture;
956 | *
957 | * function setup() {
958 | * createCanvas(480, 120);
959 | * capture = createCapture(VIDEO);
960 | * }
961 | *
962 | * function draw() {
963 | * image(capture, 0, 0, width, width*capture.height/capture.width);
964 | * filter(INVERT);
965 | * }
966 | *
968 | * function setup() {
969 | * createCanvas(480, 120);
970 | * var constraints = {
971 | * video: {
972 | * mandatory: {
973 | * minWidth: 1280,
974 | * minHeight: 720
975 | * },
976 | * optional: [
977 | * { maxFrameRate: 10 }
978 | * ]
979 | * },
980 | * audio: true
981 | * };
982 | * createCapture(constraints, function(stream) {
983 | * console.log(stream);
984 | * });
985 | * }
986 | *
1049 | * var h2 = createElement('h2','im an h2 p5.element!');
1050 | *
1051 | */
1052 | p5.prototype.createElement = function(tag, content) {
1053 | var elt = document.createElement(tag);
1054 | if (typeof content !== 'undefined') {
1055 | elt.innerHTML = content;
1056 | }
1057 | return addElement(elt, this);
1058 | };
1059 |
1060 |
1061 | // =============================================================================
1062 | // p5.Element additions
1063 | // =============================================================================
1064 | /**
1065 | *
1066 | * Adds specified class to the element.
1067 | *
1068 | * @for p5.Element
1069 | * @method addClass
1070 | * @param {String} class name of class to add
1071 | * @return {Object|p5.Element}
1072 | * @example
1073 | *
1074 | * var div = createDiv('div');
1075 | * div.addClass('myClass');
1076 | *
1118 | * var div0 = createDiv('this is the parent');
1119 | * var div1 = createDiv('this is the child');
1120 | * div0.child(div1); // use p5.Element
1121 | *
1123 | * var div0 = createDiv('this is the parent');
1124 | * var div1 = createDiv('this is the child');
1125 | * div1.id('apples');
1126 | * div0.child('apples'); // use id
1127 | *
1129 | * var div0 = createDiv('this is the parent');
1130 | * var elt = document.getElementById('myChildDiv');
1131 | * div0.child(elt); // use element from page
1132 | *
1160 | * function setup() {
1161 | * var div = createDiv('').size(10,10);
1162 | * div.style('background-color','orange');
1163 | * div.center();
1164 | *
1165 | * }
1166 | *
1218 | * var div = createDiv('').size(100,100);
1219 | * div.html('hi');
1220 | *
1222 | * var div = createDiv('Hello ').size(100,100);
1223 | * div.html('World', true);
1224 | *
1251 | * function setup() {
1252 | * var cnv = createCanvas(100, 100);
1253 | * // positions canvas 50px to the right and 100px
1254 | * // below upper left corner of the window
1255 | * cnv.position(50, 100);
1256 | * }
1257 | *
1335 | * var myDiv = createDiv("I like pandas.");
1336 | * myDiv.style("font-size", "18px");
1337 | * myDiv.style("color", "#ff0000");
1338 | *
1340 | * var col = color(25,23,200,50);
1341 | * var button = createButton("button");
1342 | * button.style("background-color", col);
1343 | * button.position(10, 10);
1344 | *
1346 | * var myDiv = createDiv("I like lizards.");
1347 | * myDiv.style("position", 20, 20);
1348 | * myDiv.style("rotate", 45);
1349 | *
1351 | * var myDiv;
1352 | * function setup() {
1353 | * background(200);
1354 | * myDiv = createDiv("I like gray.");
1355 | * myDiv.position(20, 20);
1356 | * }
1357 | *
1358 | * function draw() {
1359 | * myDiv.style("font-size", mouseX+"px");
1360 | * }
1361 | *
1414 | * var myDiv = createDiv("I like pandas.");
1415 | * myDiv.attribute("align", "center");
1416 | *
1438 | * var button;
1439 | * var checkbox;
1440 | *
1441 | * function setup() {
1442 | * checkbox = createCheckbox('enable', true);
1443 | * checkbox.changed(enableButton);
1444 | * button = createButton('button');
1445 | * button.position(10, 10);
1446 | * }
1447 | *
1448 | * function enableButton() {
1449 | * if( this.checked() ) {
1450 | * // Re-enable the button
1451 | * button.removeAttribute('disabled');
1452 | * } else {
1453 | * // Disable the button
1454 | * button.attribute('disabled','');
1455 | * }
1456 | * }
1457 | *
1474 | * // gets the value
1475 | * var inp;
1476 | * function setup() {
1477 | * inp = createInput('');
1478 | * }
1479 | *
1480 | * function mousePressed() {
1481 | * print(inp.value());
1482 | * }
1483 | *
1485 | * // sets the value
1486 | * var inp;
1487 | * function setup() {
1488 | * inp = createInput('myValue');
1489 | * }
1490 | *
1491 | * function mousePressed() {
1492 | * inp.value("myValue");
1493 | * }
1494 | *
1516 | * var div = createDiv('div');
1517 | * div.style("display", "none");
1518 | * div.show(); // turns display to block
1519 | *
1533 | * var div = createDiv('this is a div');
1534 | * div.hide();
1535 | *
1554 | * var div = createDiv('this is a div');
1555 | * div.size(100, 100);
1556 | *
1613 | * var myDiv = createDiv('this is some text');
1614 | * myDiv.remove();
1615 | *
1895 | * function setup() {
1896 | * audioEl = createAudio('assets/beat.mp3');
1897 | * audioEl.showControls(true);
1898 | * audioEl.onended(sayDone);
1899 | * }
1900 | *
1901 | * function sayDone(elt) {
1902 | * alert('done playing ' + elt.src );
1903 | * }
1904 | *
2035 | * function setup() {
2036 | * background(255,255,255);
2037 | *
2038 | * audioEl = createAudio('assets/beat.mp3');
2039 | * audioEl.showControls();
2040 | *
2041 | * // schedule three calls to changeBackground
2042 | * audioEl.addCue(0.5, changeBackground, color(255,0,0) );
2043 | * audioEl.addCue(1.0, changeBackground, color(0,255,0) );
2044 | * audioEl.addCue(2.5, changeBackground, color(0,0,255) );
2045 | * audioEl.addCue(3.0, changeBackground, color(0,255,255) );
2046 | * audioEl.addCue(4.2, changeBackground, color(255,255,0) );
2047 | * audioEl.addCue(5.0, changeBackground, color(255,255,0) );
2048 | * }
2049 | *
2050 | * function changeBackground(val) {
2051 | * background(val);
2052 | * }
2053 | *