Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var px=200, py=300, ex=600, ey=300, pr=0, er=0, php = 20, ehp = 20;
- var nb = 0;
- var bx = [], by = [], dx = [], dy = [], color = [];
- var pspeed = 5, bspeed = 15, r = 10;
- function d(x1, y1, x2, y2){
- return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
- }
- function shoot(x, y, tx, ty, c){
- bx[nb] = x;
- by[nb] = y;
- let dist = d(x, y, tx, ty);
- dx[nb] = (tx-x)*bspeed/dist;
- dy[nb] = (ty-y)*bspeed/dist;
- color[nb] = c;
- ++nb;
- }
- function move_enemy(){
- if (er == 0){
- er = r;
- shoot(ex, ey, px, py, "red");
- }
- }
- function rem_bullet(i){
- nb--;
- bx[i] = bx[nb];
- by[i] = by[nb];
- dx[i] = dx[nb];
- dy[i] = dy[nb];
- color[i] = color[nb];
- }
- function update() {
- if (php <= 0 || ehp <= 0) return;
- if (pr > 0) --pr;
- if (er > 0) --er;
- if (isKeyPressed[65]) px -= pspeed;
- if (isKeyPressed[68]) px += pspeed;
- if (isKeyPressed[87]) py -= pspeed;
- if (isKeyPressed[83]) py += pspeed;
- move_enemy();
- for (let i=0; i<nb; ++i){
- bx[i] += dx[i];
- by[i] += dy[i];
- if (color[i] == 'red' && areColliding(px, py, 30, 30, bx[i], by[i], 5, 5)){
- php--;
- rem_bullet(i);
- --i;
- continue;
- }
- if (color[i] == 'blue' && areColliding(ex, ey, 30, 30, bx[i], by[i], 5, 5)){
- ehp--;
- rem_bullet(i);
- --i;
- continue;
- }
- }
- }
- function draw() {
- context.fillStyle = "blue";
- context.fillRect(px, py, 30, 30);
- context.fillStyle = "red";
- context.fillRect(ex, ey, 30, 30);
- for (let i=0; i<nb; ++i){
- context.fillStyle = color[i];
- context.fillRect(bx[i], by[i], 5, 5);
- }
- };
- function keyup(key) {
- // Show the pressed keycode in the console
- console.log("Pressed", key);
- };
- function mouseup() {
- if (pr == 0){
- pr = r;
- shoot(px, py, mouseX, mouseY, "blue");
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement