-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript2.js
More file actions
167 lines (155 loc) · 5.38 KB
/
Copy pathscript2.js
File metadata and controls
167 lines (155 loc) · 5.38 KB
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
//parameters
var bullet_speed=500;
//
var root=document.querySelector(':root');
const shooter=document.getElementById("shooter");
const bullet=document.getElementById("bullet");
const score=document.getElementById("score");
const egg=document.getElementById("egg");
const egg_bullet=document.getElementById("egg-bullet");
const heart1=document.getElementById("heart1");
const heart2=document.getElementById("heart2");
const heart3=document.getElementById("heart3");
var bulletTop;
var bulletLeft;
var shooterTop;
var shooterLeft;
var eggTop;
var eggLeft;
var eggBulletLeft;
var eggBulletTop;
var heart=3;
//shooter function
function jump(){
shooter.classList.add("jump-animation");
setTimeout(() => {
shooter.classList.remove("jump-animation");
}, 500);
}
function left_move(){
shooter.style.left=String(shooterLeft-5)+"px";
}
function right_move(){
shooter.style.left=String(shooterLeft+5)+"px";
}
//egg function
function egg_jump(){
egg.classList.add("jump-animation");
setTimeout(() => {
egg.classList.remove("jump-animation");
}, 500);
}
//bullet function
function shoot(){
//determine bullet position by shooter position
parse_positions();
root.style.setProperty("--bullet_left_start",String(shooterLeft+20)+"px");
root.style.setProperty("--bullet_left_end",String(shooterLeft+800+20)+"px");
root.style.setProperty("--bullet_top_start",String(shooterTop)+"px");
root.style.setProperty("--bullet_top_end",String(shooterTop)+"px");
//add bullet fly animation
bullet.classList.add("shoot-animation");
setTimeout(()=>{
bullet.classList.remove("shoot-animation");
},500);
}
//egg bullet function
function egg_shoot(){
//determine bullet position by shooter position
parse_positions();
root.style.setProperty("--egg_left_start",String(eggLeft-20)+"px");
root.style.setProperty("--egg_left_end",String(eggLeft-800-20)+"px");
root.style.setProperty("--egg_top_start",String(eggTop)+"px");
root.style.setProperty("--egg_top_end",String(eggTop)+"px");
//add bullet fly animation
egg_bullet.classList.add("egg-shoot-animation");
setTimeout(()=>{
egg_bullet.classList.remove("egg-shoot-animation");
},500);
}
//collision function
function egg_break(eggT,eggL,buT,buL){
if(buT>eggT-10 && buT<(eggT+75-10) && buL>eggL && buL<(eggL+75) && !egg.classList.contains("egg-break")){
//window.alert("egg break");
egg.classList.add("egg-break");
setTimeout(()=>{
egg.classList.remove("egg-break");
score.innerHTML++;
},500);
}
}
function shooter_damage(shT,shL,buT,buL){
if(buT>shT-10 && buT<(shT+75-10) && buL>shL && buL<(shL+75)){
//window.alert("shooter damage!");
heart--;
}
}
//parse posistion function
function parse_positions(){
bulletTop=parseInt(window.getComputedStyle(bullet).getPropertyValue("Top"));
bulletLeft=parseInt(window.getComputedStyle(bullet).getPropertyValue("Left"));
shooterTop=parseInt(window.getComputedStyle(shooter).getPropertyValue("Top"));
shooterLeft=parseInt(window.getComputedStyle(shooter).getPropertyValue("Left"));
eggTop=parseInt(window.getComputedStyle(egg).getPropertyValue("Top"));
eggLeft=parseInt(window.getComputedStyle(egg).getPropertyValue("Left"));
eggBulletTop=parseInt(window.getComputedStyle(egg_bullet).getPropertyValue("Top"));
eggBulletLeft=parseInt(window.getComputedStyle(egg_bullet).getPropertyValue("Left"));
}
//set interval
setInterval(()=>{
//parse position for objects
parse_positions();
//detect collision between egg and bullet
egg_break(eggTop,eggLeft,bulletTop,bulletLeft);
//detect collision between shooter and egg bullet
shooter_damage(shooterTop,shooterLeft,eggBulletTop,eggBulletLeft);
//shooter heart display
if(heart==2)heart3.style.visibility="hidden";
if(heart==1)heart2.style.visibility="hidden";
if(heart==0){
heart1.style.visibility="hidden";
window.alert("game over!");
heart++;//temporary
}
},20);
//keyboard eventlistener
document.addEventListener('keypress',(event)=>{
//bullet
if(event.code=="KeyS" && !bullet.classList.contains('shoot-animation')){
/*!bullet.classList.contains('shoot-animation')*/
/*window.alert("shoot");*/
shoot();
}
//shooter
else if(event.code=="KeyW" && !shooter.classList.contains('jump-animation')){
jump();
}
else if(event.code=="KeyA" && !shooter.classList.contains('jump-animation')){
left_move();
}
else if(event.code=="KeyD" && !shooter.classList.contains('jump-animation')){
right_move();
}
//egg_bullet
else if(event.code=="KeyK" && !egg_bullet.classList.contains('egg-shoot-animation')){
/*!bullet.classList.contains('shoot-animation')*/
/*window.alert("shoot");*/
egg_shoot();
}
//egg
else if(event.code=="KeyI" && !egg.classList.contains('jump-animation')){
egg_jump();
}
//other function
else if(event.code=="KeyP"){
parse_positions();
s="bulletTop "+String(bulletTop)+"\n"+"eggTop "+String(eggTop)+"\n"+"bulletLeft "+String(bulletLeft)+"\n"+"eggLeft "+String(eggLeft);
//window.alert();
window.alert(s);
//window.alert("bulletLeft "+String(bulletLeft));
//window.alert("eggLeft "+String(eggLeft));
}
else{
//window.alert(event.code);
}
});