# Fighting game java

**URL:** https://discourse.processing.org/t/fighting-game-java/13295
**Category:** Coding Questions
**Created:** [August 10, 2019, 8:41pm UTC](https://discourse.processing.org/t/fighting-game-java/13295 "2019-08-10T20:41:02Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)
#### Post date: [August 10, 2019, 8:41pm UTC](https://discourse.processing.org/t/fighting-game-java/13295/1 "2019-08-10T20:41:02Z")

</div>

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

```auto
// 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

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

```

Warrior class

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

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 11, 2019, 12:42am UTC](https://discourse.processing.org/t/fighting-game-java/13295/2 "2019-08-11T00:42:00Z")

</div>

hi,  
your code gives me error

 ![classsuper](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/76467a5b3b4822f4ebb829fbaf4347f7a5ad6fc6.png)  
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 )

```auto
// 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);
  }
}

```

---

<div class="post-metadata">

### Author: ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)
#### Post date: [August 11, 2019, 1:52am UTC](https://discourse.processing.org/t/fighting-game-java/13295/3 "2019-08-11T01:52:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 11, 2019, 1:58am UTC](https://discourse.processing.org/t/fighting-game-java/13295/4 "2019-08-11T01:58:22Z")

</div>

> [@ctrembla](#):
>
> 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?

---

<div class="post-metadata">

### Author: ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)
#### Post date: [August 11, 2019, 2:00am UTC](https://discourse.processing.org/t/fighting-game-java/13295/5 "2019-08-11T02:00:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [August 12, 2019, 7:06pm UTC](https://discourse.processing.org/t/fighting-game-java/13295/6 "2019-08-12T19:06:31Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)
#### Post date: [August 14, 2019, 7:11pm UTC](https://discourse.processing.org/t/fighting-game-java/13295/7 "2019-08-14T19:11:37Z")

</div>

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.
