xxxxxxxxxx
177
let first;
let arr = [];
let numofspaceships = 5;
let player;
function preload() {
img = loadImage('215c7fdca6033092baa04b35c17466bd.gif');
}
function setup() {
createCanvas(600, 400);
first = new Spaceshuttle();
player = new playerShuttle();
for(let i = 0; i < numofspaceships;i++){
arr[i] = new Spaceshuttle();
}
}
function draw() {
background(220);
image(img, 0, 0);
first.display();
player.display();
for(let i = 0; i < numofspaceships;i++){
arr[i].display();
}
}
class Spaceshuttle {
constructor() {
this.x = random(width);
this.y = random(height);
// this.diameter = random(10, 30);
this.speed = 10;
this.redd = random(255);
this.greenn = random(255);
this.bluee= random(255);
this.randomx = random(100);
this.randomy = random(100);
}
move() {
this.x = map(noise(this.randomx),0,1,0,width+150);
this.y = map(noise(this.randomy),0,1,0,height+150);
this.randomx += 0.005;
this.randomy += 0.005;
}
display() {
noStroke();
fill(0);
strokeWeight(2);
stroke(51);
line(this.x+10,this.y,this.x+20,this.y+15);
line(this.x-10,this.y,this.x-20,this.y+15);
stroke(0);
fill(this.redd,this.greenn,this.bluee);
ellipse(this.x, this.y, 80, 25);
fill(0,179,255);
arc(this.x, this.y, 40, 40, PI, 2*PI , CHORD);
this.move();
}
}
class playerShuttle {
constructor() {
this.x = random(width);
this.y = random(height);
// this.diameter = random(10, 30);
this.speed = 10;
this.redd = random(255);
this.greenn = random(255);
this.bluee= random(255);
this.speedx = 0;
this.speedy = 0;
}
move() {
let rate = 0.07;
let maxspeed = 5;
if(keyIsPressed){
if(keyCode == LEFT_ARROW){
this.speedx -= rate;
}else if(keyCode == RIGHT_ARROW){
this.speedx += rate;
}
if(keyCode == UP_ARROW){
this.speedy -= rate;
}else if(keyCode == DOWN_ARROW){
this.speedy += rate;
}
}else{
if(this.speedx != 0){
if(this.speedx > 0){
this.speedx -= rate * 2;
}else{
this.speedx += rate * 2;
}
}
if(this.speedy != 0){
if(this.speedy > 0){
this.speedy -= rate * 2;
}else{
this.speedy += rate * 2;
}
}
}
if(this.speedx>maxspeed ){
this.speedx = maxspeed;
}else if(this.speedx<(-1*maxspeed)){
this.speedx = -1*maxspeed;
}
if(this.speedy>maxspeed){
this.speedy = maxspeed;
}else if(this.speedy<(-1*maxspeed)){
this.speedy = -1*maxspeed;
}
this.x += this.speedx;
this.y += this.speedy;
}
display() {
noFill();
strokeWeight(2);
stroke(250);
ellipse(this.x, this.y, 80, 80);
noStroke();
fill(0);
strokeWeight(2);
stroke(51);
line(this.x+10,this.y,this.x+20,this.y+15);
line(this.x-10,this.y,this.x-20,this.y+15);
stroke(0);
fill(this.redd,this.greenn,this.bluee);
ellipse(this.x, this.y, 80, 25);
fill(0,179,255);
arc(this.x, this.y, 40, 40, PI, 2*PI , CHORD);
if(this.x > width+2){
this.x = 0;
}else if(this.x <= 0){
this.x = width;
}
if(this.y > height+2){
this.y = 0;
}else if(this.y <= 0){
this.y = height;
}
this.move();
}
}