feat: Базовая логика движения мяча и столкновения со стенами

This commit is contained in:
Ilia Mashkov
2026-07-16 09:05:55 +03:00
parent 74fedc787b
commit a43d5c5ae2
2 changed files with 41 additions and 2 deletions
+4
View File
@@ -3,3 +3,7 @@ export const CONTAINER_HEIGHT = 600;
export const PADDLE_WIDTH = 50; export const PADDLE_WIDTH = 50;
export const PADDLE_HEIGHT = 10; export const PADDLE_HEIGHT = 10;
export const BALL_RADIUS = 10;
export const BALL_SPEED = 3;
export const BALL_INITIAL_ANGLE = 180;
+37 -2
View File
@@ -1,6 +1,14 @@
import './style.css'; import './style.css';
import { Application, Assets, Container, Graphics, Sprite } from 'pixi.js'; import { Application, Assets, Container, Graphics, Sprite } from 'pixi.js';
import { CONTAINER_HEIGHT, CONTAINER_WIDTH, PADDLE_HEIGHT, PADDLE_WIDTH } from './config'; import {
BALL_INITIAL_ANGLE,
BALL_RADIUS,
BALL_SPEED,
CONTAINER_HEIGHT,
CONTAINER_WIDTH,
PADDLE_HEIGHT,
PADDLE_WIDTH,
} from './config';
(async () => { (async () => {
// Create a new application // Create a new application
@@ -24,7 +32,6 @@ import { CONTAINER_HEIGHT, CONTAINER_WIDTH, PADDLE_HEIGHT, PADDLE_WIDTH } from '
app.stage.addChild(container); app.stage.addChild(container);
const paddle = new Graphics().rect(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT).fill('#fff000'); const paddle = new Graphics().rect(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT).fill('#fff000');
container.addChild(paddle); container.addChild(paddle);
container.on('pointermove', (event) => { container.on('pointermove', (event) => {
@@ -34,4 +41,32 @@ import { CONTAINER_HEIGHT, CONTAINER_WIDTH, PADDLE_HEIGHT, PADDLE_WIDTH } from '
paddle.x = localPosition.x; paddle.x = localPosition.x;
} }
}); });
const ball = new Graphics().circle(0, 0, BALL_RADIUS).fill('#ffffff');
container.addChild(ball);
const leftBoundary = BALL_RADIUS;
const rightBoundary = CONTAINER_WIDTH - BALL_RADIUS;
const topBoundary = BALL_RADIUS;
const bottomBoundary = CONTAINER_HEIGHT - BALL_RADIUS;
let horizontalSpeed = BALL_SPEED * Math.cos(BALL_INITIAL_ANGLE);
let verticalSpeed = -1 * BALL_SPEED * Math.sin(BALL_INITIAL_ANGLE);
app.ticker.add((time) => {
// Не даем выйти за границы стен слева / справав и меняем направление
if (ball.x <= leftBoundary || ball.x >= rightBoundary) {
ball.x = ball.x <= leftBoundary ? leftBoundary : rightBoundary;
horizontalSpeed *= -1;
}
// Не даем выйти за границы стен сверху / снизу и меняем направление
if (ball.y <= topBoundary || ball.y >= bottomBoundary) {
ball.y = ball.y <= topBoundary ? topBoundary : bottomBoundary;
verticalSpeed *= -1;
}
ball.x += horizontalSpeed * time.deltaTime;
ball.y += verticalSpeed * time.deltaTime;
});
})(); })();