Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myGameArea = {
- canvas : document.createElement("canvas"),
- start : function() {
- this.canvas.width = 600;
- this.canvas.height = 480;
- //this.canvas.style.cursor = "none"; //hide the original cursor
- this.context = this.canvas.getContext("2d");
- document.body.insertBefore(this.canvas, document.body.childNodes[0]);
- this.interval = setInterval(updateGameArea, 1);
- window.addEventListener('mousedown', function (e) {
- myGameArea.x = e.pageX;
- myGameArea.y = e.pageY;
- console.log("hello");
- });
- window.addEventListener('mouseup', function (e) {
- myGameArea.x = false;
- myGameArea.y = false;
- });
- },
- clear : function(){
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
- }
- }
- function component(width, height, color, x, y, type) {
- this.type = type;
- this.width = width;
- this.height = height;
- this.speedX = 0;
- this.speedY = 0;
- this.x = x;
- this.y = y;
- this.update = function() {
- ctx = myGameArea.context;
- if (this.type == "text") {
- ctx.font = this.width + " " + this.height;
- ctx.fillStyle = color;
- ctx.fillText(this.text, this.x, this.y);
- } else {
- ctx.fillStyle = color;
- ctx.fillRect(this.x, this.y, this.width, this.height);
- }
- }
- this.clicked = function() {
- var myleft = this.x;
- var myright = this.x + (this.width);
- var mytop = this.y;
- var mybottom = this.y + (this.height);
- var clicked = true;
- if ((mybottom < myGameArea.y) || (mytop > myGameArea.y) || (myright < myGameArea.x) || (myleft > myGameArea.x)) {
- clicked = false;
- }
- return clicked;
- }
- }
- function updateGameArea() {
- myGameArea.clear();
- if (myGameArea.x && myGameArea.y) {
- if (item1.clicked()) {
- console.log("red square says hi");
- myScore = myScore + 1;
- }
- if (item2.clicked()) {
- console.log("orange square says hi");
- }
- if (item3.clicked()) {
- console.log("yellow square says hi");
- }
- if (item4.clicked()) {
- console.log("green square says hi");
- }
- if (item5.clicked()) {
- console.log("blue square says hi");
- }
- if (item6.clicked()) {
- console.log("purple square says hi");
- }
- }
- scoreDisplay.text = "Score: " + myScore;
- scoreDisplay.update();
- myGamePiece.update();
- item1.update();
- item2.update();
- item3.update();
- item4.update();
- item5.update();
- item6.update();
- itemArea.update();
- orderScreen.update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement