Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var canvas= document.getElementById("monCanvas");
- var ctx = canvas.getContext("2d");
- var hero = {
- x:40,
- y:canvas.height/2,
- life:5
- }
- var game = {
- gravity:0.8,
- up:-0.8,
- sy:0,
- dt:0.5,
- }
- var keysDown = {};
- var missilesArray = [];
- var ennemisArray= [];
- addEventListener("keydown", function (e) {
- keysDown[e.keyCode] = true;
- }, false);
- addEventListener("keyup", function (e) {
- delete keysDown[e.keyCode];
- }, false);
- console.log(ennemisArray);
- animate();
- function Missiles(posX, posY, w, h){
- this.x = posX,
- this.y = posY,
- this.w = w,
- this.h = h
- }
- function Ennemis(posX, posY, w, h){
- this.x = posX,
- this.y = posY,
- this.w = w,
- this.h = h
- }
- function animate(){
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- draw();
- update();
- requestAnimationFrame(animate);
- }
- function drawBalle(){
- ctx.fillStyle="#0AE6E9";
- ctx.beginPath();
- ctx.arc(hero.x, hero.y, 20, 0, 2*Math.PI);
- ctx.fill();
- }
- function draw(){
- drawBalle();
- for(i=0;i<missilesArray.length;i++){
- ctx.fillStyle="#0AE6E9";
- ctx.fillRect(missilesArray[i].x+=5,missilesArray[i].y,missilesArray[i].w,missilesArray[i].h);}
- for(i=0;i<ennemisArray.length;i++){
- ctx.fillStyle="pink";
- ctx.fillRect(ennemisArray[i].x+=5,ennemisArray[i].y,ennemisArray[i].w,ennemisArray[i].h);}
- }
- function update(){
- game.up=-0.8;
- game.sy += game.gravity* game.dt;
- hero.y+=game.sy*game.dt;
- if(hero.y>canvas.height+50){respawn();}
- if(hero.y<0){hero.y=0; game.up=5;}
- if(32 in keysDown){game.sy += game.up;}
- window.setInterval(createEnnemis(), 5000);
- }
- function respawn(){
- hero.y=canvas.height/2;
- hero.life=hero.life-1;
- game.sy=0;
- }
- canvas.addEventListener("click", souris);
- function souris(e){
- missilesArray.push(new Missiles(hero.x, hero.y, 15, 5));
- //console.log(sourisX, sourisY)
- }
- function createEnnemis(){
- ennemisArray.push(new Ennemis(0, canvas.height/2, 15, 5));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement