July 14, 2019

Moving Robot Html Javascript code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>AI Robot</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
    border:1px solid #d3d3d3;
    background-color: #f1f1f1;
}
</style>
</head>
<body onload="startGame()">
<script>

var robotHead;
var robotBody;
var robotLegOne;
var robotLegTwo;
var robotHandOne;
var robotHandTwo;

function startGame() {
robotHead = new robotHeadComp(100, 100, "#8ED6FF", 50, 50);
robotBody = new robotBodyComp(100, 200, "blue", 50, 150);
robotLegOne = new robotBodyComp(25, 200, "blue", 50, 250);
robotLegTwo = new robotBodyComp(25, 200, "blue", 125, 250);
robotHandOne = new robotBodyComp(25, 25, "blue", 150, 150);
robotHandTwo = new robotBodyComp(25, 25, "blue", 25, 150);
    myGameArea.start();
}

var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 900;
        this.canvas.height = 500;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function robotHeadComp(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;   
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
        ctx.strokeStyle = "black";
        ctx.stroke();
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;       
    }   
}

function robotBodyComp(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;   
    this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;       
    }   
}

function updateGameArea() {
    myGameArea.clear();
    robotHead.newPos();   
    robotHead.update();
    robotBody.newPos();   
    robotBody.update();
    robotLegOne.newPos();   
    robotLegOne.update();
    robotLegTwo.newPos();   
    robotLegTwo.update();
    robotHandOne.newPos();   
    robotHandOne.update();
    robotHandTwo.newPos();   
    robotHandTwo.update();
}

function moveup() {
robotHead.speedY -= 1;
}

function movedown() {
robotHead.speedY += 1;
}

function moveleft() {
robotHead.speedX -= 1;
}

function moveright() {
robotHead.speedX += 1;
robotBody.speedX += 1;
robotLegOne.speedX += 1;
robotLegTwo.speedX += 1;
robotHandOne.speedX += 1;
robotHandTwo.speedX += 1;
}

function chargeBattery(){

}
</script>

<div style="text-align:center;width:480px;">
  <button onclick="chargeBattery()">charge Battery</button><br>
<!--   <button onclick="moveup()">UP</button><br><br> -->
<!--   <button onclick="moveleft()">LEFT</button> -->
  <button onclick="moveright()">Walk</button><br><br>
<!--   <button onclick="movedown()">DOWN</button> -->
</div>


</body>
</html>

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments