Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will make a simple JavaScript game that will break the canvas up into 64X64

ID: 3877084 • Letter: Y

Question

You will make a simple JavaScript game that will break the canvas up into 64X64 pixel squares.   You will use a 2D array to dynamically draw a simple maze consisting of bricks and trees. The player will push 1 of four buttons moving their character in that direction one square. If the player runs into a monster the player will attack the monster (But not move to the square).   After the player has moved all of the monsters will move, if the monster moves on the player it will attack the player (but not move to the square). Monsters and Players cannot move through trees or walls. There will be at least 2 potions on the map that will restore the player’s health if they move on to them. (The potion will then be destroyed after it is collected). If all the monsters have died then print “You Win” on the screen. If the player is killed, then you will print “Game Over”.   Both the monsters and the hero will have the following properties:

·         HP – Health

·         MHP – Max health

·         ATK – Attack bonus

·         DEF – Defense bonus

·         X – X coordinate (on the grid not the pixel value)

·         Y – Y coordinate (on the grid not the pixel value)

·         PIC – The variable pointing to the html image for the appropriate character.

Both the player and the monster will be rendered by images, they will also have a health bar next to them. (You pick the orientation).   The images will be rendered on the appropriate grid based on the X and Y value.   Since both the hero and the monster use the same render function you may want to use inheritance. For combat you will follow the equation: Damage = (other.ATK-this.DEF or 1 whichever is higher)*random number 1-6. Again this could be implemented in a parent class. You will have three monsters with varying difficulty and one hero.   I will let you decide what the attributes should be. The game should be challenging but not impossible. Submit a zipped folder with all your files inside it. The tree and the rock/wall will be rendered through the 2D context draw functions.

Explanation / Answer

java script:

var cols, rows;
var scl = 40;
var grid = [];
var stack = [];

var active;
var player;
var finish;

var highlightShow = true;

function setup() {
createCanvas(1000, 600);
background(237, 34, 93);
cols = floor(width/scl);
rows = floor(height/scl);
  
for (var j = 0; j < rows; j++) {
for (var i = 0; i < cols; i++) {
var cell = new Cell(i,j);
grid.push(cell);
}
}
active = grid[0];
player = grid[0];
finish = grid[index(14,14)];
}

function index(i, j) {
if (i < 0 || j < 0 || i > cols-1 || j > cols -1) {
return -1;
}
return i + j * cols;
}

function draw() {
for (var i = 0; i < grid.length; i++) {
grid[i].show();
}
  
player.visible();
finish.visible();
  
  
active.checked = true;
active.highlight();
var next = active.checkN();
if (next) {
next.checked = true;
removeLine(active,next);
active = next;
stack.push(active);
}
}

function Cell(i,j) {
this.i = i;
this.j = j;
this.walls = [true, true, true, true] // top right bottom left
this.checked = false;
  
this.show = function() {
var x = this.i*scl;
var y = this.j*scl;
  
stroke(255);
  
if (this.walls[0]) {line(x ,y ,x+scl,y );} // top
if (this.walls[1]) {line(x+scl,y ,x+scl,y+scl);} // right
if (this.walls[2]) {line(x ,y+scl,x+scl,y+scl);} // bottom
if (this.walls[3]) {line(x ,y ,x ,y+scl);} // left
  
if (this.checked) {
noStroke();
fill(60, 50, 170);
rect(x,y,scl,scl);
}
}
  
this.checkN = function() {
var n = []
  
var top = grid[index(i , j-1)];
var right = grid[index(i+1, j )];
var bottom = grid[index(i , j+1)];
var left = grid[index(i-1, j )];
  
if (top && !top.checked ) {n.push(top );}
if (right && !right.checked ) {n.push(right );}
if (bottom && !bottom.checked) {n.push(bottom);}
if (left && !left.checked ) {n.push(left );}
  
if (n.length > 0) {
var r = floor(random(0,n.length));
return n[r];
}else{
back();
}
}
  
this.highlight = function() {
if (highlightShow) {
var x = this.i*scl;
var y = this.j*scl;

noStroke();  
fill(20, 240, 30,100);
rect(x,y,scl,scl);
}
}
  
this.visible = function() {
if (allChecked()) {
var x = this.i*scl;
var y = this.j*scl;

noStroke();
fill(20, 240, 30);
rect(x+5,y+5,scl-10,scl-10);
}
}
}

back = function() {
if (!allChecked()) {
stack.pop();
active = stack[stack.length-1];
}else{
highlightShow = false;
}
}

allChecked = function() {
var finished = true;
for (var i = 0; i < grid.length-1; i++) {
if (!grid[i].checked) {
finished = false;
}
}
if (finished) {return true;}else{return false;}
}

removeLine = function(a, n) {
var x = a.i - n.i;
var y = a.j - n.j;
console.log (x + " " + y);
if (y === 1) {a.walls[0] = false; n.walls[2] = false;}else // top
if (x === -1 ) {a.walls[1] = false; n.walls[3] = false;}else // right
if (y === -1 ) {a.walls[2] = false; n.walls[0] = false;}else // bottom
if (x === 1) {a.walls[3] = false; n.walls[1] = false;} // left
}

function keyTyped() {
if (key === 'w' && !player.walls[0]) {
player = grid[index(player.i, player.j-1)];
console.log("W");
}else if (key === 'd' && !player.walls[1]) {
player = grid[index(player.i+1, player.j)];
console.log("D");
}else if (key === 's' && !player.walls[2]) {
player = grid[index(player.i, player.j+1)];
console.log("S");
}else if (key === 'a' && !player.walls[3]) {
player = grid[index(player.i-1, player.j)];
console.log("A");
}
  
if (player === finish) {
reset();
}
}

reset = function() {
grid = []
background(237, 34, 93);
  
for (var j = 0; j < rows; j++) {
for (var i = 0; i < cols; i++) {
var cell = new Cell(i,j);
grid.push(cell);
}
}
  
active = grid[0];
player = grid[0];
finish = grid[index(14,14)];
}

HTML CODE :

<head>
<script language="javascript" type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.2.13/p5.min.js"></script>
</head>

<body>
</body>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote