Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
dragon731012
GitHub Repository: dragon731012/-WORKING-bookmarklets-and-games
Path: blob/main/tests (don't work)/Square shooter
15482 views
var x=window.innerWidth;
var width=x;
var y=window.innerHeight;
var height=y;
x=x/2-25;
y=y-100;
var up=0;
var down=0;
var left=0;
var right=0;
var playerspeed=6;
var enemyx=0;
var enemyy=-50;
var bulletchangey=1;
var bulletshoot=1;
var enemyy=-50;
var enemyx=-50;
var count=0;
var enemyspeed=6;
var enemylist=[];

(function () {
	var you = document.createElement('div');
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(you);

	you.style.position = 'fixed';
	you.style.top = ''+y+'px';
	you.style.left = ''+x+'px';
	you.style.paddingTop = '10px';
	you.style.width = '50px';
	you.style.height = '50px';
	you.style.zIndex = 10000;
	you.style.opacity = 1;
	you.style.color = 'white';
	you.style.backgroundColor = 'black';
	you.style.border = '0px solid white';
	you.style.textAlign = 'center';
	you.id = 'you';
	you.style.display = 'block';
	you.innerText = '';
}());

window.addEventListener("keydown", function(event) { 
    if (event.key == "ArrowUp") {
        up=1;
    }
	if (event.key == "ArrowDown") {
        down=1;
    }
	if (event.key == "ArrowLeft") {
        left=1;
    }
	if (event.key == "ArrowRight") {
        right=1;
    }
});

window.addEventListener("keyup", function(event) { 
    if (event.key == "ArrowUp") {
        up=0;
    }
	if (event.key == "ArrowDown") {
        down=0;
    }
	if (event.key == "ArrowLeft") {
        left=0;
    }
	if (event.key == "ArrowRight") {
        right=0;
    }
});

function move(){
if (up==1){
	y=y-playerspeed;
	you.style.top = ''+y+'px';
	you.style.left = ''+x+'px';
}
if (down==1){
    y=y+playerspeed;
	you.style.top = ''+y+'px';
	you.style.left = ''+x+'px';
}
if (left==1){
    x=x-playerspeed;
	you.style.top = ''+y+'px';
	you.style.left = ''+x+'px';
}
if (right==1){
    x=x+playerspeed;
	you.style.top = ''+y+'px';
	you.style.left = ''+x+'px';
}
if (bulletchangey==1){
	
}
}
setInterval(move,15);

window.addEventListener("keyup", function(event) {
if (event.key == "s") {
if (bulletshoot==1){
(function () {
	var bullet = document.createElement('div');
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(bullet);

	bullet.style.position = 'fixed';
	var a=y-10;
	bullet.style.top = ''+a+'px';
	var z=x+25-7;
	bullet.style.left = ''+z+'px';
	bullet.style.paddingTop = '10px';
	bullet.style.width = '7px';
	bullet.style.height = '10px';
	bullet.style.zIndex = 10000;
	bullet.style.opacity = 1;
	bullet.style.color = 'red';
	bullet.style.backgroundColor = 'red';
	bullet.style.border = '0px solid white';
	bullet.style.textAlign = 'center';
	bullet.id = 'bullet';
	bullet.style.display = 'block';
	bullet.innerText = '';
	
	function moveup(){
		a=a-6;
		bullet.style.top = ''+a+'px';
		if (a<=0){
			bulletshoot=1;
		}
	}
	setInterval(moveup,15);
	bulletshoot=0;
}());
}
}
});

function makeenemy(){
(function () {
	enemylist.push(enemy.style.top);
	enemylist.push(enemy.style.left);
	var enemy = document.createElement('div');
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(enemy);
	enemyx=Math.floor(Math.random()*(width-0+1)+0);
	enemy.style.position = 'fixed';
	enemy.style.top = ''+enemyy+'px';
	enemy.style.left = ''+enemyx+'px';
	enemy.style.paddingTop = '10px';
	enemy.style.width = '50px';
	enemy.style.height = '50px';
	enemy.style.opacity = 1;
	enemy.style.color = 'blue';
	enemy.style.backgroundColor = 'blue';
	enemy.style.border = '0px solid white';
	enemy.style.textAlign = 'center';
	count=count+1;
	enemy.id = 'enemy'+count;
	enemy.style.display = 'block';
	enemy.innerText = '';
	function starting(){
	function movedown(){
		enemylist[count+1]=enemylist[count+1]+enemyspeed;
		enemy.style.top = ''+enemylist[count+1]+'px';
		enemylist[count]=enemy.style.left;
		if (enemylist[count+1]>=height){
			makeenemy();
		}
	}
	setInterval(movedown,15);
	}
	setTimeout(starting,500);
}());
}
makeenemy();