Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.8 kB
5
Indexable
Never
var ship = new WebImage("https://vignette.wikia.nocookie.net/chickeninvaders/images/5/51/BomberFront.png/revision/latest?cb=20191006132306");
var shipwidth =40;
var shipheight =30;
var shipofset =30;

var row =4;
var column = 6;
var chickenwidth = 40;
var chickenheight= 30;
var offset = (getWidth()-chickenwidth * 6)/ 7;
var y = 10;
var x = offset;


var buletwidth=8;
var buletheight= 16;
var bullet;
var bullet_dx = 0;
var bullet_dy = -3;

var score =new Text ("score 0", "15pt Arial");
var scores= 0;

var bullnum = 30;
var bullnumber = new Text (bullnum , "15pt Arial");

var dxckn=4;
var dyckn=0;

var chickens = [];
var eggs= [];
var bullets = [];

var winningscore=120;

var background = new WebImage("https://vignette.wikia.nocookie.net/chickeninvaders/images/5/50/Wiki-background/revision/latest?cb=20191201203017");

var egg ;
var eggw = 10;
var eggh= 12;
var egg_dx =0;
var egg_dy =3;


var count = 0;
var chickens_move_call_number = 0;


function start() {
    background = new WebImage("https://vignette.wikia.nocookie.net/chickeninvaders/images/5/50/Wiki-background/revision/latest?cb=20191201203017");
    background.setSize(getWidth(), getHeight());
    background.setPosition (0 ,0 );
    add (background);
    
    var prolog = readLine ("This is chicken invaners game, avoid eggs and kill all chicken. You have only 30 bullet. Good Luck");
   
   
    scorecount();
    bulletscount();
    
	draw();
	drawship();
	
	mouseMoveMethod(shipmove);
	mouseClickMethod(drawbullet);
	
	setTimer(chickenmove,20);
	setTimer(drawegg, 300);
	
	setTimer(eggmove, 50);
}


//this function draws eggs
function drawegg (){
    egg = new WebImage (" https://vignette.wikia.nocookie.net/chickeninvaders/images/2/26/Egg.png/revision/latest?cb=20180728171055");
    egg.setSize(eggw, eggh);
    
    var random_chicken_number = Randomizer.nextInt(0, chickens.length-1);
    
    if (chickens[random_chicken_number] != undefined) {
        var eggx= chickens[random_chicken_number].getX() + chickenwidth/2;
        var eggy= chickens[random_chicken_number].getY() + chickenheight;
    
        egg.setPosition (eggx, eggy);
        add (egg);
        eggs.push(egg);
    }
}

//this function moves mutiple eggs
function eggmove(e){
    for(var i = 0; i < eggs.length; i++){
        eggs[i].move(egg_dx, egg_dy);
    }
    checkegg();

}
//this function is for egg and ship colission an game over
function checkegg(e) {
    for(var i = 0; i < eggs.length; i++){
        var elem = getElementAt(eggs[i].getX(), eggs[i].getY()+eggh+1);
    	if (elem == ship) {
    	    stopTimer(drawegg);
    	    stopTimer(eggmove);
    	   
    	    removeAll();
    	    
    	    add (background);
    	    
    	    var gameover = new Text ("GAME OVER", "12pt Arial");
            gameover.setColor(Color.white);
            gameover.setPosition (getWidth()/2 -40, getHeight()/2);
            add(gameover);
    	}
	}   	    
}

//this function moves multiply bullets    
function bulletmove(e){
    for (var i=0; i<bullets.length; i++) {
        if (bullets[i] != undefined) {
            bullets[i].move(bullet_dx, bullet_dy);
           
        }
    }
    checkchicken();
    
}

//this function draws bullets
function drawbullet(){
    if (bullnum > 0) {
        bullet = new WebImage("https://vignette.wikia.nocookie.net/chickeninvaders/images/e/e0/UtensilPokerCarving.png/revision/latest?cb=20180808210855");
        bullet.setSize(buletwidth, buletheight);
        bullet.setPosition(ship.getX() + shipwidth/2-buletwidth/2, getHeight()-shipofset -shipheight-buletheight);
        add(bullet);
        bullets.push(bullet);
        
        setTimer(bulletmove,20);
        
        bullnum--;
        bullnumber.setText("bullet: " + bullnum);
    } else {
        gameover();
    }
}
//this function moves chickens
function chickenmove(e){
    chickens_move_call_number++;
    if (chickens_move_call_number % 11 == 0) {
        dxckn = -dxckn;
    }
    
    for(var i = 0; i < chickens.length; i++){
        if (chickens[i] != undefined) {
            chickens[i].move(dxckn,dyckn);    
        }
        
    }
    
}
// this function returns if chicken and bullet have colission
function checkIfChickenIsDead(chicken_to_check, bullet_to_check) {
    if (chicken_to_check == undefined || bullet_to_check == undefined) {
        return false;
    } 
    
    var top = getElementAt(bullet_to_check.getX(), bullet_to_check.getY() - bullet_to_check.getHeight());
    if (top == chicken_to_check)  {
        return true;
    }
    
    return false;
}


//this function removes chickens and bullets when hit
function checkchicken(e) {
    
    for (var i=0; i<chickens.length; i++) {
        for (var k=0; k<bullets.length; k++) {
            if (checkIfChickenIsDead(chickens[i], bullets[k]) ==  true) {
                scores = scores +5;
                
                remove(chickens[i]);
                remove(bullets[k]);
                delete chickens[i];
                delete bullets[k];
                
                winner();
            }
        }
    }
}
    





//this function draws score count bar
function scorecount (e){
    score = new Text ("score 0", "12pt Arial");
    score.setColor(Color.white);
    score.setPosition (getWidth()-100, getHeight()-10);
    add (score);
    
}
//this function draws bullet count bar
function bulletscount (e){
    bullnumber = new Text ("bullet: 30 ", "12pt Arial");
    bullnumber.setColor(Color.white);
    bullnumber.setPosition (20, getHeight()-10);
    add (bullnumber);
    
}
//this function is for ship movement
function shipmove(e){
    ship.setPosition(e.getX()-shipwidth/2, getHeight()-shipofset -shipheight);
    if(e.getX()-shipwidth/2<0 ){
        ship.setPosition(0,getHeight()-shipofset-shipheight);
    }
    if(e.getX() +shipwidth/2>getWidth() ){
        ship.setPosition(getWidth() - shipwidth,getHeight()-shipofset-shipheight);
    }
}
//this function draws ship
function drawship(){
    ship = new WebImage("https://vignette.wikia.nocookie.net/chickeninvaders/images/5/51/BomberFront.png/revision/latest?cb=20191006132306");
    ship.setSize(shipwidth, shipheight);
    
    ship.setPosition(getWidth()/2 -shipwidth/2, getHeight() - shipofset-shipheight);
    add (ship);
    
    

}

// this function draws chickens
function drawChicken(type, position_x, position_y) {
    var chicken_image_url = '';
    if (type == 'boss') {
        chicken_image_url = 'https://vignette.wikia.nocookie.net/chickeninvaders/images/8/8d/SweaterChickenCIU.png/revision/latest?cb=20190120215403';
    } else if (type == 'blue') {
        chicken_image_url = 'https://vignette.wikia.nocookie.net/chickeninvaders/images/b/b4/BigChicken.png/revision/latest?cb=20180311182645';
    } else if (type == 'red') {
        chicken_image_url = 'https://vignette.wikia.nocookie.net/chickeninvaders/images/0/00/BigChickenCIU.png/revision/latest?cb=20190225195618';
    }
    
    var chicken = new WebImage(chicken_image_url);
    chicken.setSize(chickenwidth, chickenheight);
    chicken.setPosition (position_x , position_y);
    add (chicken);
    chickens.push(chicken);
}
    

    


//this functions draws chickens
function draw(){
    

    var chicken_x = offset;
    var chicken_y = offset;
    var chicken_count = 1;
    var chicken_type = 'boss';
    
    for (var i=0; i<row; i++) {
        chicken_y = (chickenheight + offset)*i + offset;
            
        for (var k=0; k<column; k++) {
            chicken_x = (chickenwidth + offset)*k + offset;

            drawChicken(chicken_type, chicken_x, chicken_y);
            chicken_count++;
 
            if (chicken_count <= 6) {
                chicken_type = 'boss';
            } else if (chicken_count > 6 && chicken_count <= 12) {
                chicken_type = 'red';
            } else {
                chicken_type = 'blue';
            }
        }
    }
    
}


//this functions draws  winner text	
function winner (){
    score.setText ("score " + scores);
    if (scores ==winningscore){
         stopTimer(drawegg);
    	 stopTimer(eggmove);
         removeAll();
    	 add (background);
        var winner = new Text ("YOU WIN", "12pt Arial");
        winner.setColor(Color.white);
        winner.setPosition (getWidth()/2 -40, getHeight()/2);
        add(winner);
    }
}
//this functions draws  gameover text	
function gameover (){
            
                
                stopTimer(drawegg);
    	        stopTimer(eggmove);
                removeAll();
    	        add (background);
    	    
    	    var gameover = new Text ("GAME OVER", "12pt Arial");
            gameover.setColor(Color.white);
            gameover.setPosition (getWidth()/2 -40, getHeight()/2);
            add(gameover);
            
}