Update JS
This commit is contained in:
parent
947e7996aa
commit
6e7cf9166c
63
Script.js
63
Script.js
@ -34,42 +34,45 @@ function init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function keyDownPressed(e) {
|
function keyDownPressed(e) {
|
||||||
if (e.keycode == 81) {
|
if (e.keyCode == 81) {
|
||||||
keys.up = true;
|
keys.up = true;
|
||||||
} else if (e.keycode == 65) {
|
} else if (e.keyCode == 65) {
|
||||||
keys.down = true
|
keys.down = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.keycode == 79) {
|
if (e.keyCode == 79) {
|
||||||
keys.left = true;
|
keys.left = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.keycode == 80) {
|
if (e.keyCode == 80) {
|
||||||
keys.right = true;
|
keys.right = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.keycode == 32) {
|
if (e.keyCode == 32) {
|
||||||
keys.fire = true;
|
keys.fire = true;
|
||||||
alert("fire");
|
alert("fire");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function keyUpPressed(e) {
|
function keyUpPressed(e) {
|
||||||
if (e.keycode == 81) {
|
if (e.keyCode == 81) {
|
||||||
keys.up = false;
|
keys.up = false;
|
||||||
} else if (e.keycode == 65) {
|
} else if (e.keyCode == 65) {
|
||||||
keys.down = false;
|
keys.down = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.keycode == 79) {
|
if (e.keyCode == 79) {
|
||||||
keys.left = false;
|
keys.left = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.keycode == 80) {
|
if (e.keyCode == 80) {
|
||||||
keys.right = false;
|
keys.right = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e.keycode == 32) {
|
if (e.keyCode == 32) {
|
||||||
keys.fire = false;
|
keys.fire = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,13 +81,13 @@ function clearGame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateGame() {
|
function updateGame() {
|
||||||
|
player1.update();
|
||||||
|
|
||||||
game.frames++;
|
game.frames++;
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawGame() {
|
function drawGame() {
|
||||||
|
player1.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
function gameLoop(timestamp){
|
function gameLoop(timestamp){
|
||||||
@ -92,4 +95,36 @@ function gameLoop(timestamp){
|
|||||||
updateGame();
|
updateGame();
|
||||||
drawGame();
|
drawGame();
|
||||||
window.requestAnimationFrame(gameLoop);
|
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);
|
||||||
Loading…
x
Reference in New Issue
Block a user