xxxxxxxxxx
var ship_svg;
var spaceship;
var starsky;
var bullets = [];
var asteroids = [];
var screen;
var is_space_pressed = false;
var is_left_pressed = false;
var is_right_pressed = false;
var is_up_pressed = false;
var is_down_pressed = false;
var is_z_pressed = false;
class Screen{
constructor(){
this.x = 0;
this.y = 0;
this.vx = 0.0;
this.vy = 0.0;
}
draw(){
background(0);
return;
stroke(100);
for (var y = 0; y < height + 50; y += 50){
var yy = this.y % 50;
line(0, y - yy, width, y - yy);
}
for (var x = 0; x < width + 50; x += 50){
var xx = this.x % 50;
line(x - xx, 0, x - xx, height);
}
}
step(){
// this.x += this.vx * deltaTime;
// this.y += this.vy * deltaTime;
}
}
class Star{
constructor(x, y){
this.x = x;
this.y = y;
this.r = Math.random() * 4 + 1;
this.b = 50 + Math.random() * 100;
this.nb = Math.random() * 3;
}
draw(){
// noStroke();
this.nb += 0.05;
stroke(noise(this.nb) * 155);
var x = (this.x - screen.x % width) % width;
var y = (this.y - screen.y % height) % height;
if (x < 0)
x += width;
if (y < 0)
y += height;
push();
translate(x, y);
//circle(0, 0, this.r);
strokeWeight(this.r);
line(0, 0, screen.vx * 10, screen.vy * 10);
pop();
}
step(){
// this.x += screen.vx * deltaTime;
// this.y += screen.vy * deltaTime;
// if (this.x > width)
// this.x = 0;
// if(this.x < 0)
// this.x = width;
// if(this.y > height)
// this.y = 0;
// if(this.y < 0)
// this.y = height;
}
}
class StarSky{
constructor(){
this.stars = [];
for (var i = 0; i < 100; i++) {
this.stars.push(new Star(Math.floor(Math.random() * width), Math.floor(Math.random() * height)));
}
}
draw(){
// background(0);
this.stars.forEach((s)=>s.draw());
}
step(){
this.stars.forEach((s)=>s.step());
}
}
class Asteroid{
constructor(x, y){
this.x = x;
this.y = y;
this.v = Math.random() * 0.09 + 0.03;
this.a = Math.PI / 2 + Math.random();
this.already_in_screen = false;
}
draw(){
push();
translate(this.x, this.y);
fill(100, 200, 0);
circle(0, 0, 25);
pop();
}
step(){
this.x += this.v * Math.cos(this.a) * deltaTime;
this.y += this.v * Math.sin(this.a) * deltaTime;
if(!this.already_in_screen && this.is_in_screen()){
this.already_in_screen = true;
}
if(this.already_in_screen && !this.is_in_screen()){
var i = asteroids.indexOf(this);
asteroids.splice(i, i + 1);
}
}
is_in_screen(){
return this.x > 0 & this.x < width & this.y > 0 & this.y < height;
}
}
class Bullet{
constructor(x, y, a, vx, vy){
this.a = a;
this.x = x;
this.y = y;
this.vx = vx + Math.cos(a);
this.vy = vy + Math.sin(a);
this.is_exploding = false;
this.explosion_radius = 0;
}
distance(){
return Math.sqrt(Math.pow(spaceship.x - this.x, 2) + Math.pow(spaceship.y - this.y, 2));
}
draw(){
push();
if(!this.is_exploding){
translate(this.x - screen.x, this.y - screen.y);
stroke(255);
rotate(this.a);
line(0, 0, 5, 0);
}else{
this.x += (spaceship.vx) * deltaTime;
this.y += (spaceship.vy) * deltaTime;
translate(this.x - screen.x, this.y - screen.y);
stroke(0);
fill(255, 0, 0);
circle(0, 0, this.explosion_radius);
}
pop();
}
step(){
if (!this.is_exploding){
this.x += (this.vx) * deltaTime;
this.y += (this.vy) * deltaTime;
if (this.distance() > 300){
this.explode();
// var i = bullets.indexOf(this);
// bullets.splice(i, i + 1);
}
}else{
this.explosion_radius += 3;
if(this.explosion_radius > 20){
var i = bullets.indexOf(this);
bullets.splice(i, i + 1);
}
}
}
is_in_screen(){
return this.x > 0 & this.x < width & this.y > 0 & this.y < height;
}
explode(){
this.is_exploding = true;
}
}
class Spaceship{
constructor(screen){
screen = screen;
this.points = [0, 10, 5, -10, -5, -10];
this.x = 40;
this.y = 40;
this.a = 1;
this.vx = 0.0;
this.vy = 0.0;
this.vamp = 0;
this.va = 0;
this.flame_length_counter = 0.0;
this.flame_length_switch = false;
this.flame_lengths = [5, 15];
}
draw(){
push();
translate(this.x - screen.x, this.y - screen.y);
rotate(this.a - Math.PI / 2);
stroke(0);
fill(200);
triangle(this.points[0], this.points[1], this.points[2],
this.points[3], this.points[4], this.points[5],
this.points[6], this.points[7], this.points[8]);
fill(255, 100, 100);
stroke(100, 0, 0);
var amp = is_up_pressed ? 40 : 5;
triangle(0, -10 - noise(this.flame_length_counter) * amp, this.points[2],
this.points[3], this.points[4], this.points[5],
this.points[6], this.points[7], this.points[8]);
pop();
this._draw_velocity_vector();
}
_draw_velocity_vector(){
push();
stroke(255, 255, 0)
translate(width - 50, 50);
rotate(this.va - Math.PI / 2);
line(0, 0, 0, this.vamp * 20.0);
line(0, this.vamp * 20.0, 3, this.vamp * 20.0 - 5);
line(0, this.vamp * 20.0, -3, this.vamp * 20.0 - 5);
pop();
}
step(){
this.x += this.vx * deltaTime;
this.y += this.vy * deltaTime;
if (is_left_pressed)
this.a -= 0.1;
if(is_right_pressed)
this.a += 0.1;
if(is_up_pressed){
this.vx += 0.01 * Math.cos(this.a);
this.vy += 0.01 * Math.sin(this.a);
}
if(is_down_pressed){
this.vx -= 0.01 * Math.cos(this.a);
this.vy -= 0.01 * Math.sin(this.a);
}
if(is_z_pressed){
this.vx = 0.0;
this.vy = 0.0;
// this.x = 40;
// this.y = 40;
}
// if(is_down_pressed & spaceship.v > 0)
// spaceship.v -= 0.02;
this.va = Math.atan2(this.vy, this.vx);
this.vamp = Math.sqrt(Math.pow(this.vx, 2) + Math.pow(this.vy, 2));
screen.x = this.x - width / 2;
screen.y = this.y - height / 2;
screen.vx = this.vx;
screen.vy = this.vy;
if(this.flame_length_counter > 10){
this.flame_length_counter = 0;
this.flame_length_switch = !this.flame_length_switch;
}
this.flame_length_counter += 1.1;
if(is_space_pressed)
this.shoot();
}
shoot(){
var b = new Bullet(this.x, this.y, this.a, this.vx, this.vy);
bullets.push(b);
}
}
function preload(){
}
function setup() {
createCanvas(1000, 600);
screen = new Screen();
spaceship = new Spaceship();
starsky = new StarSky();
ship_svg = loadImage('cactus1.png');
}
function draw() {
background(0);
// screen.step();
// screen.draw();
starsky.step();
starsky.draw();
bullets.forEach((b)=>{b.step()});
spaceship.step();
bullets.forEach((b)=>{b.draw()});
spaceship.draw();
// asteroids.forEach((a)=>a.step());
// asteroids.forEach((a)=>a.draw());
fill(255);
text(bullets.length, 10, 20);
text(asteroids.length, 10, 40);
}
function keyPressed() {
switch(key){
case ' ':
is_space_pressed = true;
break;
case 'z':
is_z_pressed = true;
// asteroids.push(new Asteroid(Math.floor(Math.random() * width), -20));
break;
}
switch(keyCode){
case LEFT_ARROW:
is_left_pressed = true;
break;
case RIGHT_ARROW:
is_right_pressed = true;
break;
case UP_ARROW:
is_up_pressed = true;
break;
case DOWN_ARROW:
is_down_pressed = true;
break;
}
}
function keyReleased() {
switch(key){
case ' ':
is_space_pressed = false;
break;
case 'z':
is_z_pressed = false;
break;
}
switch(keyCode){
case LEFT_ARROW:
is_left_pressed = false;
break;
case RIGHT_ARROW:
is_right_pressed = false;
break;
case UP_ARROW:
is_up_pressed = false;
break;
case DOWN_ARROW:
is_down_pressed = false;
break;
}
}