//variables
Ship ship = new Ship();
Meteor meteor = new Meteor();
Meteor meteor1 = new Meteor();
float backgroundOther = 255;
float meteorStartY;
float determineSpawn;
int projectileX;
//arrays and pvectors
PVector playerPosition = new PVector (mouseX, mouseY);
int [] projectilePosition = new int[2];
PVector meteorPosition = new PVector();
boolean isFire = false;
void setup() {
size(800, 600);
}
void draw() {
frameRate(400);
//the only background color that changes is red
background(255, backgroundOther, backgroundOther);
//this tells the enemy to randomly spawn along the Y axis
determineSpawn = random(100);
//this is how the player is moved
playerPosition.x = mouseX;
playerPosition.y = mouseY;
//these are the object references
meteor.StartY();
meteor.Move();
meteor1.StartY();
meteor1.Move();
ship.drawShip();
keyPressed();
ship.Update();
//these are the hit conditions
if (dist(playerPosition.x, playerPosition.y, meteorPosition.x, meteorPosition.y) < 100) {
backgroundOther = backgroundOther -0.3;
print("shot");
}
//if the player hits the meteor
if (dist(meteorPosition.x, meteorPosition.y, projectilePosition[0], projectilePosition[1]) < 100) {
backgroundOther = backgroundOther + -1;
print("hit");
}
}
//these fire the projectile with W
void keyPressed() {
if (keyCode == 87) {
isFire = true;
projectileX = projectileX - 10;
}
if (keyCode != 87) {
isFire = false;
projectileX = projectileX - 10;
}
}class Meteor {
float meteorSpeed = -1;
void StartY() {
if (meteorSpeed < 0) {
meteorStartY = random(0, 600);
}
if (meteorSpeed > width) {
meteorSpeed = -10;
}
}
void Move() {
meteorSpeed = meteorSpeed + random(0, 5); //makes the meteor jerk around while moving
fill(0); //colour
ellipse(meteorSpeed, meteorStartY, 100, 100); //creates the meteors
meteorPosition.x = meteorSpeed; //moves the meteor according to speed
}
}class Ship {
void drawShip() {
fill(255, 0, 0);
ellipse(mouseX, mouseY, 10, 10);
if (isFire == false) {
projectileX = mouseX;
}
}
void Update() {
rect(projectileX, mouseY, 10, 10);
projectilePosition [0] = projectileX;
projectilePosition [1] = mouseY;
}
}