File size: 6,652 Bytes
7ee1649
5021382
 
 
 
a6be460
5021382
 
 
 
a6be460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5021382
 
a6be460
5021382
 
 
a6be460
 
 
5021382
 
a6be460
 
 
 
5021382
 
 
 
 
a6be460
5021382
 
a6be460
 
 
 
 
5021382
 
 
 
a6be460
 
5021382
a6be460
 
 
 
5021382
a6be460
5021382
a6be460
 
 
 
 
 
5021382
a6be460
 
 
 
 
 
5021382
a6be460
 
 
 
5021382
a6be460
5021382
a6be460
 
5021382
a6be460
 
 
 
 
 
 
5021382
a6be460
 
 
5021382
a6be460
5021382
a6be460
 
 
 
 
 
5021382
 
 
 
a6be460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5021382
a6be460
5021382
a6be460
 
5021382
a6be460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5021382
a6be460
 
 
 
 
 
 
5021382
a6be460
5021382
a6be460
 
 
 
5021382
a6be460
5021382
a6be460
 
 
 
5021382
 
a6be460
 
 
5021382
a6be460
 
 
 
 
 
 
 
 
 
 
 
5021382
 
a6be460
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Word Attack — Vocabulary Battle</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>

<style>
:root{
  --bg:#05050f;--panel:rgba(0,0,0,.45);--accent:#b71c1c;
  --text:#f5f5f5;--muted:#bfc6cc;
}
html,body{margin:0;height:100%;background:var(--bg);color:var(--text);font-family:Inter,system-ui;overflow:hidden}
button{background:var(--accent);border:0;color:#fff;padding:8px 12px;border-radius:8px;font-weight:600;cursor:pointer}
button.secondary{background:transparent;border:1px solid rgba(255,255,255,.08)}
.ui{position:fixed;left:12px;right:12px;top:12px;display:flex;justify-content:space-between;z-index:10}
.panel{background:var(--panel);padding:8px 12px;border-radius:8px;display:flex;gap:12px;align-items:center}
#questionBox{position:fixed;top:84px;left:50%;transform:translateX(-50%);
background:rgba(255,255,255,.05);padding:8px 16px;border-radius:8px;font-weight:700}
canvas{width:100vw;height:100vh}
.overlay{position:fixed;inset:0;background:rgba(0,0,0,.7);
display:flex;align-items:center;justify-content:center;opacity:0;pointer-events:none}
.overlay.show{opacity:1;pointer-events:auto}
.overlay-panel{background:#000;padding:24px;border-radius:12px;text-align:center}
</style>
</head>

<body>

<div class="ui">
  <div class="panel">
    <div>Score: <span id="score">0</span></div>
    <div>Lives: <span id="lives">3</span></div>
  </div>
  <div class="panel">
    <label>
      <input type="file" id="fileInput" accept=".xlsx" hidden>
      <span style="cursor:pointer">Upload Excel</span>
    </label>
    <button id="startBtn">Start</button>
    <button id="pauseBtn" class="secondary">Pause</button>
  </div>
</div>

<div id="questionBox">Word: <span id="currentWord">—</span></div>
<canvas id="game"></canvas>

<div id="overlay" class="overlay">
  <div class="overlay-panel">
    <h1>GAME OVER</h1>
    <p>Final Score: <span id="finalScore"></span></p>
    <button id="restartBtn">Restart</button>
  </div>
</div>

<script>
/* ---------------- CONFIG ---------------- */
const CONFIG = { lives:3, options:4 };

/* ---------------- DATA ---------------- */
const FALLBACK = [
 ['vivid','bright'],['fragile','easily broken'],
 ['orbit','path around a body'],['gravity','pulling force']
];
let pairs=[...FALLBACK], currentIndex=-1;

/* ---------------- STATE ---------------- */
let score=0,lives=CONFIG.lives,state='stopped';
let options=[], lasers=[], stars=[];
const canvas=document.getElementById('game');
const ctx=canvas.getContext('2d');
resize(); window.addEventListener('resize',resize);

/* ---------------- UI ---------------- */
const scoreEl=scoreSpan=document.getElementById('score');
const livesEl=document.getElementById('lives');
const wordEl=document.getElementById('currentWord');
const overlay=document.getElementById('overlay');
const finalScore=document.getElementById('finalScore');

/* ---------------- HELPERS ---------------- */
function resize(){
  canvas.width=window.innerWidth;
  canvas.height=window.innerHeight;
}
function rand(a,b){return a+Math.random()*(b-a);}

/* ---------------- STARS ---------------- */
for(let i=0;i<100;i++)stars.push({x:Math.random()*canvas.width,y:Math.random()*canvas.height,s:Math.random()*2});

/* ---------------- GAME FLOW ---------------- */
function nextRound(){
  currentIndex=Math.floor(Math.random()*pairs.length);
  const [word,correct]=pairs[currentIndex];
  wordEl.textContent=word;
  options=[];
  ctx.font='16px Inter, Arial';

  const pool=new Set([correct]);
  while(pool.size<CONFIG.options){
    pool.add(pairs[Math.floor(Math.random()*pairs.length)][1]);
  }
  [...pool].sort(()=>Math.random()-0.5).forEach(t=>{
    options.push({
      text:t,isCorrect:t===correct,
      x:rand(120,canvas.width-120),
      y:rand(-120,-40),
      vy:rand(40,80),
      w:Math.max(140,ctx.measureText(t).width+30),
      h:32,resolved:false
    });
  });
}

/* ---------------- INPUT ---------------- */
canvas.addEventListener('click',e=>{
  if(state!=='running')return;
  const r=canvas.getBoundingClientRect();
  const mx=e.clientX-r.left,my=e.clientY-r.top;
  const hit=options.find(o=>
    mx>o.x-o.w/2&&mx<o.x+o.w/2&&
    my>o.y-o.h/2&&my<o.y+o.h/2
  );
  if(!hit||hit.resolved)return;
  hit.resolved=true;
  lasers.push({sx:canvas.width/2,sy:canvas.height-60,tx:hit.x,ty:hit.y,a:0});

  if(hit.isCorrect){
    score+=20;scoreEl.textContent=score;
    setTimeout(nextRound,250);
  }else{
    lives--;livesEl.textContent=lives;
    options=options.filter(o=>o!==hit);
    if(lives<=0)gameOver();
  }
});

/* ---------------- LOOP ---------------- */
function loop(){
  requestAnimationFrame(loop);
  if(state!=='running')return;
  ctx.fillStyle='#03030a';ctx.fillRect(0,0,canvas.width,canvas.height);

  stars.forEach(s=>{s.y+=s.s;if(s.y>canvas.height)s.y=0;
    ctx.fillStyle='#fff';ctx.fillRect(s.x,s.y,2,2);});

  ctx.font='16px Inter, Arial';
  options.forEach(o=>{
    o.y+=o.vy*0.016;
    ctx.fillStyle='rgba(255,255,255,.06)';
    ctx.fillRect(o.x-o.w/2,o.y-o.h/2,o.w,o.h);
    ctx.fillStyle='#fff';ctx.textAlign='center';ctx.textBaseline='middle';
    ctx.fillText(o.text,o.x,o.y);
    if(o.isCorrect&&o.y>canvas.height){
      lives--;livesEl.textContent=lives;
      nextRound();
    }
  });

  lasers.forEach(l=>{
    l.a+=0.1;
    ctx.strokeStyle='rgba(255,80,80,.8)';
    ctx.lineWidth=4;
    ctx.beginPath();ctx.moveTo(l.sx,l.sy);ctx.lineTo(l.tx,l.ty);ctx.stroke();
  });
  lasers=lasers.filter(l=>l.a<1);
}
loop();

/* ---------------- CONTROL ---------------- */
function start(){
  score=0;lives=CONFIG.lives;
  scoreEl.textContent=score;livesEl.textContent=lives;
  overlay.classList.remove('show');
  state='running';nextRound();
}
function gameOver(){
  state='stopped';
  finalScore.textContent=score;
  overlay.classList.add('show');
}

document.getElementById('startBtn').onclick=start;
document.getElementById('restartBtn').onclick=start;
document.getElementById('pauseBtn').onclick=()=>state=state==='running'?'paused':'running';

/* ---------------- EXCEL ---------------- */
document.getElementById('fileInput').onchange=e=>{
  const f=e.target.files[0];if(!f)return;
  const r=new FileReader();
  r.onload=ev=>{
    const wb=XLSX.read(new Uint8Array(ev.target.result),{type:'array'});
    const rows=XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]],{header:1});
    pairs=rows.filter(r=>r[0]&&r[1]).map(r=>[String(r[0]),String(r[1])]);
    alert(`Loaded ${pairs.length} words`);
  };
  r.readAsArrayBuffer(f);
};
</script>
</body>
</html>