graph_editor
ρ=Vm
代码SmallAndWeak: 3c38dc9e-aea5-4cc4-8868-1b3b33a7b78d
294d36
HTML<!doctype html>
<html>
<head>
<title>弹球游戏</title>
<style>
#main {
position: relative;
width: 800px;
height: 600px;
background-color: #000;
}
.obj {
position: absolute;
}
.ball {
background-color: #ffd700;
border-radius: 50%;
}
.paddle {
background-color: #0ff;
}
.txt {
color: #fff;
font-size: 36px;
}
</style>
</head>
<body>
<div id="main"></div>
<script>
const main = document.getElementById('main');
function poom(a, b) {
const a_left = a.x, a_right = a.x + a.w, a_top = a.y, a_bottom = a.y + a.h;
const b_left = b.x, b_right = b.x + b.w, b_top = b.y, b_bottom = b.y + b.h;
return a_right >= b_left && a_left <= b_right && a_bottom >= b_top && a_top <= b_bottom;
}
class Ball {
constructor(x, y, wh, dx, dy) {
this.x = x;
this.y = y;
this.w = main.clientHeight * wh;
this.h = main.clientHeight * wh;
this.dx = main.clientHeight * dx;
this.dy = main.clientHeight * dy;
this.element = document.createElement('div');
this.element.className = 'obj ball';
this.element.style.width = `${this.w}px`;
this.element.style.height = `${this.h}px`;
main.appendChild(this.element);
}
update() {
this.x += this.dx;
this.y += this.dy;
if (this.x <= 0){
this.dx *= -1;
this.x = 0;
}
else if (this.x + this.w >= main.clientWidth){
this.dx *= -1;
this.x = main.clientWidth - this.w;
}
if (this.y <= 0){
this.dy *= -1;
}
if (poom(this, paddle) && this.dy > 0) {
this.dy *= -1.01;
let offset = (this.x + this.w / 2 - paddle.x - paddle.w / 2) / paddle.w * 2;
this.dx = offset * [-20, -10, 10, 20][Math.floor(Math.random() * 4)] + Math.random();
this.y = paddle.y - this.h;
score++;
}
if (this.y + this.h >= main.clientHeight)
running = false;
this.element.style.left = `${this.x}px`;
this.element.style.top = `${this.y}px`;
}
}
class Paddle {
constructor(x, y, w, h, spd) {
this.x = x;
this.y = y;
this.w = main.clientWidth * w;
this.h = main.clientHeight * h;
this.spd = main.clientWidth * spd;
this.element = document.createElement('div');
this.element.className = 'obj paddle';
this.element.style.width = `${this.w}px`;
this.element.style.height = `${this.h}px`;
main.appendChild(this.element);
}
update() {
if (presskey['ArrowDown'] && ball.y + ball.h + ball.dy > this.y) {
if (this.x > ball.x + ball.w + ball.dx)
this.x = ball.x + ball.w + ball.dx * 2;
if (this.x + this.w < ball.x + ball.dx)
this.x = ball.x - this.w + ball.dx * 2;
}
else {
if (presskey['ArrowLeft'] && this.x > 0)
this.x -= this.spd;
if (presskey['ArrowRight'] && this.x + this.w < main.clientWidth)
this.x += this.spd;
}
this.element.style.left = `${this.x}px`;
this.element.style.top = `${this.y}px`;
}
}
class Txt {
constructor(text, x, y) {
this.text = text;
this.x = x;
this.y = y;
this.element = document.createElement('div');
this.element.className = 'obj txt';
this.element.textContent = text;
main.appendChild(this.element);
}
show() {
this.element.style.left = `${this.x}px`;
this.element.style.top = `${this.y}px`;
}
}
let ball = new Ball(0, 0, 0, 0, 0), paddle = new Paddle(0, 0, 0, 0, 0), text = [], score, running;
const presskey = {};
function init() {
ball.element.remove();
paddle.element.remove();
ball = new Ball(main.clientWidth * 0.45, main.clientHeight * 0.05, 0.05, [-1/60, 1/60][Math.floor(Math.random() * 2)], 1/60);
paddle = new Paddle(main.clientWidth * 0.375, main.clientHeight * 0.967, 0.25, 0.025, 0.01);
score = 0;
running = true;
}
function update() {
text.forEach(i => i.element.remove());
text = [];
if (running) {
paddle.update();
ball.update();
}
else {
text.push(new Txt('游戏结束! 按空格键重新开始', main.clientWidth / 2 - 234, main.clientHeight / 2 - 18));
}
text.push(new Txt(`得分:${score},速度倍率:${Math.round(Math.abs(ball.dy) / main.clientHeight * 60 * 1000) / 1000}`, 20, 20));
text.forEach(i => i.show());
}
document.addEventListener('keydown', e => {
if (e.key === ' ' && running === false) {
init();
}
if (e.key === 's') {
main.requestFullscreen();
}
if (e.key === 'b') {
document.exitFullscreen();
}
presskey[e.key] = true;
});
document.addEventListener('keyup', e => {
delete presskey[e.key];
});
window.addEventListener('resize', () => {
init();
running = false;
});
init();
setInterval(update, 17);
</script>
</body>
</html>