Update Redditlikespacesuit.HTML
This commit is contained in:
parent
7671a13fef
commit
2df3e5cdb4
1 changed files with 66 additions and 21 deletions
|
@ -15,10 +15,13 @@
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
}
|
}
|
||||||
.control-button {
|
.control-button {
|
||||||
margin: 0 15px;
|
margin: 0 20px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
width: 50px; /* Increased button size */
|
}
|
||||||
|
#score {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
font-size: 24px;
|
||||||
|
margin-top: 20px;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
@ -29,6 +32,7 @@
|
||||||
<div class="control-button" id="shoot-button">!</div>
|
<div class="control-button" id="shoot-button">!</div>
|
||||||
<div class="control-button" id="right-arrow">></div>
|
<div class="control-button" id="right-arrow">></div>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="score">Score: <span id="score-value">0</span> | Highest Score: <span id="highest-score">0</span></div>
|
||||||
<script>
|
<script>
|
||||||
const canvas = document.getElementById("gameCanvas");
|
const canvas = document.getElementById("gameCanvas");
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
|
@ -38,9 +42,9 @@
|
||||||
y: canvas.height - 30,
|
y: canvas.height - 30,
|
||||||
width: 30,
|
width: 30,
|
||||||
height: 30,
|
height: 30,
|
||||||
speed: 6,
|
speed: 8,
|
||||||
isMovingLeft: false,
|
movingLeft: false,
|
||||||
isMovingRight: false,
|
movingRight: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const bullets = [];
|
const bullets = [];
|
||||||
|
@ -52,34 +56,61 @@
|
||||||
const maxInvaders = 2;
|
const maxInvaders = 2;
|
||||||
|
|
||||||
let failedCount = 0;
|
let failedCount = 0;
|
||||||
|
let score = 0;
|
||||||
|
let highestScore = 0;
|
||||||
|
|
||||||
const leftArrowButton = document.getElementById("left-arrow");
|
const leftArrowButton = document.getElementById("left-arrow");
|
||||||
const rightArrowButton = document.getElementById("right-arrow");
|
const rightArrowButton = document.getElementById("right-arrow");
|
||||||
const shootButton = document.getElementById("shoot-button");
|
const shootButton = document.getElementById("shoot-button");
|
||||||
|
const scoreDisplay = document.getElementById("score-value");
|
||||||
|
const highestScoreDisplay = document.getElementById("highest-score");
|
||||||
|
|
||||||
leftArrowButton.addEventListener("mousedown", () => (player.isMovingLeft = true));
|
leftArrowButton.addEventListener("mousedown", () => startMoveLeft());
|
||||||
leftArrowButton.addEventListener("mouseup", () => (player.isMovingLeft = false));
|
leftArrowButton.addEventListener("mouseup", () => stopMoveLeft());
|
||||||
rightArrowButton.addEventListener("mousedown", () => (player.isMovingRight = true));
|
leftArrowButton.addEventListener("touchstart", () => startMoveLeft());
|
||||||
rightArrowButton.addEventListener("mouseup", () => (player.isMovingRight = false));
|
leftArrowButton.addEventListener("touchend", () => stopMoveLeft());
|
||||||
shootButton.addEventListener("click", () => shoot());
|
|
||||||
|
|
||||||
function shoot() {
|
rightArrowButton.addEventListener("mousedown", () => startMoveRight());
|
||||||
bullets.push({ x: player.x + player.width / 2, y: player.y });
|
rightArrowButton.addEventListener("mouseup", () => stopMoveRight());
|
||||||
}
|
rightArrowButton.addEventListener("touchstart", () => startMoveRight());
|
||||||
|
rightArrowButton.addEventListener("touchend", () => stopMoveRight());
|
||||||
|
|
||||||
document.addEventListener("keydown", (event) => {
|
document.addEventListener("keydown", (event) => {
|
||||||
if (event.key === " ") {
|
if (event.key === "ArrowLeft") {
|
||||||
|
startMoveLeft();
|
||||||
|
} else if (event.key === "ArrowRight") {
|
||||||
|
startMoveRight();
|
||||||
|
} else if (event.key === " ") {
|
||||||
shoot();
|
shoot();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function movePlayer() {
|
document.addEventListener("keyup", (event) => {
|
||||||
if (player.isMovingLeft && player.x > 0) {
|
if (event.key === "ArrowLeft") {
|
||||||
player.x -= player.speed;
|
stopMoveLeft();
|
||||||
|
} else if (event.key === "ArrowRight") {
|
||||||
|
stopMoveRight();
|
||||||
}
|
}
|
||||||
if (player.isMovingRight && player.x < canvas.width - player.width) {
|
});
|
||||||
player.x += player.speed;
|
|
||||||
|
function startMoveLeft() {
|
||||||
|
player.movingLeft = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stopMoveLeft() {
|
||||||
|
player.movingLeft = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function startMoveRight() {
|
||||||
|
player.movingRight = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopMoveRight() {
|
||||||
|
player.movingRight = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function shoot() {
|
||||||
|
bullets.push({ x: player.x + player.width / 2, y: player.y });
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawPlayer() {
|
function drawPlayer() {
|
||||||
|
@ -101,7 +132,15 @@
|
||||||
|
|
||||||
function updateGameArea() {
|
function updateGameArea() {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
movePlayer();
|
|
||||||
|
if (player.movingLeft && player.x > 0) {
|
||||||
|
player.x -= player.speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.movingRight && player.x < canvas.width - player.width) {
|
||||||
|
player.x += player.speed;
|
||||||
|
}
|
||||||
|
|
||||||
drawPlayer();
|
drawPlayer();
|
||||||
|
|
||||||
for (let i = bullets.length - 1; i >= 0; i--) {
|
for (let i = bullets.length - 1; i >= 0; i--) {
|
||||||
|
@ -132,6 +171,7 @@
|
||||||
) {
|
) {
|
||||||
bullets.splice(j, 1);
|
bullets.splice(j, 1);
|
||||||
invaders.splice(i, 1);
|
invaders.splice(i, 1);
|
||||||
|
score++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,6 +184,10 @@
|
||||||
failedCount++;
|
failedCount++;
|
||||||
if (failedCount >= 3) {
|
if (failedCount >= 3) {
|
||||||
alert("You failed, try again");
|
alert("You failed, try again");
|
||||||
|
if (score > highestScore) {
|
||||||
|
highestScore = score;
|
||||||
|
highestScoreDisplay.textContent = highestScore;
|
||||||
|
}
|
||||||
document.location.reload();
|
document.location.reload();
|
||||||
}
|
}
|
||||||
invaders.splice(i, 1);
|
invaders.splice(i, 1);
|
||||||
|
@ -154,6 +198,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scoreDisplay.textContent = score;
|
||||||
requestAnimationFrame(updateGameArea);
|
requestAnimationFrame(updateGameArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue