mardi 1 septembre 2020

Javascript Grid Gameboard

So Im trying to make like a gameboard on a 10 by 10 grid where the upper left hand corner is position (0,0) and the bottom right is position (9,9). The properties of it are name (String), livesLeft (integer), score (integer), speed (integer), gridLocationX (integer), and gridLocationY. X is left to right and Y is up and down. The methods are die() moveLeft(), moveRight(), moveUp(), and moveDown(). For example, moveRight() would move the player to (1,0) starting from (0,0). Here is my Javascript file, I know I'm supposed to run a test case of the player function on an html file (also not using CSS for this). Any help or advice is appreciated. Thank you.

Code:

var Player = function(name, livesLeft, score, speed){
this.name=name;
this.livesLeft=livesLeft;
this.score=score;
this.speed=speed;
this.gridLocationX=0;
this.gridLocationY=0;
var board = new Array(10);
// rows=0;
// cols=0;
for (i=1,i<=board.length,i++){
    for(j=1,j<=board.length,j++){
        document.write(board); //know this is very wrong lol
        
    }
        document.write("<br/>");              
}
//Methods: die(), moveLeft(), moveRight(), moveUp(), moveDown().
 moveUp(){
    
    if (this.gridLocationX > 0) { 
        this.GridLocationX += 1; }
    if (this.gridLocationY > 0) { 
        this.GridLocationY =- 1; }
}
moveDown(){ 
    if (this.gridLocationY > 0) { 
        this.GridLocationY -= 1; }
    if (this.gridLocationX > 9) { 
        this.GridLocationX =- 1; }
}

moveLeft(){
     if (this.gridLocationX > 0) { 
        this.GridLocationX += 1; }
     if (this.gridLocationX < 0) { 
        this.GridLocationX =- 1; }
moveRight(){
    if (this.gridLocationY > 0) { 
        this.GridLocationY += 1; } 
    if (this.gridLocationY > 9) { 
        this.GridLocationY =- 1; }
}
die(){
    if (this.gridLocationX < 0 || this.gridLocationX>9) { 
        this.GridLocationX =- 1; }
    if (this.gridLocationY < 0 || this.gridLocationY>9) { 
        this.GridLocationY =- 1; }
}

} Some more info and picture of grid

Aucun commentaire:

Enregistrer un commentaire