-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
130 lines (113 loc) · 2.58 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html>
<head>
<title>Pac - Man</title>
<style>
body
{
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>
<script>
/**window.requestAnimFrame = (function(callback)
{
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback)
{
window.setInterval(callback, 1000 / 60);
};
})();*/
var Pacman = function(pArea)
{
function Pacman(pArea)
{
this.area = pArea;
this.x = 21;
this.y = 21;
this.animation = 0;
this.sides =
{
right :
{
start : 0.25,
end : 1.75
},
up :
{
start : 1.75,
end : 1.25
},
down :
{
start : 0.75,
end : 0.25
},
left:
{
start: 1.25,
end: 0.75
}
};
this.face = JSON.parse(JSON.stringify(this.sides.right));
this.move = function(e)
{
if (e.keyCode == 37)//left
{
this.face = JSON.parse(JSON.stringify(this.sides.left));
if (this.x - 14 > 0)
this.x -= 1;
}
else if (e.keyCode == 38)//up
{
this.face = JSON.parse(JSON.stringify(this.sides.up));
if (this.y - 14 > 0)
this.y -= 1;
}
else if (e.keyCode == 39)//right
{
this.face = JSON.parse(JSON.stringify(this.sides.right));
if (this.x + 14 < this.area.width)
this.x += 1;
}
else if (e.keyCode == 40)//down
{
this.face = JSON.parse(JSON.stringify(this.sides.down));
if (this.y + 14 < this.area.height)
this.y += 1;
}
}
this.draw = function()
{
var ctx = this.area.getContext('2d');
ctx.clearRect(0, 0, this.area.width, this.area.height);
ctx.fillStyle = "rgb(0,0,0)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = "rgb(255, 255, 0)";
if (this.animation >= 0.6)
this.animation = 0;
ctx.arc(this.x, this.y, 20, Math.PI * this.face.start - this.animation, Math.PI * this.face.end + this.animation, false);
this.animation += 0.15;
ctx.lineTo(this.x, this.y);
ctx.fill();
}
}
return new Pacman(pArea);
}
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var game = new Pacman(canvas);
game.draw();
function keyDown(e)
{
game.move(e);
game.draw();
}
window.addEventListener("keydown", keyDown, false);
</script>
</html>