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();
  }
}


 

Creating artwork

For this assignment, I created an artwork where you can see a group of wheel-looking objects rotating in different sizes. Personally, this resembles the solar system because the wheels seem to have their own orbits and rotations in place. The wheels are comprised of equally sized rectangles and they create an effect that looks like pulses. Enjoy!

float c1 = random(255);
float c2 = random(255);
float c3 = random(255);
float c4 = random(255);
float c5 = random(255);

void setup() {
  size(640, 360);
}

void draw() {
  background(c1, c2, c3);
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 20.0);
  ring(c1, c2, c3, 1, 20, 20, 1, 50, 20, 0.01, 0.01);
  popMatrix();
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 200.0);
  ring(c2, c4, c1, 5, 70, 20, 1, 100, 100, 0.1, 0.1);
  popMatrix();
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 50.0);
  ring(c3, c5, c2, 10, 50, 50, 5, 50, 100, 0.1, 0.01);
  popMatrix();
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 100.0);
  ring(c4, c1, c3, 20, -200, 200, 10, 200, 100, 0.01, 0.01);
  popMatrix();
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 100.0);
  ring(c5, c1, c2, 16, -120, 120, 10, 200, 200, 0.01, 0.1);
  popMatrix();
  
}

void ring(float col, float col2, float col3, int h, float x, float y, float radius, int npoints, float amplitude, float speed, float granular) {
  randomSeed(0);
  stroke(0);
  fill(col, col2, col3);
  float angle = TWO_PI / npoints;
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius;
    float sy = y + sin(a) * radius;
    float freq = frameCount*speed + (x * granular);
    float adjustedHeight = noise(freq);
    adjustedHeight -= .5;
    adjustedHeight *= amplitude;
    pushMatrix();
    translate(sx, sy);
    rotate(a);
    rect(a+sx, sy, 8, h + adjustedHeight);
    popMatrix();
  }
}

void mousePressed () {
  if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
    c1 = random(255);
    c2 = random(255);
    c3 = random(255);
    c4 = random(255);
    c5 = random(255);
  }
}

 

David’s Portrait

For the first assignment in this course, we each had to make a self-portrait using Processing. The way I see it, self-portrait shows the way you want yourself to be presented to other people rather than how you precisely look on the exterior. Having grown in numerous different places I call home as a third-culture kid, I have always perceived myself as someone who has a spectrum of colors. Adapting was something that I grew accustomed to almost as if I became a chameleon. So even in this project I have incorporated an effect where my cartoon-like self-portrait is bouncing around the screen and, at the same time, changing colors (when a mouse is pressed). The change in color does not only happen in one area of the frame but rather everywhere in different variations. So when the background color changes, the color of my face, the color of my pupils, and the color of my name all change asynchronously. All of these animations were put into place so that I could illustrate how I adapt to the capricious environment. The following are the video of my animated self-portrait and code:

//David's self portrait

float x;
float y;
int xspeed = 2;
int yspeed = 2;
float c1 = random(255);
float c2 = random(255);
float c3 = random(255);

void setup(){
  size(500, 300);
  x = width/2;
  y = height/2;
}

void draw(){
  background(c1, c2, c3);
  stroke(10);
  noFill();
  if (x >= width-80 || x<=50) {
    xspeed *= -1;
  }
  if (y >= height-80 || y<=50) {
    yspeed *= -1;
  }
  x += xspeed;
  y += yspeed;
  
  face(x,y);
}

void face(float x, float y){
  //face
  fill(c3,c2,c1);
  ellipse(x+15, y+15, 130, 150);
  
  //eyes
  fill(255);
  ellipse(x-10, y, 30, 20);
  ellipse(x+40, y, 30, 20);
  
  //pupils
  fill(c1,c2,c3);
  ellipse(x-10, y, 10, 10);
  ellipse(x+40, y, 10, 10);
  
  //mouth
  fill(c3,c1,c2);
  arc(x+15, y+40, 80, 50, 0, 3.14);
  line(x-25, y+40, x+55, y+40);
  
  //eyebrows
  noFill();
  arc(x+40, y-10, 30, 20, 3.14+0.3, TWO_PI-0.3);
  arc(x-10, y-10, 30, 20, 3.14+0.3, TWO_PI-0.3);
  
  //ears
  fill(c3,c2,c1);
  arc(x-50, y+10, 30, 20, HALF_PI, PI+2*QUARTER_PI);
  arc(x+80, y+10, 30, 20, PI+2*QUARTER_PI,TWO_PI+2*QUARTER_PI); 
  
  //nose
  arc(x+15, y+20, 30, 15, HALF_PI, PI+2*QUARTER_PI);
  
  //NYU Cap
  fill(101, 101, 252);
  arc(x+15, y-25, 110, 100, 3.14, TWO_PI);
  fill(101, 101, 252);
  line(x+70, y-25, x+100, y-25);
  
  //text
  textSize(25);
  fill(c2,c1,c3);
  text("NYU", x-10, y-40);
  fill(c1,c3,c2);
  text("David", x-16, y+120);
}

void mousePressed () {
  if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
    c1 = random(255);
    c2 = random(255);
    c3 = random(255);
  }
}