# Syntax incomplete statement or extra code near 'extraneous input' help

**URL:** <https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 5, 2023, 3:48pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462 "2023-01-05T15:48:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethan1](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Ethan1](https://discourse.processing.org/u/Ethan1)\
**Post date:** [January 5, 2023, 3:48pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/1 "2023-01-05T15:48:58Z")

</div>

I have an assignment to do where there is a stationary target in the middle and enemies move towards the target and the user will need to click over the enemies to destroy them. I have encountered a problem in my Target class when there is a syntax error “syntax incomplete statement or extra code near ‘extraneous input’”. The error is on line 15.

```auto
class Target {
  float x, y; // coordinates of the target
  int radius = 25; // radius of the target
  PImage img; // image for the target

  Target(float x, float y) {
    this.x = x;
    this.y = y;
    img = loadImage("target.png");
  }

  void draw() {
    image(img, x - radius, y - radius);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Ethan1](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Ethan1](https://discourse.processing.org/u/Ethan1)\
**Post date:** [January 5, 2023, 3:49pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/2 "2023-01-05T15:49:40Z")

</div>

if you need to see more code let me know and I will provide some

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2023, 3:55pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/3 "2023-01-05T15:55:48Z")

</div>

we need to see more, since this part is correct

```auto

void setup() {
  size(333, 333);
}

class Target {
  float x, y; // coordinates of the target
  int radius = 25; // radius of the target
  PImage img; // image for the target

  Target(float x, float y) {
    this.x = x;
    this.y = y;
    img = loadImage("target.png");
  }

  void draw() {
    image(img, 
      x - radius, y - radius);
  }
}//class
//

```

---

<div class="post-metadata">

**Author:** ![Ethan1](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Ethan1](https://discourse.processing.org/u/Ethan1)\
**Post date:** [January 5, 2023, 4:08pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/4 "2023-01-05T16:08:55Z")

</div>

enemy class:

```auto
float speed; // speed at which the enemies move
boolean gameOver; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background

class Enemy {
  float x, y; // coordinates of the enemy
  int radius = 25; // radius of the enemy
  boolean alive; // true if the enemy is alive, false if it has been destroyed
  PImage img; // image for the enemy

  Enemy(float x, float y) {
    this.x = x;
    this.y = y;
    alive = true;
    img = loadImage("enemy.png");
  }

  void update(float targetX, float targetY) {
    // move the enemy towards the target
    float dx = targetX - x;
    float dy = targetY - y;
    float angle = atan2(dy, dx);
    x += cos(angle) * speed;
    y += sin(angle) * speed;

    // check for collision between the enemy and the target
    float distance = dist(targetX, targetY, x, y);
    if (distance < radius) {
      gameOver = true; // end the game
    }
  }

  void draw() {
    image(img, x - radius, y - radius);
  }
}

```

main sketch:

```auto
float speed; // speed at which the enemies move
boolean gameOver; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background

Enemy enemy; // enemy object
Target target; // target object

void setup() {
  size(400, 400);
  target = new Target(200, 200);
  enemy = new Enemy(50, 50);
  speed = 2;
  gameOver = false;
  score = 0;
  backgroundImg = loadImage("space.jpg");
}

