Anonymous
Функция попадания в мини-шутер [закрыто]
Сообщение
Anonymous » 22 окт 2025, 13:31
Я делаю мини-шутер, но после того, как я добавил хороший способ заставить несколько пуль выходить после нажатия пробела, он сломал несколько вещей. Во-первых, мой игрок (я знаю, почему это так) может столкнуться с врагом и нанести урон (это потому, что это this.x, теперь раньше это работало с Bullet.x). А во-вторых, мои пули не наносят 1 урон, а затем исчезают (это работало до того, как я добавил массив).
Я хочу, чтобы мой код работал с несколькими пулями
https://jsfiddle.net/tmanrocks999/7mLpo8uj/
Мой новый код:
https://jsfiddle.net/tmanrocks999/64thbvm3/190/
Я ожидаю, что после пробела нажмите на пули, чтобы сделать 1 урон, пули исчезают при попадании во врага, и мой игрок не может нанести урон, ударив врага. но на данный момент мои пули пролетают мимо врага и наносят 3-4 урона, хотя на базе должно быть 1.
Код: Выделить всё
var myGamePiece;
var endGoalPiece;
var myEnemy1;
var bullets = [];
var myEnemy1Hp = 10;
var myEnemy1Armor = 0;
var damage = 1;
var playerExp = 0;
var playerMaxExp = 10;
function startGame() {
myGameArea.start();
myGamePiece = new component(30, 30, "red", 0, 240);
endGoalPiece = new component(30, 30, "black", 450, 240);
myEnemy1 = new component(30, 30, "green", 200, 240);
}
var myGameArea = {
canvas: document.createElement("canvas"),
start: function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.interval = setInterval(updateGameArea, 20);
window.addEventListener('keydown', function(e) {
myGameArea.key = e.keyCode;
})
window.addEventListener('keyup', function(e) {
myGameArea.key = false;
})
},
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y) {
this.gamearea = myGameArea;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
//this.gravity = 0.05;
//this.gravitySpeed = 0;
this.x = x;
this.y = y;
this.color = color;
this.update = function() {
ctx = myGameArea.context;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY; //+ this.gravitySpeed;
this.hitBottom();
this.hitTop();
this.hitRight();
this.hitLeft();
this.hitObject();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
}
}
this.hitTop = function() {
var rockTop = 0;
if (this.y < rockTop) {
this.y = rockTop;
}
}
this.hitRight = function() {
var rockRight = myGameArea.canvas.width - this.width;
if (this.x > rockRight) {
this.x = rockRight;
}
}
this.hitLeft = function() {
var rockLeft = 0;
if (this.x < rockLeft) {
this.x = rockLeft;
}
}
function enemyRespawn() {
myEnemy1 = new component(30, 30, "green", 200, 240);
myEnemy1Hp = 10;
document.getElementById('myEnemy1Hp').innerHTML = myEnemy1Hp;
}
this.hitObject = function() {
myGamePiece.update();
var enemy = myEnemy1.x-11;
if (this.x == enemy) {
myEnemy1Hp = myEnemy1Hp - (damage - myEnemy1Armor);
bullet= 0,0;
document.getElementById('myEnemy1Hp').innerHTML = myEnemy1Hp;
if(myEnemy1Hp =playerMaxExp){
playerExp = 0;
playerMaxExp = playerMaxExp*1.5;
damage = damage + 1;
document.getElementById('playerExp').innerHTML = playerExp;
document.getElementById('playerMaxExp').innerHTML = playerMaxExp;
}
myEnemy1 = new component(0, 0, "green", 0, 0);
myEnemy1.update();
setTimeout(enemyRespawn, 5000);
}
}
}
}
function shootGun() {
let bullet = new component(11, 5, "blue", myGamePiece.x + 27, myGamePiece.y + 13);
bullet.newPos();
bullet.speedX = 1;
bullets.push( bullet );
}
function updateGameArea() {
myGameArea.clear();
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
if (myGameArea.key && myGameArea.key == 37) {
myGamePiece.speedX = -1;
} //left
if (myGameArea.key && myGameArea.key == 39) {
myGamePiece.speedX = 1;
} //right
if (myGameArea.key && myGameArea.key == 38) {
myGamePiece.gravitySpeed = -1;
} //jump
if (myGameArea.key && myGameArea.key == 32) {
shootGun()
} //shoot gun
//if (myGameArea.key && myGameArea.key == 40) {myGamePiece.speedY = 1; }// down
myEnemy1.update();
endGoalPiece.update();
myGamePiece.newPos();
myGamePiece.update();
bullets.forEach( (bullet)=> {
bullet.newPos()
bullet.update();
});
// bullet.newPos();
// bullet.update();
}
Код: Выделить всё
canvas {
border: 4px solid #d3d3d3;
background-color: #f1f1f1;
}
Код: Выделить всё
Basic Shooter
use the arrow keys on you keyboard to move the red square.
10
0 / 10 🐺
Подробнее здесь:
https://stackoverflow.com/questions/586 ... t-function
1761129100
Anonymous
Я делаю мини-шутер, но после того, как я добавил хороший способ заставить несколько пуль выходить после нажатия пробела, он сломал несколько вещей. Во-первых, мой игрок (я знаю, почему это так) может столкнуться с врагом и нанести урон (это потому, что это this.x, теперь раньше это работало с Bullet.x). А во-вторых, мои пули не наносят 1 урон, а затем исчезают (это работало до того, как я добавил массив). Я хочу, чтобы мой код работал с несколькими пулями https://jsfiddle.net/tmanrocks999/7mLpo8uj/ Мой новый код: https://jsfiddle.net/tmanrocks999/64thbvm3/190/ Я ожидаю, что после пробела нажмите на пули, чтобы сделать 1 урон, пули исчезают при попадании во врага, и мой игрок не может нанести урон, ударив врага. но на данный момент мои пули пролетают мимо врага и наносят 3-4 урона, хотя на базе должно быть 1. [code] var myGamePiece; var endGoalPiece; var myEnemy1; var bullets = []; var myEnemy1Hp = 10; var myEnemy1Armor = 0; var damage = 1; var playerExp = 0; var playerMaxExp = 10; function startGame() { myGameArea.start(); myGamePiece = new component(30, 30, "red", 0, 240); endGoalPiece = new component(30, 30, "black", 450, 240); myEnemy1 = new component(30, 30, "green", 200, 240); } var myGameArea = { canvas: document.createElement("canvas"), start: function() { this.canvas.width = 480; this.canvas.height = 270; this.context = this.canvas.getContext("2d"); document.body.insertBefore(this.canvas, document.body.childNodes[0]); this.interval = setInterval(updateGameArea, 20); window.addEventListener('keydown', function(e) { myGameArea.key = e.keyCode; }) window.addEventListener('keyup', function(e) { myGameArea.key = false; }) }, clear: function() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); } } function component(width, height, color, x, y) { this.gamearea = myGameArea; this.width = width; this.height = height; this.speedX = 0; this.speedY = 0; //this.gravity = 0.05; //this.gravitySpeed = 0; this.x = x; this.y = y; this.color = color; this.update = function() { ctx = myGameArea.context; ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y, this.width, this.height); } this.newPos = function() { this.gravitySpeed += this.gravity; this.x += this.speedX; this.y += this.speedY; //+ this.gravitySpeed; this.hitBottom(); this.hitTop(); this.hitRight(); this.hitLeft(); this.hitObject(); } this.hitBottom = function() { var rockbottom = myGameArea.canvas.height - this.height; if (this.y > rockbottom) { this.y = rockbottom; } } this.hitTop = function() { var rockTop = 0; if (this.y < rockTop) { this.y = rockTop; } } this.hitRight = function() { var rockRight = myGameArea.canvas.width - this.width; if (this.x > rockRight) { this.x = rockRight; } } this.hitLeft = function() { var rockLeft = 0; if (this.x < rockLeft) { this.x = rockLeft; } } function enemyRespawn() { myEnemy1 = new component(30, 30, "green", 200, 240); myEnemy1Hp = 10; document.getElementById('myEnemy1Hp').innerHTML = myEnemy1Hp; } this.hitObject = function() { myGamePiece.update(); var enemy = myEnemy1.x-11; if (this.x == enemy) { myEnemy1Hp = myEnemy1Hp - (damage - myEnemy1Armor); bullet= 0,0; document.getElementById('myEnemy1Hp').innerHTML = myEnemy1Hp; if(myEnemy1Hp =playerMaxExp){ playerExp = 0; playerMaxExp = playerMaxExp*1.5; damage = damage + 1; document.getElementById('playerExp').innerHTML = playerExp; document.getElementById('playerMaxExp').innerHTML = playerMaxExp; } myEnemy1 = new component(0, 0, "green", 0, 0); myEnemy1.update(); setTimeout(enemyRespawn, 5000); } } } } function shootGun() { let bullet = new component(11, 5, "blue", myGamePiece.x + 27, myGamePiece.y + 13); bullet.newPos(); bullet.speedX = 1; bullets.push( bullet ); } function updateGameArea() { myGameArea.clear(); myGamePiece.speedX = 0; myGamePiece.speedY = 0; if (myGameArea.key && myGameArea.key == 37) { myGamePiece.speedX = -1; } //left if (myGameArea.key && myGameArea.key == 39) { myGamePiece.speedX = 1; } //right if (myGameArea.key && myGameArea.key == 38) { myGamePiece.gravitySpeed = -1; } //jump if (myGameArea.key && myGameArea.key == 32) { shootGun() } //shoot gun //if (myGameArea.key && myGameArea.key == 40) {myGamePiece.speedY = 1; }// down myEnemy1.update(); endGoalPiece.update(); myGamePiece.newPos(); myGamePiece.update(); bullets.forEach( (bullet)=> { bullet.newPos() bullet.update(); }); // bullet.newPos(); // bullet.update(); }[/code] [code] canvas { border: 4px solid #d3d3d3; background-color: #f1f1f1; }[/code] [code] Basic Shooter use the arrow keys on you keyboard to move the red square. 10 0 / 10 🐺 [/code] Подробнее здесь: [url]https://stackoverflow.com/questions/58677552/mini-shooter-game-hit-function[/url]