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.
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);
}
}
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:
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++;
}
you had the global variables speed etc. repeated which is not necessary and not allowed
//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;
}
//
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
//