1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| //Declarations
let
canvas,
context,
width,
height,
frames = 0,
floor,
floor_position,
actual_status = "play",
character,
character_position,
game_velocity = 6,
jump_force = 20,
maxJump = 1,
background,
players,
actionLeft = false,
actionRight = false,
actionJump = false,
action = false,
actionHold = false,
actionBomb = false,
actionEsc = false,
startAction = false;
//Class floor
class Floor {
constructor(position_floor, width_floor, height_floor) {
this.position = position_floor;
this.height = height_floor;
this.width = width_floor;
this.color = "#ffdf70";
}
//Drawing the floor
draw_floor() {
context.fillStyle = this.color;
context.fillRect(this.position.x, this.position.y, this.width, this.height);
}
}
//Class position
class Position {
constructor(x_const, y_const) {
this.x = x_const;
this.y = y_const;
}
right() {
if (this.x < width - 50) {
this.x += game_velocity;
} else if (this.x >= width - 50) {
this.x = width - 50;
}
}
left() {
if (this.x > 0) {
this.x -= game_velocity;
} else if (this.x <= 0) {
this.x = 0;
}
}
jump(object) {
if (object.number_of_jump < maxJump) {
object.velocity = -jump_force;
}
}
}
//Class Player
class Player {
constructor(id, name, color) {
this.id = id
this.name = name;
this.velocity = 0;
this.width = 50;
this.height = 50;
this.gravity = 1;
this.color = color;
this.position = new Position(Math.floor(width * Math.random()), Math.floor(height * Math.random()));
this.number_of_jump = 0;
}
draw_character() {
// let character_icon = new Image();
// character_icon.src = "./images/character.png";
// context.drawImage(character_icon, -this.width/2, -this.height/2);
context.fillStyle = this.color;
context.fillRect(this.position.x, this.position.y, this.width, this.height);
};
refresh_character() {
this.velocity += this.gravity;
this.position.y += this.velocity;
if (this.position.y > (floor.position.y - this.height) && actual_status != "lost") {
this.position.y = floor.position.y - this.height;
this.velocity = 0;
this.number_of_jump = 0;
}
};
}
function draw() {
floor.draw_floor(); //LE BUG EST ICI QUAND J'essaie d'accéder à la fonction draw_floor de mon objet floor ça dit que flor est indéfini
}
function menu() {
console.log(floor);
// drawSun(); // indéfini
start();
}
function refresh() {
console.log(`fonction inexistante`);
}
//Function play is the function wich permit to the game to have an infinite loop
function start() {
//Permit to request for commands
// sendAjax();
refresh();
draw();
window.requestAnimationFrame(start);
}
// Starting the Script
function main() {
actual_status = "play";
//Getting window information(height and width)
height = window.innerHeight;
width = window.innerWidth;
//Creating the canvas
canvas = document.createElement("canvas");
canvas.height = height;
canvas.width = width;
canvas.style.border = "1px solid red";
context = canvas.getContext("2d");
//Adding canvas to html
document.body.appendChild(canvas);
floor_position = new Position(0, 550);
floor = new Floor(floor_position, width, 50);
menu();
}
document.addEventListener('DOMContentLoaded', ev => {
// le DOM est construit, la page web n'est pas visible
// début code du test
// fin code du test
}, {
capture: false,
passive: true,
once: false
});
window.addEventListener('load', ev => {
// le DOM est construit et la page web est visible
// début code du test
main();
// fin code du test
}, {
capture: false,
passive: true,
once: false
}); |
Partager