Update JS

This commit is contained in:
Hijau-dev 2025-11-27 21:55:32 +07:00
parent 947e7996aa
commit 6e7cf9166c

View File

@ -34,41 +34,44 @@ function init() {
}
function keyDownPressed(e) {
if (e.keycode == 81) {
if (e.keyCode == 81) {
keys.up = true;
} else if (e.keycode == 65) {
} else if (e.keyCode == 65) {
keys.down = true
}
if (e.keycode == 79) {
if (e.keyCode == 79) {
keys.left = true;
}
if (e.keycode == 80) {
if (e.keyCode == 80) {
keys.right = true;
}
if (e.keycode == 32) {
if (e.keyCode == 32) {
keys.fire = true;
alert("fire");
}
}
function keyUpPressed(e) {
if (e.keycode == 81) {
if (e.keyCode == 81) {
keys.up = false;
} else if (e.keycode == 65) {
} else if (e.keyCode == 65) {
keys.down = false;
}
if (e.keycode == 79) {
if (e.keyCode == 79) {
keys.left = false;
}
if (e.keycode == 80) {
if (e.keyCode == 80) {
keys.right = false;
}
if (e.keycode == 32) {
if (e.keyCode == 32) {
keys.fire = false;
}
}
@ -78,13 +81,13 @@ function clearGame() {
}
function updateGame() {
player1.update();
game.frames++;
}
function drawGame() {
player1.draw();
}
function gameLoop(timestamp){
@ -93,3 +96,35 @@ function gameLoop(timestamp){
drawGame();
window.requestAnimationFrame(gameLoop);
}
class PlayerObject{
constructor(x,y){
this.x = x;
this.y = y;
this.width = 200;
this.height = 150;
this.color = 'blue';
this.speed = 10;
this.lives = 3;
this.score = 0;
this.health = 100;
}
draw(){
ctx.save();
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.fill();
ctx.restore();
}
update(){
if(keys.up){
this.y -= this.speed;
}
else if (keys.down){
this.y += this.speed;
}
}
}
let player1 = new PlayerObject(100.100);