forked from simran-toor/AthenaHackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-ignore.html
113 lines (103 loc) · 2.96 KB
/
game-ignore.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<head>
<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 playerGamePiece;
var teacherGamePiece;
function startGame() {
playerGamePiece = new component(10, 10, "./player.png", 120, 110, "image");
teacherGamePiece = new component(20, 20, "red", 10, 10);
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 250;
this.canvas.height = 130;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
window.addEventListener('keydown', function (e) {
myGameArea.key = e.keyCode;
})
window.addEventListener('keyup', function (e) {
myGameArea.key = false;
})
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
stop : function() {
clearInterval(this.interval);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
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 = myGameArea.context;
if (type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
} else {
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();
playerGamePiece.newPos();
playerGamePiece.update();
playerGamePiece.update();
teacherGamePiece.speedX = 1;
teacherGamePiece.speedY = 0;
teacherGamePiece.newPos();
teacherGamePiece.update();
}
function move(key) {
playerGamePiece.image.src = "./player.png";
playerGamePiece.speedX = 0;
playerGamePiece.speedY = 0;
if (myGameArea.key && myGameArea.key == 37) {playerGamePiece.speedX = -1; }
if (myGameArea.key && myGameArea.key == 39) {playerGamePiece.speedX = 1; }
if (myGameArea.key && myGameArea.key == 38) {playerGamePiece.speedY = -1; }
if (myGameArea.key && myGameArea.key == 40) {playerGamePiece.speedY = 1; }
}
function clearmove() {
playerGamePiece.image.src = "./player.png";
playerGamePiece.speedX = 0;
playerGamePiece.speedY = 0;
}
</script>
</body>
</html>