void draw() {
  image(backgroundImg, 0, 0);

  if (!gameOver) {
    if (enemy.alive) {
      enemy.update(target.x, target.y); // move the enemy towards the target
      enemy.draw(); // draw the enemy
    } else {
      // enemy has been destroyed, spawn a new one
      enemy = new Enemy(random(0, width), random(0, height));
      score++;
    }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2023, 4:23pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/5 "2023-01-05T16:23:21Z")

</div>

this sees to run

remove my class Target

you had the global variables speed etc. repeated which is not necessary and not allowed

```auto

//float speed; // speed at which the enemies move
//boolean gameOver; // true if the game is over, false otherwise
//int score; // current score
//PImage backgroundImg; // image for the background

class Enemy {
  float x, y; // coordinates of the enemy
  int radius = 25; // radius of the enemy
  boolean alive; // true if the enemy is alive, false if it has been destroyed
  PImage img; // image for the enemy

  Enemy(float x, float y) {
    this.x = x;
    this.y = y;
    alive = true;
    img = loadImage("enemy.png");
  }

  void update(float targetX, float targetY) {
    // move the enemy towards the target
    float dx = targetX - x;
    float dy = targetY - y;
    float angle = atan2(dy, dx);
    x += cos(angle) * speed;
    y += sin(angle) * speed;

    // check for collision between the enemy and the target
    float distance = dist(targetX, targetY, x, y);
    if (distance < radius) {
      gameOver = true; // end the game
    }
  }

  void draw() {
    if (img!=null)
      image(img, x - radius, y - radius);
  }
}

// main sketch:

float speed; // speed at which the enemies move
boolean gameOver; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background

Enemy enemy; // enemy object
Target target; // target object

void setup() {
  size(400, 400);
  target = new Target();
  enemy = new Enemy(50, 50);
  speed = 2;
  gameOver = false;
  score = 0;
  backgroundImg = loadImage("space.jpg");
}

void draw() {
  if (backgroundImg!=null)
    image(backgroundImg, 0, 0);

  if (!gameOver) {
    if (enemy.alive) {
      enemy.update(target.x, target.y); // move the enemy towards the target
      enemy.draw(); // draw the enemy
    } else {
      // enemy has been destroyed, spawn a new one
      enemy = new Enemy(random(0, width), random(0, height));
      score++;
    }
  }//if
}//func 

class Target {
  float x, y;
}
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2023, 4:26pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/6 "2023-01-05T16:26:54Z")

</div>

you forgot to post the Target class though

---

<div class="post-metadata">

**Author:** ![Ethan1](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Ethan1](https://discourse.processing.org/u/Ethan1)\
**Post date:** [January 5, 2023, 4:41pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/7 "2023-01-05T16:41:40Z")

</div>

sorry, here’s the target class

```auto
class Target {
  float x, y; // coordinates of the target
  int radius = 25; // radius of the target
  PImage img; // image for the target

  Target(float x, float y) {
    this.x = x;
    this.y = y;
    img = loadImage("target.png");
  }

  void draw() {
    image(img, x - radius, y - radius);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Ethan1](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Ethan1](https://discourse.processing.org/u/Ethan1)\
**Post date:** [January 5, 2023, 4:48pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/9 "2023-01-05T16:48:49Z")

</div>

you’re solution has worked but nothing is being displayed when I run it

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2023, 5:12pm UTC](https://discourse.processing.org/t/syntax-incomplete-statement-or-extra-code-near-extraneous-input-help/40462/10 "2023-01-05T17:12:10Z")

</div>

here

```auto

class Enemy {
  float x, y; // coordinates of the enemy
  int radius = 25; // radius of the enemy
  boolean alive; // true if the enemy is alive, false if it has been destroyed
  PImage img; // image for the enemy

  Enemy(float x, float y) {
    this.x = x;
    this.y = y;
    alive = true;
    img = loadImage("enemy.png");
  }

  void update(float targetX, float targetY) {
    // move the enemy towards the target
    float dx = targetX - x;
    float dy = targetY - y;
    float angle = atan2(dy, dx);
    x += cos(angle) * speed;
    y += sin(angle) * speed;

    // check for collision between the enemy and the target
    float distance = dist(targetX, targetY, x, y);
    if (distance < radius) {
      gameOver = true; // end the game
    }
  }

  void draw() {
    // if (img!=null)
    image(img, x - radius, y - radius);
  }
}

// ====================================================================================

class Target {
  float x, y; // coordinates of the target
  int radius = 25; // radius of the target
  PImage img; // image for the target

  Target(float x, float y) {
    this.x = x;
    this.y = y;
    img = loadImage("target.png");
  }

  void draw() {
    image(img, x - radius, y - radius);
  }
}

// main sketch:

float speed; // speed at which the enemies move
boolean gameOver = false; // true if the game is over, false otherwise
int score; // current score
PImage backgroundImg; // image for the background

Enemy enemy; // enemy object
Target target; // target object

void setup() {
  size(400, 400);

  target = new Target(212, 212);
  enemy = new Enemy(50, 50);
  speed = 2;
  gameOver = false;
  score = 0;
  backgroundImg = loadImage("space.jpg");
}

void draw() {
  image(backgroundImg, 0, 0);

  if (!gameOver) {
    if (enemy.alive) {
      enemy.update(target.x, target.y); // move the enemy towards the target
      enemy.draw(); // draw the enemy
      target.draw();
    } else {
      // enemy has been destroyed, spawn a new one
      enemy = new Enemy(random(0, width), random(0, height));
      score++;
    }
  }//if
}//func 

//

```
