-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
205 lines (157 loc) · 4.77 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<HTML>
<HEAD>
<script type="text/javascript">
var gamePaused = true;
var score_hits = 0;
var lives_remaining = 5;
var bgMusic = true;
class Ghost {
constructor(myDivID) {
this.myDivID = myDivID;
this.reBorn()
}
move() {
this.xPos += this.xSpeed*this.xDirection;
this.yPos += this.ySpeed*this.yDirection;
if(this.xPos>window.innerWidth-100) {
this.xDirection = -1;
this.reSpeed();
}
if(this.xPos<0) {
this.xDirection = 1;
this.reSpeed();
}
if(this.yPos>window.innerHeight-100) {
this.yDirection = -1;
this.reSpeed();
}
if(this.yPos<0) {
this.yDirection = 1;
this.reSpeed();
}
var box = document.getElementById(this.myDivID);
box.style.left = this.xPos + "px";
box.style.top = this.yPos + "px";
if(this.rotate >0) {
box.style.transform = "rotate("+this.rotate+"deg)";
this.rotate -= 30
}
else
box.style.transform = "rotate(0deg)";
if(this.isAlive==false && this.yPos>window.innerHeight) {
this.reBorn()
}
}
reBorn() {
this.isAlive = true
this.rotate = 0
this.xPos = window.innerWidth * Math.random()
this.yPos = window.innerHeight * Math.random()
this.xDirection = 1
this.yDirection = 1
if(Math.random()<0.5)
this.xDirection = -1
if(Math.random()<0.5)
this.yDirection = -1
this.reSpeed()
}
dieOnTouch() {
this.isAlive = false;
this.yDirection = 1;
this.xSpeed = 0;
this.ySpeed = 50;
this.rotate = 1000;
}
reSpeed() {
this.xSpeed = 2+ Math.random()*5;
this.ySpeed = 2+ Math.random()*5;
}
}
Ghosts = [new Ghost("Ghost_1"),new Ghost("Ghost_2"),new Ghost("Ghost_3"),new Ghost("Ghost_4"),new Ghost("Ghost_5")];
document.addEventListener('click', Tap, true);
function Tap(e){
cursorX = e.pageX;
cursorY = e.pageY;
if(gamePaused==true)
return;
var anyGhostHit = false;
for(i=0; i<Ghosts.length; i++) {
if( cursorX>Ghosts[i].xPos && cursorX<Ghosts[i].xPos+100 &&
cursorY>Ghosts[i].yPos && cursorY<Ghosts[i].yPos+100) {
Ghosts[i].dieOnTouch();
var cryAudio = new Audio('pics/cry.mp3');
if(Math.random()<0.5)
cryAudio = new Audio('pics/cry2.mp3');
cryAudio.play();
score_hits += 10;
anyGhostHit = true;
}
}
if(anyGhostHit==false) {
var laughAudio = new Audio('pics/laugh.mp3');
if(Math.random()<0.5)
laughAudio = new Audio('pics/laugh2.mp3');
laughAudio.play();
lives_remaining--;
}
}
function game()
{
if(gamePaused!=true) {
for(i=0; i<Ghosts.length; i++) {
Ghosts[i].move();
}
}
var score = document.getElementById('score');
score.innerHTML = " Point: "+score_hits + ", Lives: ";
for(i=0; i<lives_remaining; i++){
score.innerHTML += " <img src = pics/life.png height=20 style = \"margin-bottom: -4px;\"> ";
}
if(lives_remaining>0)
setTimeout("game()", 30)
else
alert('Game Over!');
}
function startGame()
{
gamePaused = false;
document.getElementById('playButton').style.display='none';
new Audio('pics/bg.mp3').play();
}
function pauseGame()
{
var pauseButtonPic = document.getElementById('pauseButtonPic');
if(gamePaused == false) {
gamePaused = true;
pauseButtonPic.src = "pics/play.png";
}
else {
gamePaused = false;
pauseButtonPic.src = "pics/pause.png";
}
}
</script>
<style type="text/css">
.ghost {
border: 0px solid red; width: 100px; height: 100px;
position: absolute; top: 0px; left: 0px;
}
</style>
</HEAD>
<BODY ONLOAD = "game()" style = "margin-left: 0px; margin-top: 0px; background: url('./pics/bg.jpg'); ">
<DIV>
<DIV ID = "Ghost_1" CLASS = "ghost"><IMG SRC = "pics/ghost2.gif" HEIGHT = 100></DIV>
<DIV ID = "Ghost_2" CLASS = "ghost"><IMG SRC = "pics/ghost2.gif" HEIGHT = 100></DIV>
<DIV ID = "Ghost_3" CLASS = "ghost"><IMG SRC = "pics/ghost2.gif" HEIGHT = 100></DIV>
<DIV ID = "Ghost_4" CLASS = "ghost"><IMG SRC = "pics/ghost2.gif" HEIGHT = 100></DIV>
<DIV ID = "Ghost_5" CLASS = "ghost"><IMG SRC = "pics/ghost2.gif" HEIGHT = 100></DIV>
</DIV>
<DIV ID = score STYLE = "background: #FF0; padding: 4px; font-weight: strong; position: absolute; top: 0px; left: 0px;"> </DIV>
<DIV ID = playButton STYLE = "top: 0px; left: 0px; text-align: center; width: 100%;height:100%; display:block; border: 0px solid red;">
<img src = "pics/play.png" style=" margin: 0; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);" ONCLICK = "startGame();">
</DIV>
<DIV>
<img ID = pauseButtonPic src = "pics/pause.png" ONCLICK = "pauseGame();" align = right width = 100 style="padding: 10px; margin: 20px;">
</DIV>
</BODY>
</HTML>