Loading...
Logo
Processing Forum
Hi there!

I'm trying to create a simple simulation of a zombie appocolypse. There will be more people and zombies and hopefully a "zombie patrol" but I need to work this bug out first.

I want it so that if the "zombie" collides with the "citizen", the "citizen" becomes a zombie. The program won't let me convert one class into another and I tried displaying a new class but that won't work either.

Once I figure this out, this project should be all downhill from here! Any help would be GREATLY appreciated!!!

full code

// For this project, I want to simulate a zombie appocolypse. I will be using three objects to represent the zombies, average people and law enforcement. 
//I want to make it so if a zombie touches (bites) a citizen, they will in turn become a zombie
//If a Zombie-Patrol person touches (shoots) a zombie, the zombie  will die.
//if everything works properly, the game will end if everyone becomes a zombie or all the zombies die. 

Zombie myZombie;
newZombie myNewZombie;

float zombieX;
float zombieXspeed = -.3;
float zombieY;
float zombieYspeed =-.3;
float zombieXspeed2 = -.1;
int zombieSize = 30;

Citizen myCitizen;
float citizenX;
float citizenXspeed = .3;
float citizenY;
float citizenYspeed = .3;
int citizenSize = 30;



void setup() {
  size(500, 500);
  //frameRate(30);

  //randomize zombie location
  zombieX = random(200,400);
  zombieY = random(200,400);
  
  //randomize citizen location
  
  citizenX = random(20,355);
  citizenY = random(20,355);
  
  
  
  myZombie = new Zombie(zombieSize);
  myCitizen = new Citizen(citizenSize);
  
//Zombie = ellipse(zombieX,zombieY); 
}

void draw(){
    background(0);
  zombieX += zombieXspeed;
  zombieY += zombieYspeed;
  myZombie.display(zombieX, zombieY);
  myZombie.display(zombieX+40, zombieY+40);
  myZombie.display(zombieX+20, zombieY+20);
  
  citizenX += citizenXspeed;
  citizenY += citizenYspeed;
  myCitizen.display(citizenX, citizenY);
  myCitizen.display(citizenX-40, citizenY-60);
  
 
 
 
 //scene wrapping for first zombie
 
  if (zombieX > width + zombieSize/3){
    zombieX = 0+ zombieSize/3;
  }
  
  if (zombieX < 0){
    zombieX = width - zombieSize/3;
  }
  
    if (zombieY > height + zombieSize/3){
    zombieY = 0+ zombieSize/3;
  }
  
  if (zombieY < 0){
    zombieX = height - zombieSize/3;
  }
  
  if (zombieX+20 == citizenX+20 && zombieY+20 == citizenY+20){
    myNewZombie.display(citizenX,citizenY);
    citizenXspeed = -zombieXspeed;
    citizenYspeed = -zombieYspeed;
  }


}







zombie class


class Zombie {  //class name

  int r; //float zx, zy;

Zombie(int tempR) {
    r = tempR;
  }
  //methods
  void display(float zx, float zy) {
    //Zombie;
    fill(0,200,0);
    ellipse(zx, zy, 20, 20); //zombies will be represented by green circles
  }
}



citizen class

class Citizen {  //class name

  int rc; //float zx, zy;

Citizen(int tempRc) {
    rc = tempRc;
  }
  //methods
  void display(float cx, float cy) {
    //Zombie;
    fill(241,217,120);
    ellipse(cx, cy, 20, 20); //zombies will be represented by green circles
  }
}







attempted "New Zombie" class

class newZombie {  //class name

  int rnz; //float zx, zy;

newZombie(int tempRnz) {
    rnz = tempRnz;
  }
  //methods
  void display(float nzx, float nzy) {
    //Zombie;
    fill(0,200,0);
    ellipse(nzx, nzy, 20, 20); //zombies will be represented by green circles
  }
}

Replies(2)

Here's a better start. You now have one list of beings, and each being remembers it's own position, direction, and what type of being it is.

Copy code
  1. ArrayList beings;

  2. void setup() {
  3.   size(500, 500);
  4.   beings = new ArrayList();
  5.   beings.add( new Being( random(200, 400), random(200, 400), -.3, -.3, true ) ); // First zombie.
  6.   beings.add( new Being( ((Being)beings.get(0)).px+20, ((Being)beings.get(0)).py+20, -.3, -.3, true ) ); // Second zombie.
  7.   beings.add( new Being( ((Being)beings.get(0)).px+40, ((Being)beings.get(0)).py+40, -.3, -.3, true ) ); // Third zombie.
  8.   beings.add( new Being( random(20, 355), random(20, 355), .3, .3, false ) ); // First person.
  9.   beings.add( new Being( ((Being)beings.get(3)).px-40, ((Being)beings.get(3)).py-60, .3, .3, false ) ); // Second person.
  10.   smooth();
  11. }

  12. void draw() {
  13.   background(0);
  14.   for ( int i = 0; i < beings.size(); i++) {
  15.     Being temp = (Being) beings.get(i);
  16.     temp.draw();
  17.   }
  18. }

  19. class Being {
  20.   float px, py, vx, vy;
  21.   boolean isZombie;
  22.   int r;
  23.   color c;
  24.   Being( float px, float py, float vx, float vy, boolean isZombie ) {
  25.     this.px=px;
  26.     this.py=py;
  27.     this.vx=vx;
  28.     this.vy=vy;
  29.     this.isZombie=isZombie;
  30.     r = 20;
  31.   }
  32.   void newSize( int r ) {
  33.     this.r=r;
  34.   }
  35.   void draw() {
  36.     simulate();
  37.     testCollisions();
  38.     render();
  39.   }
  40.   void simulate() {
  41.     px+=vx;
  42.     py+=vy;
  43.     px%=width;
  44.     py%=height;
  45.   }
  46.   void render() {
  47.     pushStyle();
  48.     ellipseMode(CENTER);
  49.     fill(241, 217, 120);
  50.     stroke(0);
  51.     if ( isZombie ) { 
  52.       fill(0, 200, 0);
  53.     }
  54.     ellipse(px, py, r, r);
  55.     popStyle();
  56.   }
  57.   void testCollisions() {
  58.     if ( isZombie ) { 
  59.       return;
  60.     }
  61.     for ( int i = 0; i < beings.size(); i++) {
  62.       Being temp = (Being) beings.get(i);
  63.       if( temp.isZombie && dist( px, py, temp.px, temp.py ) < (temp.r + r) / 2 ){
  64.         isZombie = true;
  65.       }
  66.     }
  67.   }
  68. }

  69. void mousePressed(){
  70.   beings.add( new Being( mouseX, mouseY, 0,0, true ) );
  71. }
To avoid having to use (Being) cast operator every time an element is read from an ArrayList,
declare field variable beings something like below:  

final static List<Being> beings = new ArrayList <Being> ();
// or
final static ArrayList<Being> beings = new ArrayList <Being> ();

It makes beings accepting instances of class Being (or derivatives) only! Otherwise, it won't even compile!!!