r/processing • • Apr 03 '17

[PWC56] Space Invader

Hello Everybody, this is the 56th Weekly Processing challenge, the challenges are decided just to give you a prompt to test your skills so it can be as simple or as complicated as you have time to write!

Start Date : 03-04-2017 End Date : 09-04-2017 Post entries in the comments here.

This Weeks Challenge : Space Invader, Start out simply by trying to draw a space invader and move from there! The only way to fail is to never try.

Winner from last week : Praetrorian

3 Upvotes

25 comments sorted by

2

u/ChuckEye Apr 05 '17 edited Apr 05 '17

A Space Invader breeding program... Four generations. Parent pairs spawn children with identical dominant genes and a chance for a recessive gene to slip through.

int PixelSize = 8;
SpaceInvader[] GreatGrandParent;
SpaceInvader[] GrandParent;
SpaceInvader[] Parent;
SpaceInvader Child;

void setup() {
  size(800, 400);
  rectMode(CENTER);
  background(0);
  frameRate(1);
}

void draw() {
  GreatGrandParent = new SpaceInvader[8];
  for (int foo = 0; foo < 8; ++foo) {
    GreatGrandParent[foo] = new SpaceInvader(foo + 1, 1);
    GreatGrandParent[foo].spawn();
    GreatGrandParent[foo].show();
  }

  GrandParent = new SpaceInvader[4];
  for (int foo = 0; foo < 4; ++foo) {
    GrandParent[foo] = new SpaceInvader(foo + 1, 2);
    GrandParent[foo].breed(GreatGrandParent[foo*2], GreatGrandParent[(foo*2)+1]);
    GrandParent[foo].show();
  }

  Parent = new SpaceInvader[2];
  for (int foo = 0; foo < 2; ++foo) {
    Parent[foo] = new SpaceInvader(foo + 1, 3);
    Parent[foo].breed(GrandParent[foo*2], GrandParent[(foo*2)+1]);
    Parent[foo].show();
  }

  Child = new SpaceInvader(1, 4);
  Child.breed(Parent[0], Parent[1]);
  Child.show();
}

class SpaceInvader {
  boolean[] Pixel = new boolean[15];
  int BlockX, Generation; 
  SpaceInvader (int tempX, int tempY) {  
    BlockX = tempX; 
    Generation = tempY;
  }
  void spawn() {
    for (int y = 0; y < 5; ++y) {
      for (int x = 0; x < 3; ++x) {
        int i = (3 * y) + x;
        Pixel[i] = boolean(int(random(10)/5));
      }
    }
  }
  void show() {
    noStroke();
    pushMatrix(); 
    translate((pow(2, Generation-1)*100*BlockX)-(pow(2, Generation-1)*50), (100 * Generation)-50);
    scale(PixelSize);
    for (int y = 0; y < 5; ++y) {
      for (int x = 0; x < 3; ++x) {
        int i = (3 * y) + x;
        if (Pixel[i]) { 
          fill(0, 255, 0);
        } else { 
          fill(0);
        }
        rect(0-x, 3-y, 1, 1);
        rect(x, 3-y, 1, 1);
      }
    }
    if (Generation > 1) { //add lines to show breeding
      scale(1.0/PixelSize);
      stroke(255);    
      line(0, -25, 0, -50);
      line(pow(2, Generation-2)*-50, -50, pow(2, Generation-2)*50, -50);
    }
    popMatrix();
  }
  void breed(SpaceInvader Mom, SpaceInvader Dad) {
    for (int y = 0; y < 5; ++y) {
      for (int x = 0; x < 3; ++x) {
        int i = (3 * y) + x;
        //dominant gene
        if (Mom.Pixel[i] == Dad.Pixel[i]) {
          Pixel[i] = Mom.Pixel[i];
        } else {
          //recessive gene
          Pixel[i] = boolean(int(random(10)/5));
        }
      }
    }
  }
}

1

u/seoceojoe Apr 05 '17

is that genetic computing in Processing!

→ More replies (8)

4

u/Praetrorian Apr 04 '17

A circular space invaders. My best score is 11500, have fun guys!

  Tank tank = new Tank();
  ArrayList<Invader> invaders = new ArrayList<Invader>();
  boolean shooting = false;
  int score = 0;
  int bestScore = 0;
  
  
  void setup() {
    size(800, 800);
    for (float j = 0; j < 10; j++) {
      for (float i = 0; i < 2*PI; i+= 2*PI/25) {
        invaders.add(new Invader(new PVector((250+50*j)*cos(i), (250+50*j)*sin(i)), i, j%2==0));
      }
    }
  }
  
  void draw() {
    background(0); 
    for (int i=invaders.size()-1; i>-1; i--) {
      invaders.get(i).update(); 
      invaders.get(i).show();
      for (Bullet b : invaders.get(i).bullets) {
        if (tank.hit(b))
          reset();
      }
      for (int j=tank.bullets.size()-1; j>-1; j--) {
        if (invaders.get(i).hit(tank.bullets.get(j))) {
          invaders.remove(i);
          tank.bullets.remove(j);
          score += 100;
        }
      }
    }
  
    tank.update(); 
    tank.show();
    showPlanet();
    HUD();
  }
  
  void reset() {
    invaders = new ArrayList<Invader>();
    tank = new Tank();
    score = 0;
    for (float j = 0; j < 10; j++) {
      for (float i = 0; i < 2*PI; i+= 2*PI/25) {
        invaders.add(new Invader(new PVector((250+50*j)*cos(i), (250+50*j)*sin(i)), i, j%2==0));
      }
    }
  }
  
  
  void showPlanet() {
    pushMatrix();
    translate(width/2, height/2);
    fill(10, 220, 10);
    stroke(10, 90, 10);
    ellipse(0, 0, 205, 205);
    popMatrix();
  }
  
  void HUD() {
    textAlign(CENTER);
    textSize(20);
    fill(220, 255, 220);
    text("LEFT/RIGHT\nARROW\nSPACE TO SHOOT", width/2, height/2-30);
  
    textAlign(LEFT);
    text("SCORE : " + nf(score, 10, 0), 20, 20);
    if (score > bestScore) {
      bestScore = score;
    }
    textAlign(RIGHT);
    text("BEST SCORE : " + nf(bestScore, 10, 0), width - 20, 20);
  }
  
  void newInvaders() {
    float mag = invaders.get(invaders.size()-1).pos.mag();
    if (mag < 300) {
      for (float i = 0; i < 2*PI; i+= 2*PI/25) {
        invaders.add(new Invader(new PVector(mag*cos(i), mag*sin(i)), i, !invaders.get(invaders.size()-1).movingClockwise));
      }
    }
  }
  class Bullet{
    PVector pos,dir;
    
    float angle;
    boolean hit;
    
    Bullet(PVector pos0, PVector dir0, float a){
      pos = pos0.copy();
      dir = dir0;
      angle = a;
      hit = false;
    }
    
    void show(){
      pushMatrix();
      translate(width/2,height/2);
      rotate(angle);
      fill(10,220,10);
      stroke(10,90,10);
      beginShape();
      vertex(pos.x-5,pos.y-5);
      vertex(pos.x-5,pos.y+5);
      vertex(pos.x+5,pos.y+5);
      vertex(pos.x+5,pos.y-5);
      
      endShape(CLOSE);   
      popMatrix(); 
      
    }
    
    void update(){
     pos.add(dir);    
    }
  }
  class Invader{
    PVector pos;
    float angle;
    boolean movingClockwise = false;
    ArrayList<Bullet> bullets = new ArrayList<Bullet>();
    
    Invader(PVector p0, float a, boolean clock){
      movingClockwise = clock;
      angle = a;
      pos = p0.copy();
    }
    void shoot(){
      if(random(1) < 0.0005){
        PVector bPos = pos.copy();
        PVector bDir = pos.copy().mult(-1).setMag(5);
        bullets.add(new Bullet(bPos, bDir, angle));
      
      }
    }
    void show(){
      pushMatrix();
      translate(width/2,height/2);
      rotate(angle);
      fill(10,220,10);
      stroke(10,90,10);
      ellipseMode(CENTER);
      ellipse(pos.x, pos.y, 20, 20);
      popMatrix();
    }
    void update(){
       shoot();
       if(movingClockwise){
         angle-=0.005;       
       } else {
         angle +=0.005; 
       }
      
      if(frameCount%600 == 0){
        movingClockwise = !movingClockwise;
        pos.setMag(pos.mag()-10);
      }
      
      if(bullets != null){
       for(int i = bullets.size()-1; i>-1; i--){
        bullets.get(i).update();
        bullets.get(i).show();
        if(bullets.get(i).pos.mag() < 100){
         bullets.remove(i); 
        }
       }
       println(bullets.size());
      }
    }
    
    boolean hit(Bullet b){
     if(PVector.dist(b.pos.copy().rotate(b.angle),pos.copy().rotate(angle))<10){
       return true;
     }
     return false;
       
      
    }
  }
  
  class Tank {
    PVector pos = new PVector(0, -100);
    float angle = 0;
    boolean moving = false;
    boolean movingClockwise = false;
    ArrayList<Bullet> bullets = new ArrayList<Bullet>();
    Tank() {
    }
  
    void show() {
      pushMatrix();
      translate(width/2, height/2);
      rotate(angle);
      fill(10, 220, 10);
      stroke(10, 90, 10);
      beginShape();
      vertex(pos.x-20, pos.y);
      vertex(pos.x-20, pos.y-10);
      vertex(pos.x-10, pos.y-10);
      vertex(pos.x-10, pos.y-20);
      vertex(pos.x+10, pos.y-20);
      vertex(pos.x+10, pos.y-10);
      vertex(pos.x+20, pos.y-10);
      vertex(pos.x+20, pos.y); 
  
      endShape(CLOSE);   
      popMatrix();
    }
    boolean hit(Bullet b) {
      if (PVector.dist(b.pos.copy().rotate(b.angle), pos.copy().rotate(angle))<20) {
        return true;
      }
      return false;
    }
    void update() {
      if (moving) {
        if (movingClockwise) {
          angle+=0.05;
        } else {
          angle -=0.05;
        }
      }  
      if (bullets != null) {
        for (int i = bullets.size()-1; i>-1; i--) {
          bullets.get(i).update();
          bullets.get(i).show();
          if (bullets.get(i).pos.mag() > width) {
            bullets.remove(i);
          }
        }
        println(bullets.size());
      }
    }
  }
  
  void keyPressed(){
   if(keyCode == LEFT){
    tank.moving = true;
    tank.movingClockwise = false; 
   }
    if(keyCode == RIGHT){
    tank.moving = true;
    tank.movingClockwise = true; 
   }  
   if(key == ' '){
    if(!shooting){ 
    tank.bullets.add(new Bullet(tank.pos, new PVector(0,-10),tank.angle));
    shooting = !shooting;
    }
   } 
  }
  void keyReleased(){
   if(keyCode == LEFT){
    tank.moving = false;
   }
    if(keyCode == RIGHT){
    tank.moving = false;
   } 
    if(key == ' '){
    shooting = !shooting;
    }
  }

