# OOP Asteroids Game

For this week’s assignment I decided to make an arcade game. The goal of the game is to avoid all the falling mysterious asteroids and be the LAST SURVIVING SQUARE on the surface of the earth! The game is definitely not the final version and I am planning on adding some more features to it. But this could be considered the prototype of what I have imagined first. Below is the logo of the game I got inspired by initially.

Here is the video of the prototype of the game:

Below is the code:

// Asteroids Game

int i;
float x, y;
float velocity;
float acceleration;
int startTime = 0;
int time = 0;
Asteroid[] a = new Asteroid[20];

public void settings() {
size(640, 640);
}

void setup(){
//player position
x = width/2;
y = height-32;
velocity = 0;
acceleration = random(8,24);

for (int i=0; i<a.length; i++){
a[i] = new Asteroid();
}
}

void draw(){
background(0);

pushMatrix();
i = 0;
while (i<a.length){
a[i].display();
a[i].update();
if (a[i].posy > 12){
i++;
}
}
popMatrix();

pushMatrix();
player();
popMatrix();

textSize(25);
fill(255);
text("Level 1", width-100, 50);
time = (millis() - startTime) / 1000;
textSize(15);
text("Time alive : " + time, width-115, 75);
}

void player(){
stroke(255);
strokeWeight(2);
fill(127);
rect(x, y, 30, 30);
}

void keyPressed() {
if (keyCode == LEFT && x-15>= 0){
velocity += acceleration;
x -= velocity;
acceleration *=0;
}
if (keyCode == RIGHT && x+15 <= width ){
velocity += acceleration;
x += velocity;
acceleration *=0;
}
}

class Asteroid {

float posx, posy;
float speed;

Asteroid () {
smooth();
//asteroid position
posx = random(width);
posy = -15;
speed = random(6);
}

void update() {
posy += speed;

if (posy > height+6) {
posy = -6*2;
posx = random(width);
}
}

void display() {
pushMatrix();
stroke(255);
strokeWeight(2);
fill(127);
ellipse(posx, posy, 24, 24);
popMatrix();
}
}