# How to make multiple enemies affect health

**URL:** https://discourse.processing.org/t/how-to-make-multiple-enemies-affect-health/43209
**Category:** Coding Questions
**Created:** [November 15, 2023, 4:40am UTC](https://discourse.processing.org/t/how-to-make-multiple-enemies-affect-health/43209 "2023-11-15T04:40:12Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![3xotic109](https://avatars.discourse-cdn.com/v4/letter/3/858c86/32.png) [@3xotic109](https://discourse.processing.org/u/3xotic109)
#### Post date: [November 15, 2023, 11:50pm UTC](https://discourse.processing.org/t/how-to-make-multiple-enemies-affect-health/43209/4 "2023-11-15T23:50:59Z")

</div>

Heres my new code:

//Declared Global Variables  
final static float MOVE\_SPEED = 7.5;  
final static float SPRITE\_SCALE = 50.0/64;  
final static float SPRITE\_SCALE1 = 50.0/1920;  
final static float SPRITE\_SCALE2 =50.0/45;  
final static float SPRITE\_SCALE3 = 225.0/600;  
final static float Teach\_Scale=100.0/2800;  
final static float SPRITE\_SIZE = 50;  
final static float GRAVITY= 0.6;  
final static float JUMP\_SPEED = 17.5;

final static float RIGHT\_MARGIN = 400;  
final static float LEFT\_MARGIN = 60;  
final static float VERTICAL\_MARGIN = 40;

final static int NEUTRAL\_FACING = 0;  
final static int RIGHT\_FACING = 1;  
final static int LEFT\_FACING = 2;

final static float WIDTH = SPRITE\_SIZE \* 38.4;  
final static float HEIGHT = SPRITE\_SIZE \* 24;  
final static float GROUND\_LEVEL = HEIGHT-SPRITE\_SIZE;

int sec = second();  
Sprite teach;  
Sprite s1,s2;  
Player p;  
Sprite text;  
PImage tile1, desk, enemy, shelf, p1;  
float view\_x = 0;  
float view\_y =0;  
ArrayList platforms;  
ArrayList walks;  
Enemy enemi;  
Enemy enemi1;  
Enemy enemi2;  
Enemy enemi3;  
Enemy enemi4;  
//initialize variables  
int green1, green2, green3, green4, green5,green6,green7,green8,green9,green10;  
int bar\_of\_know;  
boolean isGameOver;  
boolean chat=false;  
boolean chat1=false;  
boolean chat2=false;  
boolean chat3=false;  
boolean chat4=false;  
boolean chat5=false;  
boolean firstDone=false;  
boolean secondDone=false;  
boolean thirdDone=false;  
boolean fourthDone=false;  
boolean fifthDone=false;  
boolean sixthDone=false;  
boolean reset=false;  
boolean reset1=false;  
boolean reset2=false;  
boolean reset3=false;  
boolean reset4=false;  
boolean firstRun=true;  
void setup(){  
size(1920,1200);  
imageMode(CENTER);  
p1 = loadImage(“walk1.png”);  
teach = new Sprite(“teach.png”, Teach\_Scale, 3200,850);  
p=new Player(p1, 1.0);  
bar\_of\_know=0;  
p.setBottom(400);  
p.center\_x= 100;  
p.change\_x=0;  
p.change\_y=0;  
isGameOver=false;  
platforms = new ArrayList();  
walks = new ArrayList();  
enemy = loadImage(“enemywalk.png”);  
tile1=loadImage(“Tile1.png”);  
desk=loadImage(“Desk.png”);  
shelf = loadImage(“shelf1.png”);  
createPlatforms(“map1.csv”);  
green1 = 0;  
green2 =0;  
green3=0;  
green4=0;  
green5=0;  
green6=0;  
green7=0;  
green8=0;  
green9=0;  
green10=0;  
}

//modify and draw images  
void draw(){  
background(255);  
scroll();

displayAll();  
//if(!isGameOver){  
updateAll();  
//}  
}

void scroll(){  
float right\_boundary = view\_x+width-RIGHT\_MARGIN;  
if(p.getRight()\> right\_boundary){  
view\_x+=p.getRight()-right\_boundary;  
}  
float left\_boundary = view\_x+LEFT\_MARGIN;  
if(p.getLeft()\<left\_boundary){  
view\_x -=left\_boundary-p.getLeft();  
}  
float bottom\_bound = view\_y+height-VERTICAL\_MARGIN;  
if(p.getBottom()\>bottom\_bound){  
view\_y+=p.getBottom()-bottom\_bound;  
}  
float top\_bound = view\_y+VERTICAL\_MARGIN;  
if(p.getTop()\<top\_bound){  
view\_y -= top\_bound-p.getTop();  
}  
translate(-view\_x,-view\_y);  
}

void drawRect(){  
fill(174, green1, 255);  
rect(view\_x, view\_y, 100, 50);  
fill(174,green2, 255);  
rect(view\_x+101, view\_y, 100, 50);  
fill(174,green3, 255);  
rect(view\_x+202, view\_y, 100, 50);  
fill(174,green4, 255);  
rect(view\_x+303, view\_y, 100, 50);  
fill(174,green5, 255);  
rect(view\_x+404, view\_y, 100, 50);  
fill(174,green6, 255);  
rect(view\_x+505, view\_y, 100, 50);  
fill(174,green7, 255);  
rect(view\_x+606, view\_y, 100, 50);  
fill(174,green8, 255);  
rect(view\_x+707, view\_y, 100, 50);  
fill(174,green9, 255);  
rect(view\_x+808, view\_y, 100, 50);  
fill(174,green10, 255);  
rect(view\_x+909, view\_y, 100, 50);  
}

public boolean isOnPlat(Sprite s, ArrayList walls){  
s.center\_y+=5;  
ArrayListcol\_list =checkCollisionList(s, walls);  
s.center\_y-=5;  
if(col\_list.size()\>0){  
return true;  
}  
else {  
return false;  
}  
}

public boolean checkCollision(Sprite s1, Sprite s2){  
boolean noXOverlap = s1.getRight() \<= s2.getLeft() || s1.getLeft()\>=s2.getRight();  
boolean noYOverlap = s1.getBottom() \<= s2.getTop() || s1.getTop()\>=s2.getBottom();  
if (noXOverlap || noYOverlap){  
return false;  
}  
else {  
return true;  
}  
}

ArrayList checkCollisionList(Sprite s1, ArrayList list){  
ArrayList collision\_list = new ArrayList();  
for(Sprite p: list){  
if(checkCollision(s1, p))  
collision\_list.add(p);  
}  
return collision\_list;  
}

public void resolvePlatformCollisions(Sprite s, ArrayList walls){  
s.change\_y +=GRAVITY;  
s.center\_y += s.change\_y;  
ArrayList col\_list = checkCollisionList(s,walls);  
if(col\_list.size()\>0){  
Sprite collided = col\_list.get(0);  
if(s.change\_y\>0){  
s.setBottom(collided.getTop());  
}  
else if(s.change\_y\<0){  
s.setTop(collided.getBottom());  
}  
s.change\_y=0;  
}

s.center\_x+=s.change\_x;  
col\_list = checkCollisionList(s,walls);  
if(col\_list.size()\>0){  
Sprite collided = col\_list.get(0);  
if(s.change\_x\>0){  
s.setRight(collided.getLeft());  
}  
else if(s.change\_x\<0){  
s.setLeft(collided.getRight());  
}  
}

}

void createPlatforms(String filename){  
String lines = loadStrings(filename);  
for (int row=0; row \< lines.length; row++){  
String values = split(lines[row], “,”);  
for(int col = 0; col\<values.length; col++){  
if(values[col].equals(“1”)){  
Sprite t= new Sprite(tile1, SPRITE\_SCALE);  
t.center\_x = SPRITE\_SIZE/2+col_SPRITE\_SIZE;  
t.center\_y=SPRITE\_SIZE/2+row_SPRITE\_SIZE;  
platforms.add(t);  
}  
else if (values[col].equals(“2”)){  
Sprite s = new Sprite(desk, SPRITE\_SCALE1);  
s.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
s.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
platforms.add(s);  
}  
else if (values[col].equals(“3”)){  
Sprite s = new Sprite(shelf, SPRITE\_SCALE3);  
s.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
s.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
platforms.add(s);  
}  
else if (values[col].equals(“4”)){  
float bLeft = col_SPRITE\_SIZE;  
float bRight = bLeft + 8 \* SPRITE\_SIZE;  
enemi = new Enemy(enemy, 50.0/32, bLeft, bRight);  
enemi.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
enemi.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
walks.add(enemi);  
}  
else if (values[col].equals(“5”)){  
float bLeft = col_SPRITE\_SIZE;  
float bRight = bLeft + 7 \* SPRITE\_SIZE;  
enemi = new Enemy(enemy, 50.0/32, bLeft, bRight);  
enemi.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
enemi.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
walks.add(enemi);  
}  
else if (values[col].equals(“6”)){  
float bLeft = col_SPRITE\_SIZE;  
float bRight = bLeft + 6 \* SPRITE\_SIZE;  
enemi = new Enemy(enemy, 50.0/32, bLeft, bRight);  
enemi.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
enemi.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
walks.add(enemi);  
}  
else if (values[col].equals(“7”)){  
float bLeft = col_SPRITE\_SIZE;  
float bRight = bLeft + 5 \* SPRITE\_SIZE;  
enemi = new Enemy(enemy, 50.0/32, bLeft, bRight);  
enemi.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
enemi.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
walks.add(enemi);  
}  
else if (values[col].equals(“8”)){  
float bLeft = col\*SPRITE\_SIZE;  
float bRight = bLeft + 4 \* SPRITE\_SIZE;  
enemi = new Enemy(enemy, 50.0/32, bLeft, bRight);  
enemi.center\_x = SPRITE\_SIZE/2 + col \* SPRITE\_SIZE;  
enemi.center\_y = SPRITE\_SIZE/2 + row \* SPRITE\_SIZE;  
walks.add(enemi);  
}  
}  
}  
}

//call whenever a key is pressed  
void keyPressed(){  
if(keyCode == RIGHT){  
p.change\_x=MOVE\_SPEED;  
}  
else if(keyCode == LEFT){  
p.change\_x=-MOVE\_SPEED;  
}  
else if(keyCode == UP && isOnPlat(p, platforms)){  
p.change\_y= -JUMP\_SPEED;  
}  
else if(isGameOver && key == ’ '){  
setup();  
}  
if(checkCollision(p,teach) && key ==‘f’ && firstRun==true){  
chat=true;  
}  
if(checkCollision(p,teach) && key==‘f’&&reset==true){  
chat1=true;  
}  
else if(checkCollision(p,teach) && key==‘f’ &&reset1==true){  
chat2=true;  
}  
else if(checkCollision(p,teach) && key==‘f’ &&reset2==true){  
chat3=true;  
}  
else if(checkCollision(p,teach) && key==‘f’ &&reset3==true){  
chat4=true;  
}  
else if(checkCollision(p,teach) && key==‘f’ &&reset4==true){  
chat5=true;  
}  
if(key==‘r’){  
p.center\_x=100;  
p.setBottom(800);  
reset=true;  
firstRun=false;  
}  
if(key==‘r’ && bar\_of\_know==4){  
p.center\_x=100;  
p.setBottom(800);  
reset1=true;  
reset=false;  
reset2=false;  
reset3=false;  
reset4=false;  
}  
if(key==‘r’ && bar\_of\_know==6){  
p.center\_x=100;  
p.setBottom(800);  
reset2=true;  
reset1=false;  
reset=false;  
reset3=false;  
reset4=false;  
}  
if(key==‘r’ && bar\_of\_know==8){  
p.center\_x=100;  
p.setBottom(800);  
reset3=true;  
reset=false;  
reset2=false;  
reset1=false;  
reset4=false;  
}  
if(key==‘r’ && bar\_of\_know==10){  
p.center\_x=100;  
p.setBottom(800);  
reset4=true;  
reset=false;  
reset2=false;  
reset3=false;  
reset1=false;  
}

if(key == ESC){  
exit();  
}  
}

//call whenever a key is released  
void keyReleased(){  
if(keyCode == RIGHT){  
p.change\_x=0;  
}  
else if(keyCode == LEFT){  
p.change\_x=0;  
}  
else if(keyCode == UP){  
p.change\_y=0;  
}  
else if(keyCode == DOWN){  
p.change\_y=0;  
}  
else if(key==‘f’){  
chat=false;  
chat1=false;  
chat2=false;  
chat3=false;  
chat4=false;  
chat5=false;  
}  
else if(key==‘r’){

}  
}

void displayAll(){  
for(Sprite s: platforms){  
s.display();  
}  
for(Sprite enemi: walks){  
enemi.display();  
}  
p.display();  
textSize(32);  
text("Bar Of Knowledge: "+bar\_of\_know,view\_x+50,view\_y+100);  
drawRect();  
teach.display();  
//textSize(32);  
//text(“reset:”+reset,view\_x+50,view\_y+200);  
//text(“reset1:”+reset1,view\_x+50,view\_y+300);  
//text(“reset2:”+reset2,view\_x+50,view\_y+400);  
//text(“reset3:”+reset3,view\_x+50,view\_y+500);  
//text(“reset4:”+reset4,view\_x+50,view\_y+600);  
if(chat){  
textSize(32);  
text(“Master the basics of supply and demand,”, 2500,650);  
text(“and connect economic concepts to current”,2500,700);  
text(“events for a practical understanding of”,2500,750);  
text(“how economics influences the world around you.”,2500,800);  
firstDone=true;  
}

if(chat1){  
text(“Master social science basics like culture”, 2500,650);  
text(“and human behavior, and connect them”,2500,700);  
text(“to current events for a practical”,2500,750);  
text(“understanding of the world’s complexities.”,2500,800);  
secondDone=true;  
}  
if(chat2){//CHANGE  
text(“Master social science basics like culture”, 2500,650);  
text(“and human behavior, and connect them”,2500,700);  
text(“to current events for a practical”,2500,750);  
text(“understanding of the world’s complexities.”,2500,800);  
thirdDone=true;  
}  
if(chat3){//CHANGE  
text(“Master social science basics like culture”, 2500,650);  
text(“and human behavior, and connect them”,2500,700);  
text(“to current events for a practical”,2500,750);  
text(“understanding of the world’s complexities.”,2500,800);  
fourthDone=true;  
}  
if(chat4){//CHANGE  
text(“Master social science basics like culture”, 2500,650);  
text(“and human behavior, and connect them”,2500,700);  
text(“to current events for a practical”,2500,750);  
text(“understanding of the world’s complexities.”,2500,800);  
fifthDone=true;  
}  
if(chat5){//CHANGE  
text(“Master social science basics like culture”, 2500,650);  
text(“and human behavior, and connect them”,2500,700);  
text(“to current events for a practical”,2500,750);  
text(“understanding of the world’s complexities.”,2500,800);  
sixthDone=true;  
}  
if(firstDone==true){  
bar\_of\_know=2;  
firstDone=false;  
}  
else if(secondDone==true){  
bar\_of\_know=4;  
secondDone=false;  
}  
else if(thirdDone==true){  
bar\_of\_know=6;  
thirdDone=false;  
}  
else if(fourthDone==true){  
bar\_of\_know=8;  
fourthDone=false;  
}  
else if(fifthDone==true){  
bar\_of\_know=10;  
fifthDone=false;  
}

collectKnowledge();  
if(isGameOver){  
if(bar\_of\_know==-100){  
text(“YOU LOSE”,view\_x+500,view\_y+500);  
}  
else{  
text(“YOU WIN”,view\_x+500,view\_y+500);  
}}}

void updateAll(){  
checkDeath();  
if(bar\_of\_know==1){  
green1=198;  
}  
else if(bar\_of\_know==2){  
green1=198;  
green2=198;  
}  
else if(bar\_of\_know==4){  
green1=198;  
green2=198;  
green3=198;  
green4=198;  
}  
else if(bar\_of\_know==6){  
green1=198;  
green2=198;  
green3=198;  
green4=198;  
green5=198;  
green6=198;  
}  
else if(bar\_of\_know==8){  
green1=198;  
green2=198;  
green3=198;  
green4=198;  
green5=198;  
green6=198;  
green7=198;  
green8=198;  
}  
else if(bar\_of\_know==10){  
green1=198;  
green2=198;  
green3=198;  
green4=198;  
green5=198;  
green6=198;  
green7=198;  
green8=198;  
green9=198;  
green10=198;  
}  
else{  
green1=0;  
green2=0;  
green3=0;  
green4=0;  
green5=0;  
green6=0;  
green7=0;  
green8=0;  
green9=0;  
green10=0;  
}  
for(Sprite enemi:walks){  
enemi.update();  
((AnimatedSprite)enemi).updateAnimation();  
}  
p.updateAnimation();  
resolvePlatformCollisions(p,platforms);  
}

void checkDeath(){  
boolean collideEnemy = checkCollision(p, enemi);

```
 if(collideEnemy){
  bar_of_know--;
  if(bar_of_know==-100){
   isGameOver=true; 
  }
 }

```

}

void collectKnowledge(){  
if(bar\_of\_know==10){  
isGameOver=true;  
}  
}

---

_[View the full topic](https://discourse.processing.org/t/how-to-make-multiple-enemies-affect-health/43209)._