1

u/OlympicGurn Apr 06 '17

My best is 12000.

→ More replies (1)

1

u/Djokjula Apr 09 '17

A bit late, but here is my entry:

https://github.com/djokjulapfe/PWC/tree/master

It is not finished, but wait a few hours :)

1

u/seoceojoe Apr 09 '17

I don't end the contest until Monday morning UK time :) so you got about 12 hours!

→ More replies (1)

1

u/Djokjula Apr 09 '17

All done!

1

u/OlympicGurn Apr 10 '17

27788, had some fun turning the framerate up and getting anihilated.

2

u/Wootai Apr 05 '17

I think this is feature complete at this point so I'll post it.
https://github.com/Wootai/SpaceInvaders

1

u/ChuckEye Apr 05 '17

Random Space Invader-inspired sprite with horizontal symmetry:

boolean[] Cell = new boolean[15];
int CellSize = 8;

void setup() {
  size(100, 100);
  rectMode(CENTER);
  background(0);
  noStroke();
  frameRate(5);
}

void draw() {
  translate(width/2, height/2);
  scale(CellSize);

  for (int y = 0; y < 5; ++y) {
    for (int x = 0; x < 3; ++x) {
      int i = (3 * y) + x;
      Cell[i] = boolean(int(random(10)/5));
      if (Cell[i]) { 
        fill(0, 255, 0);
        rect(0-x, 3-y, 1, 1);
        rect(x, 3-y, 1, 1);
      } else { 
        fill(0);
        rect(0-x, 3-y, 1, 1);
        rect(x, 3-y, 1, 1);
      }
    }
  }
}