Feature/appearance #6

Merged
ilia merged 9 commits from feature/appearance into main 2026-07-20 07:09:11 +00:00
24 changed files with 126 additions and 42 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 KiB

+2
View File
@@ -15,6 +15,8 @@ export const BALL_SPEED_TIME_STEP = 0.002;
export const BALL_INITIAL_ANGLE = 0; export const BALL_INITIAL_ANGLE = 0;
export const BALL_SPLIT_ANGLE = 20; export const BALL_SPLIT_ANGLE = 20;
export const HEADER_HEIGHT = 30;
export const BRICK_WIDTH = 40; export const BRICK_WIDTH = 40;
export const BRICK_HEIGHT = 10; export const BRICK_HEIGHT = 10;
+7 -2
View File
@@ -5,9 +5,10 @@ import { Brick } from '../brick';
* @param {number[][]} levelMap карта уровня в формате массива * @param {number[][]} levelMap карта уровня в формате массива
* @param {number} brickWidth ширина кирпича, неотрицателное число * @param {number} brickWidth ширина кирпича, неотрицателное число
* @param {number} brickHeight длина кирпича, неотрицателное число * @param {number} brickHeight длина кирпича, неотрицателное число
* @param {number} marginTop отступ сверху, неотрицательное число
* @returns {Brick[]} массив кирпичей * @returns {Brick[]} массив кирпичей
*/ */
export function layBricks(levelMap, brickWidth, brickHeight) { export function layBricks(levelMap, brickWidth, brickHeight, marginTop = 0) {
try { try {
if (!(Array.isArray(levelMap) && levelMap.every(Array.isArray))) { if (!(Array.isArray(levelMap) && levelMap.every(Array.isArray))) {
throw new Error('Значение карты уровня должно являться вложенным масивом чисел глубины 2'); throw new Error('Значение карты уровня должно являться вложенным масивом чисел глубины 2');
@@ -21,12 +22,16 @@ export function layBricks(levelMap, brickWidth, brickHeight) {
throw new Error('Значения ширины и высоты кирпича должны являться положительными целыми числами'); throw new Error('Значения ширины и высоты кирпича должны являться положительными целыми числами');
} }
if (typeof marginTop !== 'number' || marginTop < 0) {
throw new Error('Значение отступа сверху должно быть неотрицательным числом');
}
const bricks = []; const bricks = [];
for (let i = 0; i < levelMap.length; i++) { for (let i = 0; i < levelMap.length; i++) {
for (let j = 0; j < levelMap[i].length; j++) { for (let j = 0; j < levelMap[i].length; j++) {
if (levelMap[i][j] !== 0) { if (levelMap[i][j] !== 0) {
bricks.push(new Brick(j * brickWidth, i * brickHeight, brickWidth, brickHeight, levelMap[i][j])); bricks.push(new Brick(j * brickWidth, marginTop + i * brickHeight, brickWidth, brickHeight, levelMap[i][j]));
} }
} }
} }
@@ -24,6 +24,16 @@ describe('layBricks', () => {
expect(layBricks(levelMap, 1, 1)).toBeNull(); expect(layBricks(levelMap, 1, 1)).toBeNull();
}); });
it('Возвращает корректное значение в случае некоректного отступа', () => {
const levelMap = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
];
expect(layBricks(levelMap, 1, 1, -1)).toBeNull();
});
it('Возвращает массив корректных размеров', () => { it('Возвращает массив корректных размеров', () => {
const levelMap = [ const levelMap = [
[1, 1, 1, 1, 1], [1, 1, 1, 1, 1],
+3 -2
View File
@@ -8,6 +8,7 @@ import {
BRICK_WIDTH, BRICK_WIDTH,
CONTAINER_HEIGHT, CONTAINER_HEIGHT,
CONTAINER_WIDTH, CONTAINER_WIDTH,
HEADER_HEIGHT,
PADDLE_HEIGHT, PADDLE_HEIGHT,
PADDLE_WIDTH, PADDLE_WIDTH,
PERK_BALL_SPEED_DECREASE, PERK_BALL_SPEED_DECREASE,
@@ -33,7 +34,7 @@ export class Game {
this.paddle = new Paddle(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT); this.paddle = new Paddle(0, CONTAINER_HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
this.balls = [new Ball(0, 0, BALL_RADIUS, BALL_SPEED, BALL_INITIAL_ANGLE)]; this.balls = [new Ball(0, 0, BALL_RADIUS, BALL_SPEED, BALL_INITIAL_ANGLE)];
this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT); this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT, HEADER_HEIGHT);
this.perks = []; this.perks = [];
this._placeBallOnPaddle(); this._placeBallOnPaddle();
@@ -112,7 +113,7 @@ export class Game {
_proceedToNextLevel() { _proceedToNextLevel() {
this.ball.increaseSpeed(BALL_SPEED_LEVEL_STEP); this.ball.increaseSpeed(BALL_SPEED_LEVEL_STEP);
this._placeBallOnPaddle(); this._placeBallOnPaddle();
this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT); this.bricks = layBricks(this.levels[this.currentLevel], BRICK_WIDTH, BRICK_HEIGHT, HEADER_HEIGHT);
} }
/** /**
+16 -3
View File
@@ -38,8 +38,21 @@ import { createGameView, managePerkViewsLifetime, rebuildBrickViews, syncronizeV
app.stage.addChild(container); app.stage.addChild(container);
const textures = await Assets.load([
'/sprites/fire_ball_1.png',
'/sprites/background_1.png',
'/sprites/paddle.png',
'/sprites/block_1.png',
'/sprites/block_2.png',
'/sprites/block_3.png',
'/sprites/perk_1.png',
'/sprites/perk_2.png',
'/sprites/perk_3.png',
'/sprites/perk_4.png',
]);
const game = new Game(LEVELS); const game = new Game(LEVELS);
const views = createGameView(game, container); const views = createGameView(game, container, textures);
let currentLevel = game.currentLevel; let currentLevel = game.currentLevel;
container.on('pointermove', (event) => { container.on('pointermove', (event) => {
@@ -55,10 +68,10 @@ import { createGameView, managePerkViewsLifetime, rebuildBrickViews, syncronizeV
game.update(time.deltaTime); game.update(time.deltaTime);
if (game.currentLevel !== currentLevel) { if (game.currentLevel !== currentLevel) {
rebuildBrickViews(views, game, container); rebuildBrickViews(views, game, container, textures);
currentLevel = game.currentLevel; currentLevel = game.currentLevel;
} }
managePerkViewsLifetime(views, game, container); managePerkViewsLifetime(views, game, container, textures);
syncronizeViewsWithGame(views, game); syncronizeViewsWithGame(views, game);
}); });
})(); })();
+11
View File
@@ -1 +1,12 @@
@import "./reset.css"; @import "./reset.css";
body {
min-height: 100vh;
}
canvas {
width: 100vw;
height: 100vh;
display: block;
object-fit: contain;
}
+77 -35
View File
@@ -1,4 +1,5 @@
import { Container, Graphics } from 'pixi.js'; import { Container, Graphics, NineSliceSprite, Sprite, Text } from 'pixi.js';
import { CONTAINER_HEIGHT, CONTAINER_WIDTH } from './config';
import { Ball } from './entities/ball/ball'; import { Ball } from './entities/ball/ball';
import { Brick } from './entities/brick/brick'; import { Brick } from './entities/brick/brick';
import { Paddle } from './entities/paddle/paddle'; import { Paddle } from './entities/paddle/paddle';
@@ -8,77 +9,116 @@ import { Game } from './game';
/** /**
* Создает визуальное отображение мяча с помощью Pixi.js * Создает визуальное отображение мяча с помощью Pixi.js
* @param {Ball} ball экземпляр класса мяч * @param {Ball} ball экземпляр класса мяч
* @param {object} textures объект с текстурами для визуального отображения
*/ */
function createBallView(ball) { function createBallView(ball, textures) {
return new Graphics().circle(0, 0, ball.radius).fill('#ffffff'); const texture = textures['/sprites/fire_ball_1.png'];
const ballSprite = new Sprite(texture);
ballSprite.anchor.set(0.5);
ballSprite.width = ball.radius * 2;
ballSprite.height = ball.radius * 2;
return ballSprite;
} }
/** /**
* Создает визуальное отображение ракетки с помощью Pixi.js * Создает визуальное отображение ракетки с помощью Pixi.js
* @param {Paddle} paddle экземпляр класса ракетка * @param {Paddle} paddle экземпляр класса ракетка
* @param {object} textures объект с текстурами для визуального отображения
*/ */
function createPaddleView(paddle) { function createPaddleView(paddle, textures) {
return new Graphics().rect(0, 0, paddle.width, paddle.height).fill('#fff000'); const texture = textures['/sprites/paddle.png'];
const paddleSprite = new NineSliceSprite({ texture, leftWidth: 150, rightWidth: 150, topHeight: 0, bottomHeight: 0 });
paddleSprite.width = paddle.width;
paddleSprite.height = paddle.height;
return paddleSprite;
} }
/** /**
* Создает визуальное отображение кирпича с помощью Pixi.js * Создает визуальное отображение кирпича с помощью Pixi.js
* @param {Brick} brick экземпляр класса кирпич * @param {Brick} brick экземпляр класса кирпич
* @returns {Graphics} графическое отображение кирпича * @returns {Graphics} графическое отображение кирпича
* @param {object} textures объект с текстурами для визуального отображения
*/ */
function createBrickView(brick) { function createBrickView(brick, textures) {
const brickView = new Graphics().rect(0, 0, brick.width, brick.height); const bricksTextures = [
textures['/sprites/block_1.png'],
textures['/sprites/block_2.png'],
textures['/sprites/block_3.png'],
];
switch (brick.type) { const brickSprite = new Sprite(bricksTextures[brick.type - 1]);
case 2: brickSprite.width = brick.width;
return brickView.fill('#00ff00'); brickSprite.height = brick.height;
case 3:
return brickView.fill('#ff00ff'); return brickSprite;
case 1:
default:
return brickView.fill('#000fff');
}
} }
/** /**
* Создает визуальное отображение бонуса с помощью Pixi.js * Создает визуальное отображение бонуса с помощью Pixi.js
* @param {Perk} perk экземпляр класса бонус * @param {Perk} perk экземпляр класса бонус
* @returns {Graphics} графическое отображение бонуса * @returns {Graphics} графическое отображение бонуса
* @param {object} textures текстуры блоков
*/ */
function createPerkView(perk) { function createPerkView(perk, textures) {
const perkView = new Graphics().rect(0, 0, perk.width, perk.height); let texture;
switch (perk.type) { switch (perk.type) {
case 'slow': case 'slow':
return perkView.fill('#00ffff'); texture = textures['/sprites/perk_1.png'];
break;
case 'wide': case 'wide':
return perkView.fill('#0f0f0f'); texture = textures['/sprites/perk_2.png'];
break;
case 'life': case 'life':
return perkView.fill('#f0f0f0'); texture = textures['/sprites/perk_3.png'];
break;
case 'clone':
texture = textures['/sprites/perk_3.png'];
break;
default: default:
return perkView.fill('#ffffff'); texture = textures['/sprites/perk_1.png'];
break;
} }
const perkSprite = new Sprite(texture);
perkSprite.width = perk.width;
perkSprite.height = perk.height;
return perkSprite;
} }
/** /**
* Создает визуальное отображение всех сущностей в игре с помощью Pixi.js и добавляет в контейнер * Создает визуальное отображение всех сущностей в игре с помощью Pixi.js и добавляет в контейнер
* @param {Game} game экземпляр класса игра * @param {Game} game экземпляр класса игра
* @param {Container} container экземпляр класса контейнер Pixi.js * @param {Container} container экземпляр класса контейнер Pixi.js
* @param {object} textures объект с текстурами для визуального отображения
*/ */
export function createGameView(game, container) { export function createGameView(game, container, textures) {
try { try {
const background = new Sprite(textures['/sprites/background_1.png']);
background.width = CONTAINER_WIDTH;
background.height = CONTAINER_HEIGHT;
container.addChildAt(background, 0);
const header = new Text({ style: { fill: '#ffffff', fontSize: 16 } });
header.x = 8;
header.y = 8;
container.addChild(header);
const balls = new Map(); const balls = new Map();
for (const ball of game.balls) { for (const ball of game.balls) {
const ballView = createBallView(ball); const ballView = createBallView(ball, textures);
container.addChild(ballView); container.addChild(ballView);
balls.set(ball, ballView); balls.set(ball, ballView);
} }
const paddle = createPaddleView(game.paddle); const paddle = createPaddleView(game.paddle, textures);
container.addChild(paddle); container.addChild(paddle);
const bricks = game.bricks.map((brick) => { const bricks = game.bricks.map((brick) => {
const brickView = createBrickView(brick); const brickView = createBrickView(brick, textures);
container.addChild(brickView); container.addChild(brickView);
return brickView; return brickView;
}); });
@@ -86,6 +126,7 @@ export function createGameView(game, container) {
const perks = new Map(); const perks = new Map();
return { return {
header,
balls, balls,
paddle, paddle,
bricks, bricks,
@@ -102,13 +143,11 @@ export function createGameView(game, container) {
* @param {object} views объект с визуальными отображениями сущностей игры * @param {object} views объект с визуальными отображениями сущностей игры
* @param {Game} game экземпляр класса игра * @param {Game} game экземпляр класса игра
* @param {Container} container экземпляр класса контейнер Pixi.js * @param {Container} container экземпляр класса контейнер Pixi.js
* @param {object} textures объект с текстурами для визуального отображения
*/ */
export function managePerkViewsLifetime(views, game, container) { export function managePerkViewsLifetime(views, game, container, textures) {
if (views.paddle.width !== game.paddle.width) { if (views.paddle.width !== game.paddle.width) {
container.removeChild(views.paddle); views.paddle.width = game.paddle.width;
views.paddle.destroy();
views.paddle = createPaddleView(game.paddle);
container.addChild(views.paddle);
} }
for (const [ball, ballView] of views.balls) { for (const [ball, ballView] of views.balls) {
@@ -121,7 +160,7 @@ export function managePerkViewsLifetime(views, game, container) {
for (const ball of game.balls) { for (const ball of game.balls) {
if (!views.balls.has(ball)) { if (!views.balls.has(ball)) {
const ballView = createBallView(ball); const ballView = createBallView(ball, textures);
container.addChild(ballView); container.addChild(ballView);
views.balls.set(ball, ballView); views.balls.set(ball, ballView);
} }
@@ -137,7 +176,7 @@ export function managePerkViewsLifetime(views, game, container) {
for (const perk of game.perks) { for (const perk of game.perks) {
if (!views.perks.has(perk)) { if (!views.perks.has(perk)) {
const perkView = createPerkView(perk); const perkView = createPerkView(perk, textures);
container.addChild(perkView); container.addChild(perkView);
views.perks.set(perk, perkView); views.perks.set(perk, perkView);
} }
@@ -155,6 +194,8 @@ export function syncronizeViewsWithGame(views, game) {
throw new Error('Аргумент game должен быть экземпляром класса Game'); throw new Error('Аргумент game должен быть экземпляром класса Game');
} }
views.header.text = `Уровень: ${game.currentLevel + 1} Количество жизней: ${game.livesAmount}`;
for (const ball of game.balls) { for (const ball of game.balls) {
const ballView = views.balls.get(ball); const ballView = views.balls.get(ball);
ballView.x = ball.x; ballView.x = ball.x;
@@ -187,15 +228,16 @@ export function syncronizeViewsWithGame(views, game) {
* @param {object} views объект с визуальными отображениями сущностей игры * @param {object} views объект с визуальными отображениями сущностей игры
* @param {Game} game экземпляр класса игра * @param {Game} game экземпляр класса игра
* @param {Container} container экземпляр класса контейнер Pixi.js * @param {Container} container экземпляр класса контейнер Pixi.js
* @param {object} textures объект с текстурами для визуального отображения
*/ */
export function rebuildBrickViews(views, game, container) { export function rebuildBrickViews(views, game, container, textures) {
for (const brickView of views.bricks) { for (const brickView of views.bricks) {
container.removeChild(brickView); container.removeChild(brickView);
brickView.destroy(); brickView.destroy();
} }
views.bricks = game.bricks.map((brick) => { views.bricks = game.bricks.map((brick) => {
const brickView = createBrickView(brick); const brickView = createBrickView(brick, textures);
container.addChild(brickView); container.addChild(brickView);
return brickView; return brickView;
}); });