Fighting game java

I’m making a fighting game where warriors come from both sides and meet in the middle. ATM I placed them in the middle because the map not done yet. Anyways I was trying to set up the fight but the way i’m trying to do it isn’t working. Can someone look at my code posted below and tell me if my thought process is even cabable of doing.

in sketch

// here is list of strong brave fighters
Warrior chuck;
Warrior bruce;
Knight carl;
Warrior dave;
Warrior mark;
void setup(){
  size(600,600);
}

void draw(){
  background(0);
}
/*here the problem I need fight to pull in 2 objects sometimes it be warrior vs warrior or knight verse warrior or knight verse knight*/
void fight(Object unit1, Object unit2){
  int round = 0;
  if (round == 0){
    unit2.life -= unit1.attack;
  }
  
}

knight class

class Knight extends Warrior{
  Knight(int x, int y){
    super(x,y);
    attack = 7;
  }
}

Warrior class

class Warrior{
  PVector pos;
  int attack = 5;
  int life = 50;
  boolean isalive = true;
  
  Warrior(int x, int y){
    pos = new PVector(x,y);
  }
}
1 Like

hi,
your code gives me error


is that what you ask about?


if you use a common super class Fighter
with
Warrior and Knight as sub class it is running here ( processing IDE 3.5.3 / win 10 )

// here is list of strong brave fighters
Warrior chuck;
Knight carl;

int round = 0;
PVector start = new PVector(0,0);

void setup() {
  size(600, 600);
  chuck = new Warrior(start);
  carl  = new Knight(start);
}

void draw() {
  background(0);
  fight(carl,chuck);
}

void fight(Fighter unit1, Fighter unit2) {
  if (round == 0) {
    unit2.life -= unit1.attack;
    println("round "+round+" life "+unit2.life);
  }
}

//____________________________________Fighter class
class Fighter {
  PVector pos;
  int attack = 5, life = 50;
  boolean isalive = true;
  public Fighter(PVector pos) {
    this.pos = pos;
  }
}

//____________________________________Warrior class
public class Warrior extends Fighter {
  public Warrior(PVector pos) {
    super(pos);
  }
}

//____________________________________Knight class
public class Knight  extends Fighter {
    public Knight(PVector pos) {
    super(pos);
  }
}


1 Like

Yes it gives that error because it looks for life as global and not under unit1 or unit2. idk how to fix it. So you fixed the problem & I still can just list the enemies like I had them?

not understand that question?
-a- did you run my code on your system?
-b- did you try to make more Fighters ( as Knight or Warrior )?

yes, i reduce your example to just what i needed for test
and change other small things

but for make more Fighters, also you might consider to use “array of class” structure?

ok, wasn’t sure sry I’ll try your code. I’m trying to copy a game i found on a site. It takes you step by step through making a fighting game but its in python.

When copying into a new language, start with a simple program that works (a background). Then add one thing that works (an empty fighter class with no properties). Then add another thing that works (now it draws a square)…and so on. Otherwise I waste a lot of time flailing around.

TY, the problem I’m having with the python after this post is understanding what they are asking of me. I do love your advice of how to change languages.