//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<Sprite> platforms;
ArrayList<Sprite> walks;
Enemy enemi;
Enemy enemi1;
Enemy enemi2;
Enemy enemi3;
Enemy enemi4;
//initialize variables
int green1, green2, green3, green4, green5,green6;
int bar_of_know;
boolean isGameOver;
boolean chat;
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<Sprite>();
walks = new ArrayList<Sprite>();
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;
}
//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);
}
public boolean isOnPlat(Sprite s, ArrayList<Sprite> walls){
s.center_y+=5;
ArrayList<Sprite>col_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<Sprite> checkCollisionList(Sprite s1, ArrayList<Sprite> list){
ArrayList<Sprite> collision_list = new ArrayList<Sprite>();
for(Sprite p: list){
if(checkCollision(s1, p))
collision_list.add(p);
}
return collision_list;
}
public void resolvePlatformCollisions(Sprite s, ArrayList <Sprite> walls){
s.change_y +=GRAVITY;
s.center_y += s.change_y;
ArrayList<Sprite> 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'){
chat=true;
}
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;
}
}
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();
if(chat){
textSize(32);
text("Yapping about Economics, Social Sciences, Political Sciences, Business", 3200,900);
}
if(isGameOver){
if(bar_of_know==-100){
//put lose screen here
}
else{
//put win screen here
}}}
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==3){
green1=198;
green2=198;
green3=198;
}
else if(bar_of_know==4){
green1=198;
green2=198;
green3=198;
green4=198;
}
else if(bar_of_know==5){
green1=198;
green2=198;
green3=198;
green4=198;
green5=198;
}
else if(bar_of_know==6){
green1=198;
green2=198;
green3=198;
green4=198;
green5=198;
green6=198;
}
else{
green1=0;
green2=0;
green3=0;
green4=0;
green5=0;
green6=0;
}
for(Sprite enemi:walks){
enemi.update();
((AnimatedSprite)enemi).updateAnimation();
}
p.updateAnimation();
resolvePlatformCollisions(p,platforms);
}
void checkDeath(){
boolean collideEnemy = checkCollision(p, enemi);
boolean collideEnemy1 = checkCollision(p, enemi1);
boolean collideEnemy2 = checkCollision(p, enemi2);
boolean collideEnemy3 = checkCollision(p, enemi3);
if(collideEnemy||collideEnemy1||collideEnemy2||collideEnemy3){
bar_of_know--;
if(bar_of_know==-100){
isGameOver=true;
}
}
}
void collectKnowledge(){
if(bar_of_know==10){
isGameOver=true;
}
}
â
So when I add more than 1 enemy using the csv file, only the very first enemy actually deals damage to my âplayerâ. Iâm new to processing and am not sure what would be relevant or not in solving this which is why I posted me entire code. The main points of focus are most likely âcheckCollisionâ, âcreatePlatformsâ and âcheckDeathâ