Moving Around And Shooting

i not see the code for it??

http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Nl$898UQxW3Q

Studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp

I meant can you help me adding ‘wasd’ to my code

ok, that i did not understand. good,

now as you see in @GoToLoop excellent example
there is that WASD and arrow key operation included.
see keyPressed and keyReleased
in a way that you can shoot ( mouse pressed ) and move
at the same time!!
perfect!

could you do it for me plz i dont understand

from this

//0: GUI
//1: GamePlay
//2: GameOver


ArrayList<astroid> astroids = new ArrayList<astroid>();
ArrayList <shot> shots = new ArrayList <shot> ();
PVector player, playerSpeed;
PVector pos, y;
float maxSpeed = 3;
int Scene = 0;
int astroid_rate = 2 * 60;
int astroid_count = 0;
float ast_size = 10;
int ast_id = 1;
int score = 0;
float hitRate = 0;
int numShots = 0;
int ships = 3;

int pause = 0;

void setup() {
  size (600, 600);
  frameRate(60);
  pos = new PVector(25, 25);
  noCursor();
  noStroke();
  smooth();
  fill (255);
  player = new PVector(width/2, height/2);
  playerSpeed = new PVector();
}

void draw() {
  if (Scene == 0) {
    GUI();
  } else if (Scene == 1) {
    GamePlay();
  } else if (Scene == 3) {
    GameOver();
  }
}
//Input
public void keyPressed() {
  if (Scene == 0) {
    Scene = 1;
    GamePlay();
  }

  if (Scene == 2) {
    Scene=0;
    GUI();
  }
}





//Scene Functions
void GUI() {
  background(189, 211, 161);
  textAlign(CENTER);
  fill(0);
  textSize(50);
  text("Agonising Asteroids", width/2, height/2);
  textSize(15);
  text("Start", width/2, height - 105);
}
void GamePlay() {
  
  player.add(playerSpeed);
  fill(255, 0, 0);
  ellipse(player.x, player.y, 20, 20);
  fill(255);
  ellipse(player.x, player.y, 10, 10);
  
  int i;
  // Find the angle from x=250, y=250 to the mouse
  float angle = atan2(mouseY - 250, mouseX - 250);

  if (pause==0) {

    // 1 new astroid every 5 seconds (60 fps * 4 sec)
    if (astroid_count--==0) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.5), random(0.5, 4), random(-0.1, 0.1), 
        random(-150, 150), random(-150, 150), ast_id++));
      // Increase rate just a little
      astroid_count = astroid_rate--;
    }

    // Clear screen, black
    background(0);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
      // Detect collisions with Astroids by approximating ship with 4 circles
      // fill(160, 33, 100);  
      // ellipse(250, 250, 11, 11);
      // ellipse(13*cos(angle-PI)+250, 13*sin(angle-PI)+250, 17, 17);
      // ellipse(10*cos(angle)+250, 10*sin(angle)+250, 7, 7);
      // ellipse(18*cos(angle)+250, 18*sin(angle)+250, 2, 2);
      if (a.coll(250, 250, 6, -1) ||
        a.coll(13*cos(angle-PI)+250, 13*sin(angle-PI)+250, 9, -1) ||
        a.coll(10*cos(angle)+250, 10*sin(angle)+250, 4, -1) ||
        a.coll(18*cos(angle)+250, 18*sin(angle)+250, 1, -1)) {
        ships--;
        pause=3*60;
      }
    }

    // "pushMatrix" saves current viewpoint
    pushMatrix();
    // Set 250,250 as the new 0,0 
    translate(250, 250);
    // Rotate screen "angle" 
    rotate(angle);
    fill(255);
    // Draw a triangle (the ship)
    translate(pos.x, pos.y);
    triangle(20, 0, -20, -10, -20, 10);
    // Bring back normal perspektive
    popMatrix();
  } else {
    // Pause is larger than 0
    // Clear screen, black
    background(0, 10);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      a.incSpeed();
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
    }
    if (ships == 0) {
      // Clear screen, black
      textAlign(CENTER);
      text("Game Over", width/2, height/2);
      text("Press any key to restart", width/2, 2*height/3);
      // 1 new astroid every 0.5 seconds (60 fps * 0.5 sec)
      // To make something happen while waiting 
      if (astroid_count--==0) {
        astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
          random(-150, 150), random(-150, 150), ast_id++));
        // Increase rate just a little
        astroid_count = 30;
      }
      if (keyPressed == true) {
        score = 0;
        numShots = 0;
        ships = 3;
        astroid_rate = 3 * 60;
        astroid_count = 0;
        ast_id = 1;
        astroids = new ArrayList<astroid>();
      }
    } else {
      // Wait until astroids are gone
      if (astroids.size()==0) {
        pause=0;
      }
    }
  }
  // Go through all shots (if any) and update their position
  for (i = 0; i<shots.size(); i++) {
    shot s = shots.get(i);
    if (s.update()) {
      // Remove bullet, if outside screen or if hits astroid
      shots.remove(i);
    }
  }
  textAlign(LEFT);
  text("Score   : " + score, 15, 15);
  text("Ships   : " + ships, 15, 30);
  text("Hit rate: " + int(100*score/float(numShots)) + "%", 15, 45);
}

// When left mouse button is pressed, create a new shot
void mousePressed() {
  if (pause==0) {
    // Only add shots when in action
    if (mouseButton == LEFT) {
      float angle = atan2(mouseY - 250, mouseX - 250);
      shots.add(new shot(angle, 4));
      numShots++;
    }
    if (mouseButton == RIGHT) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
        random(-80, 80), random(-80, 80), ast_id++));
    }
  }
}

// Class definition for the shot
class shot {
  // A shot has x,y, and speed in x,y. All float for smooth movement
  float angle, speed;
  float x, y, x_speed, y_speed;

  // Constructor  
  shot(float _angle, float _speed) {
    angle = _angle;
    speed = _speed;
    x_speed = speed*cos(angle);
    y_speed = speed*sin(angle);
    x = width/2+20*cos(angle);
    y = height/2+20*sin(angle);
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x + x_speed;
    y = y + y_speed;

    // Draw bullet
    ellipse (x, y, 3, 3);

    // Check for collisions
    // Go through all astroids (if any)
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.coll(x, y, 3, -1)) {
        score++;
        ast_id++;
        astroids.remove(i);
        //Remove bullet
        return true;
      }
    }
    // End, check if outside screen
    if (x<0 || x>width || y<0 || y>height) {
      return true;
    } else {
      return false;
    }
  }
}



// Class definition for the shot
class astroid {
  // An astroid angle, speed, size, rotation
  float angle, speed, size, rotSpeed;
  float position;
  float rotation;
  float xoff, yoff;
  float x, y;
  PShape s;  // The PShape object - Keeps the astroid shape
  float i;
  int id;


  // Constructor  
  astroid(float _angle, float _speed, float _size, float _rotSpeed, float _xoff, float _yoff, int _id) {
    angle = _angle;
    speed = _speed;
    size = _size;
    rotSpeed = _rotSpeed;
    xoff = _xoff;
    yoff = _yoff;
    id = _id;
    if (xoff<1000) {
      x = 250+500*cos(angle)+xoff;
      y = 250+500*sin(angle)+yoff;
    } else {
      x = _xoff-2000;
      y = _yoff-2000;
    }
    rotation = 0; 
    // Generate the shape of the astroid - Some variations for all
    s = createShape();
    s.beginShape();
    s.fill(255, 255, 100);
    s.noStroke();
    for (i=0; i<TWO_PI; i=i+PI/(random(4, 11))) {
      s.vertex(random(ast_size*0.8, ast_size*1.2)*cos(i), random(ast_size*0.8, ast_size*1.2)*sin(i));
    }
    s.endShape(CLOSE);
  }

  // Increases the speed. Used in the end of the game to clear screen of astroids
  void incSpeed() {
    speed = speed * 1.02;
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x - cos(angle)*speed;
    y = y - sin(angle)*speed;
    rotation = rotation + rotSpeed; 

    // Check for astroid vs astroid collision
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if ((a != this) && (a.coll(x, y, ast_size*size, id))) {
        if (size > 1) {
          astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
          astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));    
          ast_id++;
        }
        astroids.remove(i);
      }
    }

    pushMatrix();
    // Set position as the new 0,0 
    translate(x, y);
    // Rotate screen "angle" 
    rotate(rotation);
    // Draw astroid
    scale(size);
    shape(s, 0, 0);
    // Bring back normal perspektive
    popMatrix();

    if (x<-300 || x>800 || y<-300 || y>800) {
      return true;
    } else {
      return false;
    }
  }

  //
  boolean coll(float _x, float _y, float _size, int _id) {
    float dist;

    dist = sqrt ((x-_x)*(x-_x) + (y-_y)*(y-_y));

    // Check if distance is shorter than astroid size and other objects size
    if ((dist<(_size+ast_size*size)) && (id!=_id)) {
      // Collision, 
      if (_id>0) id = _id;
      if (size > 1) {
        // If the astroid was "large" generate two new fragments
        astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
        astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
      }
      return true;
    } else { 
      return false;
    }
  }
  void keyPressed() {
  if (keyCode == UP)    { playerSpeed.y = -maxSpeed; }
  if (keyCode == DOWN)  { playerSpeed.y = maxSpeed;  }
  if (keyCode == LEFT)  { playerSpeed.x = -maxSpeed; }
  if (keyCode == RIGHT) { playerSpeed.x = maxSpeed;  }
}
 
void keyReleased() {
  if (keyCode == UP || keyCode == DOWN)    { playerSpeed.y = 0; }
  if (keyCode == LEFT || keyCode == RIGHT) { playerSpeed.x = 0; }
}
  }



void GameOver () {
  background(100);
  textAlign(CENTER);
  fill(7);
  textSize(50);
  text("Game Over", width/2, height/2);
}

the program runs, but keyboard not function??

-a- why you put it inside a class
-b- why you put it in the asteroids class
-c- you adjust PVector

playerSpeed

but it is used

  player.add(playerSpeed);
  fill(255, 0, 0);
  ellipse(player.x, player.y, 20, 20);
  fill(255);
  ellipse(player.x, player.y, 10, 10);

ahm, i not see any circles???

so for start move it to MAIN like

//Input
void keyPressed() {
  if (Scene == 0) {
    Scene = 1;
    GamePlay();
  }
  if (Scene == 2) {
    Scene=0;
    GUI();
  }

  if (keyCode == UP)    playerSpeed.y = -maxSpeed;
  if (keyCode == DOWN)  playerSpeed.y = maxSpeed;
  if (keyCode == LEFT)  playerSpeed.x = -maxSpeed;
  if (keyCode == RIGHT) playerSpeed.x = maxSpeed;
}

void keyReleased() {
  if (keyCode == UP || keyCode == DOWN)    playerSpeed.y = 0;
  if (keyCode == LEFT || keyCode == RIGHT) playerSpeed.x = 0;
}


So ive done what you asked and i added

player.add(playerSpeed);

to the triangle but it doesnt seem to move
here is my code

//0: GUI
//1: GamePlay
//2: GameOver


ArrayList<astroid> astroids = new ArrayList<astroid>();
ArrayList <shot> shots = new ArrayList <shot> ();
PVector player, playerSpeed;
PVector pos, y;
float maxSpeed = 3;
int Scene = 0;
int astroid_rate = 2 * 60;
int astroid_count = 0;
float ast_size = 10;
int ast_id = 1;
int score = 0;
float hitRate = 0;
int numShots = 0;
int ships = 3;

int pause = 0;

void setup() {
  size (600, 600);
  frameRate(60);
  pos = new PVector(25, 25);
  noCursor();
  noStroke();
  smooth();
  fill (255);
  player = new PVector(width/2, height/2);
  playerSpeed = new PVector();
}

void draw() {
  if (Scene == 0) {
    GUI();
  } else if (Scene == 1) {
    GamePlay();
  } else if (Scene == 3) {
    GameOver();
  }
}
//Input
public void keyPressed() {
  if (Scene == 0) {
    Scene = 1;
    GamePlay();
  }

  if (Scene == 2) {
    Scene=0;
    GUI();
  }
  if (keyCode == UP)    playerSpeed.y = -maxSpeed;
  if (keyCode == DOWN)  playerSpeed.y = maxSpeed;
  if (keyCode == LEFT)  playerSpeed.x = -maxSpeed;
  if (keyCode == RIGHT) playerSpeed.x = maxSpeed;
}

void keyReleased() {
  if (keyCode == UP || keyCode == DOWN)    playerSpeed.y = 0;
  if (keyCode == LEFT || keyCode == RIGHT) playerSpeed.x = 0;
}







//Scene Functions
void GUI() {
  background(189, 211, 161);
  textAlign(CENTER);
  fill(0);
  textSize(50);
  text("Agonising Asteroids", width/2, height/2);
  textSize(15);
  text("Start", width/2, height - 105);
}
void GamePlay() {
  
  int i;
  // Find the angle from x=250, y=250 to the mouse
  float angle = atan2(mouseY - 250, mouseX - 250);

  if (pause==0) {

    // 1 new astroid every 5 seconds (60 fps * 4 sec)
    if (astroid_count--==0) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.5), random(0.5, 4), random(-0.1, 0.1), 
        random(-150, 150), random(-150, 150), ast_id++));
      // Increase rate just a little
      astroid_count = astroid_rate--;
    }

    // Clear screen, black
    background(0);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
      // Detect collisions with Astroids by approximating ship with 4 circles
      // fill(160, 33, 100);  
      // ellipse(250, 250, 11, 11);
      // ellipse(13*cos(angle-PI)+250, 13*sin(angle-PI)+250, 17, 17);
      // ellipse(10*cos(angle)+250, 10*sin(angle)+250, 7, 7);
      // ellipse(18*cos(angle)+250, 18*sin(angle)+250, 2, 2);
      if (a.coll(250, 250, 1, 1) ||
        a.coll(13*cos(angle-PI)+250, 13*sin(angle-PI)+250, 9, -1) ||
        a.coll(10*cos(angle)+250, 10*sin(angle)+250, 4, -1) ||
        a.coll(18*cos(angle)+250, 18*sin(angle)+250, 1, -1)) {
        ships--;
        pause=3*60;
      }
    }

    // "pushMatrix" saves current viewpoint
    pushMatrix();
    // Set 250,250 as the new 0,0 
    translate(250, 250);
    // Rotate screen "angle" 
    rotate(angle);
    fill(255);
    // Draw a triangle (the ship)
    player.add(playerSpeed);
    translate(pos.x, pos.y);
    triangle(20, 0, -20, -10, -20, 10);
    // Bring back normal perspektive
    popMatrix();
  } else {
    // Pause is larger than 0
    // Clear screen, black
    background(0, 10);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      a.incSpeed();
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
    }
    if (ships == 0) {
      // Clear screen, black
      textAlign(CENTER);
      text("Game Over", width/2, height/2);
      text("Press any key to restart", width/2, 2*height/3);
      // 1 new astroid every 0.5 seconds (60 fps * 0.5 sec)
      // To make something happen while waiting 
      if (astroid_count--==0) {
        astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
          random(-150, 150), random(-150, 150), ast_id++));
        // Increase rate just a little
        astroid_count = 30;
      }
      if (keyPressed == true) {
        score = 0;
        numShots = 0;
        ships = 3;
        astroid_rate = 3 * 60;
        astroid_count = 0;
        ast_id = 1;
        astroids = new ArrayList<astroid>();
      }
    } else {
      // Wait until astroids are gone
      if (astroids.size()==0) {
        pause=0;
      }
    }
  }
  // Go through all shots (if any) and update their position
  for (i = 0; i<shots.size(); i++) {
    shot s = shots.get(i);
    if (s.update()) {
      // Remove bullet, if outside screen or if hits astroid
      shots.remove(i);
    }
  }
  textAlign(LEFT);
  text("Score   : " + score, 15, 15);
  text("Ships   : " + ships, 15, 30);
  text("Hit rate: " + int(100*score/float(numShots)) + "%", 15, 45);
}

// When left mouse button is pressed, create a new shot
void mousePressed() {
  if (pause==0) {
    // Only add shots when in action
    if (mouseButton == LEFT) {
      float angle = atan2(mouseY - 250, mouseX - 250);
      shots.add(new shot(angle, 4));
      numShots++;
    }
    if (mouseButton == RIGHT) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
        random(-80, 80), random(-80, 80), ast_id++));
    }
  }
}

// Class definition for the shot
class shot {
  // A shot has x,y, and speed in x,y. All float for smooth movement
  float angle, speed;
  float x, y, x_speed, y_speed;

  // Constructor  
  shot(float _angle, float _speed) {
    angle = _angle;
    speed = _speed;
    x_speed = speed*cos(angle);
    y_speed = speed*sin(angle);
    x = width/2+20*cos(angle);
    y = height/2+20*sin(angle);
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x + x_speed;
    y = y + y_speed;

    // Draw bullet
    ellipse (x, y, 3, 3);

    // Check for collisions
    // Go through all astroids (if any)
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.coll(x, y, 3, -1)) {
        score++;
        ast_id++;
        astroids.remove(i);
        //Remove bullet
        return true;
      }
    }
    // End, check if outside screen
    if (x<0 || x>width || y<0 || y>height) {
      return true;
    } else {
      return false;
    }
  }
}



// Class definition for the shot
class astroid {
  // An astroid angle, speed, size, rotation
  float angle, speed, size, rotSpeed;
  float position;
  float rotation;
  float xoff, yoff;
  float x, y;
  PShape s;  // The PShape object - Keeps the astroid shape
  float i;
  int id;


  // Constructor  
  astroid(float _angle, float _speed, float _size, float _rotSpeed, float _xoff, float _yoff, int _id) {
    angle = _angle;
    speed = _speed;
    size = _size;
    rotSpeed = _rotSpeed;
    xoff = _xoff;
    yoff = _yoff;
    id = _id;
    if (xoff<1000) {
      x = 250+500*cos(angle)+xoff;
      y = 250+500*sin(angle)+yoff;
    } else {
      x = _xoff-2000;
      y = _yoff-2000;
    }
    rotation = 0; 
    // Generate the shape of the astroid - Some variations for all
    s = createShape();
    s.beginShape();
    s.fill(255, 255, 100);
    s.noStroke();
    for (i=0; i<TWO_PI; i=i+PI/(random(4, 11))) {
      s.vertex(random(ast_size*0.8, ast_size*1.2)*cos(i), random(ast_size*0.8, ast_size*1.2)*sin(i));
    }
    s.endShape(CLOSE);
  }

  // Increases the speed. Used in the end of the game to clear screen of astroids
  void incSpeed() {
    speed = speed * 1.02;
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x - cos(angle)*speed;
    y = y - sin(angle)*speed;
    rotation = rotation + rotSpeed; 

    // Check for astroid vs astroid collision
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if ((a != this) && (a.coll(x, y, ast_size*size, id))) {
        if (size > 1) {
          astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
          astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));    
          ast_id++;
        }
        astroids.remove(i);
      }
    }

    pushMatrix();
    // Set position as the new 0,0 
    translate(x, y);
    // Rotate screen "angle" 
    rotate(rotation);
    // Draw astroid
    scale(size);
    shape(s, 0, 0);
    // Bring back normal perspektive
    popMatrix();

    if (x<-300 || x>800 || y<-300 || y>800) {
      return true;
    } else {
      return false;
    }
  }

  //
  boolean coll(float _x, float _y, float _size, int _id) {
    float dist;

    dist = sqrt ((x-_x)*(x-_x) + (y-_y)*(y-_y));

    // Check if distance is shorter than astroid size and other objects size
    if ((dist<(_size+ast_size*size)) && (id!=_id)) {
      // Collision, 
      if (_id>0) id = _id;
      if (size > 1) {
        // If the astroid was "large" generate two new fragments
        astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
        astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
      }
      return true;
    } else { 
      return false;
    }
}
  }



void GameOver () {
  background(100);
  textAlign(CENTER);
  fill(7);
  textSize(50);
  text("Game Over", width/2, height/2);
}

i think i repair the moving and the angle errors
need testing!!!

you have several

mouseY - 250, mouseX - 250

related to a old screen size setting, can not use, must use now variable player position.

REV: missed the 250 from the collision ship / asteroids
but the asteroid constructor still use some 250??

// https://discourse.processing.org/t/need-help-for-moving-around-and-shooting/8652/32

//0: GUI
//1: GamePlay
//2: GameOver

ArrayList<astroid> astroids = new ArrayList<astroid>();
ArrayList <shot> shots = new ArrayList <shot> ();
PVector player, playerSpeed;
//PVector pos, y;
float maxSpeed = 3;
int Scene = 0;
int astroid_rate = 2 * 60;
int astroid_count = 0;
float ast_size = 10;
int ast_id = 1;
int score = 0;
float hitRate = 0;
int numShots = 0;
int ships = 3;

int pause = 0;
boolean showhitcirc = true;  // KLL pls set to false!

void setup() {
  size (600, 600);
  //  frameRate(60);
  //  pos = new PVector(25, 25);
  //test  noCursor();
  noStroke();
  //  smooth();
  fill (255);
  player = new PVector(width/2, height/2);
  playerSpeed = new PVector();
}

void draw() {
  if      (Scene == 0)  GUI();
  else if (Scene == 1)  GamePlay();
  else if (Scene == 3)  GameOver();
}

// while key pressed spaceship moves, righthand arrow keys, lefthand wasd keys
public void keyPressed() {
  if (Scene == 0)    Scene = 1;
  if (Scene == 2)    Scene = 0;
  if (keyCode == UP   || key == 'w' )  playerSpeed.y = -maxSpeed;
  if (keyCode == DOWN || key == 's' )  playerSpeed.y = maxSpeed;
  if (keyCode == LEFT || key == 'a' )  playerSpeed.x = -maxSpeed;
  if (keyCode == RIGHT ||key == 'd' )  playerSpeed.x = maxSpeed;
  //println("playerSpeed.x "+playerSpeed.x+" playerSpeed.y "+playerSpeed.y);
}

void keyReleased() {
  if (keyCode == UP   || keyCode == DOWN  || key == 'w' || key == 's' )    playerSpeed.y = 0;
  if (keyCode == LEFT || keyCode == RIGHT || key == 'a' || key == 'd' )    playerSpeed.x = 0;
}

//Scene Functions
void GUI() {
  background(189, 211, 161);
  textAlign(CENTER);
  fill(0);
  textSize(50);
  text("Agonising Asteroids", width/2, height/2);
  textSize(15);
  text("Start", width/2, height - 105);
}

void GamePlay() {
  int i;
  float angle = atan2(mouseY - player.y, mouseX - player.x); //atan2(mouseY - 250, mouseX - 250);
  if (pause==0) {
    // 1 new astroid every 5 seconds (60 fps * 4 sec)
    if (astroid_count--==0) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.5), random(0.5, 4), random(-0.1, 0.1), 
        random(-150, 150), random(-150, 150), ast_id++));
      // Increase rate just a little
      astroid_count = astroid_rate--;
    }

    // Clear screen, black
    background(0);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
      // Detect collisions with Astroids by approximating ship with 4 circles  // KLL new diameter, but not check the "1 -1 " id function!!!
      if (  a.coll(player.x, player.y, 11, 1)
        || a.coll(13*cos(angle-PI)+player.x, 13*sin(angle-PI)+player.y, 16, -1)
        || a.coll(10*cos(angle)+player.x, 10*sin(angle)+player.y, 5, -1)
        || a.coll(18*cos(angle)+player.x, 18*sin(angle)+player.y, 1, -1)         ) 
      {
        ships--;
        pause=3*60;
        println("collision with asteroid! ships: "+ships);
      }
    }

    pushMatrix();                            // "pushMatrix" saves current viewpoint
    player.add(playerSpeed);
    translate(player.x, player.y);           // translate(pos.x, pos.y);
    rotate(angle);                           // Rotate screen "angle" 
    fill(255);
    triangle(20, 0, -20, -10, -20, 10);      // Draw a triangle (the ship)
    popMatrix();                             // Bring back normal perspektive

    if (showhitcirc) {                       //KLL test show circles
      stroke(200, 0, 0); 
      fill(200, 0, 0);
      circle(player.x, player.y, 11);                                   // 1
      circle(13*cos(angle-PI)+player.x, 13*sin(angle-PI)+player.y, 16);  // 9
      circle(10*cos(angle)+player.x, 10*sin(angle)+player.y, 5);        // 4      
      circle(18*cos(angle)+player.x, 18*sin(angle)+player.y, 1);
    }
  } else {
    // Pause is larger than 0
    // Clear screen, black
    background(0, 10);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      a.incSpeed();
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
    }
    if (ships == 0) {
      // Clear screen, black
      textAlign(CENTER);
      text("Game Over", width/2, height/2);
      text("Press any key to restart", width/2, 2*height/3);
      // 1 new astroid every 0.5 seconds (60 fps * 0.5 sec)
      // To make something happen while waiting 
      if (astroid_count--==0) {
        astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
          random(-150, 150), random(-150, 150), ast_id++));
        // Increase rate just a little
        astroid_count = 30;
      }
      if (keyPressed == true) {
        score = 0;
        numShots = 0;
        ships = 3;
        astroid_rate = 3 * 60;
        astroid_count = 0;
        ast_id = 1;
        astroids = new ArrayList<astroid>();
      }
    } else {
      // Wait until astroids are gone
      if (astroids.size()==0) {
        pause=0;
      }
    }
  }
  // Go through all shots (if any) and update their position
  for (i = 0; i<shots.size(); i++) {
    shot s = shots.get(i);
    if (s.update()) {
      // Remove bullet, if outside screen or if hits astroid
      shots.remove(i);
    }
  }
  textAlign(LEFT);
  text("Score   : " + score, 15, 15);
  text("Ships   : " + ships, 15, 30);
  text("Hit rate: " + int(100*score/float(numShots)) + "%", 15, 45);
}

// When left mouse button is pressed, create a new shot
void mousePressed() {
  if (pause==0) {
    // Only add shots when in action
    if (mouseButton == LEFT) {
      float angle = atan2(mouseY - player.y, mouseX - player.x);
      shots.add(new shot(angle, 4));
      numShots++;
    }
    if (mouseButton == RIGHT) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
        random(-80, 80), random(-80, 80), ast_id++));
    }
  }
}

// Class definition for the shot
class shot {
  // A shot has x,y, and speed in x,y. All float for smooth movement
  float angle, speed;
  float x, y, x_speed, y_speed;

  // Constructor  
  shot(float _angle, float _speed) {
    angle = _angle;
    speed = _speed;
    x_speed = speed*cos(angle);
    y_speed = speed*sin(angle);
    x = player.x;   //width/2+20*cos(angle);
    y = player.y;   //height/2+20*sin(angle);
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x + x_speed;
    y = y + y_speed;

    // Draw bullet
    ellipse (x, y, 3, 3);

    // Check for collisions
    // Go through all astroids (if any)
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.coll(x, y, 3, -1)) {
        score++;
        ast_id++;
        astroids.remove(i);
        //Remove bullet
        return true;
      }
    }
    // End, check if outside screen
    if (x<0 || x>width || y<0 || y>height) {
      return true;
    } else {
      return false;
    }
  }
}



// Class definition for the astroid 
class astroid {
  // An astroid angle, speed, size, rotation
  float angle, speed, size, rotSpeed;
  float position;
  float rotation;
  float xoff, yoff;
  float x, y;
  PShape s;  // The PShape object - Keeps the astroid shape
  float i;
  int id;


  // Constructor  
  astroid(float _angle, float _speed, float _size, float _rotSpeed, float _xoff, float _yoff, int _id) {
    angle = _angle;
    speed = _speed;
    size = _size;
    rotSpeed = _rotSpeed;
    xoff = _xoff;
    yoff = _yoff;
    id = _id;
    if (xoff<1000) {
      x = 250+500*cos(angle)+xoff;
      y = 250+500*sin(angle)+yoff;
    } else {
      x = _xoff-2000;
      y = _yoff-2000;
    }
    rotation = 0; 
    // Generate the shape of the astroid - Some variations for all
    s = createShape();
    s.beginShape();
    s.fill(255, 255, 100);
    s.noStroke();
    for (i=0; i<TWO_PI; i=i+PI/(random(4, 11))) {
      s.vertex(random(ast_size*0.8, ast_size*1.2)*cos(i), random(ast_size*0.8, ast_size*1.2)*sin(i));
    }
    s.endShape(CLOSE);
  }

  // Increases the speed. Used in the end of the game to clear screen of astroids
  void incSpeed() {
    speed = speed * 1.02;
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x - cos(angle)*speed;
    y = y - sin(angle)*speed;
    rotation = rotation + rotSpeed; 

    // Check for astroid vs astroid collision
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if ((a != this) && (a.coll(x, y, ast_size*size, id))) {
        if (size > 1) {
          astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
          astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));    
          ast_id++;
        }
        astroids.remove(i);
      }
    }

    pushMatrix();
    // Set position as the new 0,0 
    translate(x, y);
    // Rotate screen "angle" 
    rotate(rotation);
    // Draw astroid
    scale(size);
    shape(s, 0, 0);
    // Bring back normal perspektive
    popMatrix();

    if (x<-300 || x>800 || y<-300 || y>800) {
      return true;
    } else {
      return false;
    }
  }

  //
  boolean coll(float _x, float _y, float _size, int _id) {
    float dist;

    dist = sqrt ((x-_x)*(x-_x) + (y-_y)*(y-_y));

    // Check if distance is shorter than astroid size and other objects size
    if ((dist<(_size+ast_size*size)) && (id!=_id)) {
      // Collision, 
      if (_id>0) id = _id;
      if (size > 1) {
        // If the astroid was "large" generate two new fragments
        astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
        astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
      }
      return true;
    } else { 
      return false;
    }
  }
}

void GameOver () {
  background(100);
  textAlign(CENTER);
  fill(7);
  textSize(50);
  text("Game Over", width/2, height/2);
}

Ok ive done that but the triangle doesnt move using the wasd keys and can you also help me when the triangle moves the hitbox for it goes with it

ive tried changing the UP DOWN LEFT RIGHT keys to ‘w’ ‘s’ ‘a’ ‘d’ keys but it doesnt work

-a- you show code using ARROW KEY but not working,
i did repair that.
-b- if you like the “WASD” keys i would ADD them with OR ||
like

if ( keyCode == UP || key == 'w' ) ...

-c- “i have done that” ? means you not use my code? ok, but you might miss changes…

ok thank you i have done that but do you know how to make the hitbox follow the spaceship (triangle) and if you do know can you help me

yes, there was a other 250 in the collision
ship <> asteroids
i missed too.
also the circle emulation of the ship did not fit, i changed diameter
AND show the circles for test.
you can disable that show after check. ( see new boolean variable )

what i do not understand

-a- did you run the last revision of my code?
-b- what exactly you need to know?

I mean how do you increase the amount asteroids in the game and how to make the background a starry background and make a border so the triangle doesn’t go off the screen and lastly add random little extra lives when you survived over 30s that can be collected
This is my code right now

//0: GUI
//1: GamePlay
//2: GameOver

ArrayList<astroid> astroids = new ArrayList<astroid>();
ArrayList <shot> shots = new ArrayList <shot> ();
PVector player, playerSpeed;
//PVector pos, y;
float maxSpeed = 3;
int Scene = 0;
int astroid_rate = 2 * 60;
int astroid_count = 0;
float ast_size = 10;
int ast_id = 1;
int score = 0;
float hitRate = 0;
int numShots = 0;
int ships = 3;
PImage Yo;

int pause = 0;
boolean showhitcirc = true;  // KLL pls set to false!

void setup() {
  size (600, 600);
  //  frameRate(60);
  //  pos = new PVector(25, 25);
  //test  noCursor();
  noStroke();
  //  smooth();
  fill (255);
  player = new PVector(width/2, height/2);
  playerSpeed = new PVector();
  Yo = loadImage("Yo.jpg");
}

void draw() {
  if      (Scene == 0)  GUI();
  else if (Scene == 1)  GamePlay();
  else if (Scene == 3)  GameOver();
}

// while key pressed spaceship moves, righthand arrow keys, lefthand wasd keys
public void keyPressed() {
  if (keyCode == UP   || key == 'w' )  playerSpeed.y = -maxSpeed;
  if (keyCode == DOWN || key == 's' )  playerSpeed.y = maxSpeed;
  if (keyCode == LEFT || key == 'a' )  playerSpeed.x = -maxSpeed;
  if (keyCode == RIGHT ||key == 'd' )  playerSpeed.x = maxSpeed;
  //println("playerSpeed.x "+playerSpeed.x+" playerSpeed.y "+playerSpeed.y);
}

void keyReleased() {
  if (keyCode == UP   || keyCode == DOWN  || key == 'w' || key == 's' )    playerSpeed.y = 0;
  if (keyCode == LEFT || keyCode == RIGHT || key == 'a' || key == 'd' )    playerSpeed.x = 0;
}

//Scene Functions
void GUI() {
  Yo.resize(600, 600);
  image(Yo, 0, 0);
  textAlign(CENTER);
  fill(#FF0000);
  textSize(50);
  text("Agonising Asteroids", width/2, height/2);
  textSize(15);
  text("Start", width/2, height - 105);
  textSize(20);
  text("Use WASD to move and Mouse to shoot", width/2, height - 50);
}

void GamePlay() {
  int i;
  float angle = atan2(mouseY - player.y, mouseX - player.x); //atan2(mouseY - 250, mouseX - 250);
  if (pause==0) {
    // 1 new astroid every 5 seconds (60 fps * 4 sec)
    if (astroid_count--==0) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.5), random(0.5, 4), random(-0.1, 0.1), 
        random(-150, 150), random(-150, 150), ast_id++));
      // Increase rate just a little
      astroid_count = astroid_rate--;
    }

    // Clear screen, black
    background(0);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
      // Detect collisions with Astroids by approximating ship with 4 circles  // KLL new diameter, but not check the "1 -1 " id function!!!
      if (  a.coll(player.x, player.y, 11, 1)
        || a.coll(13*cos(angle-PI)+player.x, 13*sin(angle-PI)+player.y, 16, -1)
        || a.coll(10*cos(angle)+player.x, 10*sin(angle)+player.y, 5, -1)
        || a.coll(18*cos(angle)+player.x, 18*sin(angle)+player.y, 1, -1)         ) 
      {
        ships--;
        pause=3*60;
        println("collision with asteroid! ships: "+ships);
      }
    }

    pushMatrix();                            // "pushMatrix" saves current viewpoint
    player.add(playerSpeed);
    translate(player.x, player.y);           // translate(pos.x, pos.y);
    rotate(angle);                           // Rotate screen "angle" 
    fill(255);
    triangle(20, 0, -20, -10, -20, 10);      // Draw a triangle (the ship)
    popMatrix();                             // Bring back normal perspektive

    if (showhitcirc) {                       //KLL test show circles
      stroke(200, 0, 0); 
      fill(200, 0, 0);
      circle(player.x, player.y, 11);                                   // 1
      circle(13*cos(angle-PI)+player.x, 13*sin(angle-PI)+player.y, 16);  // 9
      circle(10*cos(angle)+player.x, 10*sin(angle)+player.y, 5);        // 4      
      circle(18*cos(angle)+player.x, 18*sin(angle)+player.y, 1);
    }
  } else {
    // Pause is larger than 0
    // Clear screen, black
    background(0, 10);

    // Go through all astroids (if any) and update their position
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      a.incSpeed();
      if (a.update()) {
        // Remove bullet, if outside screen
        astroids.remove(i);
      }
    }
    if (ships == 0) {
      // Clear screen, black
      textAlign(CENTER);
      text("Game Over", width/2, height/2);
      text("Press any key to restart", width/2, 2*height/3);
      // 1 new astroid every 0.5 seconds (60 fps * 0.5 sec)
      // To make something happen while waiting 
      if (astroid_count--==0) {
        astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
          random(-150, 150), random(-150, 150), ast_id++));
        // Increase rate just a little
        astroid_count = 30;
      }
      if (keyPressed == true) {
        score = 0;
        numShots = 0;
        ships = 3;
        astroid_rate = 3 * 60;
        astroid_count = 0;
        ast_id = 1;
        astroids = new ArrayList<astroid>();
      }
    } else {
      // Wait until astroids are gone
      if (astroids.size()==0) {
        pause=0;
      }
    }
  }
  // Go through all shots (if any) and update their position
  for (i = 0; i<shots.size(); i++) {
    shot s = shots.get(i);
    if (s.update()) {
      // Remove bullet, if outside screen or if hits astroid
      shots.remove(i);
    }
  }
  textAlign(LEFT);
  text("Score   : " + score, 15, 15);
  text("Ships   : " + ships, 15, 30);
  text("Hit rate: " + int(100*score/float(numShots)) + "%", 15, 45);
}

// When left mouse button is pressed, create a new shot
void mousePressed() {
  if (Scene == 0)    Scene = 1;
  if (Scene == 2)    Scene = 0;
  if (pause==0) {
    // Only add shots when in action
    if (mouseButton == LEFT) {
      float angle = atan2(mouseY - player.y, mouseX - player.x);
      shots.add(new shot(angle, 4));
      numShots++;
    }
    if (mouseButton == RIGHT) {
      astroids.add(new astroid(random(0, TWO_PI), random(0.1, 2.0), random(0.5, 4), random(-0.1, 0.1), 
        random(-80, 80), random(-80, 80), ast_id++));
    }
  }
}

// Class definition for the shot
class shot {
  // A shot has x,y, and speed in x,y. All float for smooth movement
  float angle, speed;
  float x, y, x_speed, y_speed;

  // Constructor  
  shot(float _angle, float _speed) {
    angle = _angle;
    speed = _speed;
    x_speed = speed*cos(angle);
    y_speed = speed*sin(angle);
    x = player.x;   //width/2+20*cos(angle);
    y = player.y;   //height/2+20*sin(angle);
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x + x_speed;
    y = y + y_speed;

    // Draw bullet
    ellipse (x, y, 3, 3);

    // Check for collisions
    // Go through all astroids (if any)
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if (a.coll(x, y, 3, -1)) {
        score++;
        ast_id++;
        astroids.remove(i);
        //Remove bullet
        return true;
      }
    }
    // End, check if outside screen
    if (x<0 || x>width || y<0 || y>height) {
      return true;
    } else {
      return false;
    }
  }
}



// Class definition for the astroid 
class astroid {
  // An astroid angle, speed, size, rotation
  float angle, speed, size, rotSpeed;
  float position;
  float rotation;
  float xoff, yoff;
  float x, y;
  PShape s;  // The PShape object - Keeps the astroid shape
  float i;
  int id;


  // Constructor  
  astroid(float _angle, float _speed, float _size, float _rotSpeed, float _xoff, float _yoff, int _id) {
    angle = _angle;
    speed = _speed;
    size = _size;
    rotSpeed = _rotSpeed;
    xoff = _xoff;
    yoff = _yoff;
    id = _id;
    if (xoff<1000) {
      x = 250+500*cos(angle)+xoff;
      y = 250+500*sin(angle)+yoff;
    } else {
      x = _xoff-2000;
      y = _yoff-2000;
    }
    rotation = 0; 
    // Generate the shape of the astroid - Some variations for all
    s = createShape();
    s.beginShape();
    s.fill(255, 255, 100);
    s.noStroke();
    for (i=0; i<TWO_PI; i=i+PI/(random(4, 11))) {
      s.vertex(random(ast_size*0.8, ast_size*1.2)*cos(i), random(ast_size*0.8, ast_size*1.2)*sin(i));
    }
    s.endShape(CLOSE);
  }

  // Increases the speed. Used in the end of the game to clear screen of astroids
  void incSpeed() {
    speed = speed * 1.02;
  }

  // Update position, return true when out of screen
  boolean update() {
    int i;
    x = x - cos(angle)*speed;
    y = y - sin(angle)*speed;
    rotation = rotation + rotSpeed; 

    // Check for astroid vs astroid collision
    for (i = 0; i<astroids.size(); i++) {
      astroid a = astroids.get(i);
      if ((a != this) && (a.coll(x, y, ast_size*size, id))) {
        if (size > 1) {
          astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
          astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));    
          ast_id++;
        }
        astroids.remove(i);
      }
    }

    pushMatrix();
    // Set position as the new 0,0 
    translate(x, y);
    // Rotate screen "angle" 
    rotate(rotation);
    // Draw astroid
    scale(size);
    shape(s, 0, 0);
    // Bring back normal perspektive
    popMatrix();

    if (x<-300 || x>800 || y<-300 || y>800) {
      return true;
    } else {
      return false;
    }
  }

  //
  boolean coll(float _x, float _y, float _size, int _id) {
    float dist;

    dist = sqrt ((x-_x)*(x-_x) + (y-_y)*(y-_y));

    // Check if distance is shorter than astroid size and other objects size
    if ((dist<(_size+ast_size*size)) && (id!=_id)) {
      // Collision, 
      if (_id>0) id = _id;
      if (size > 1) {
        // If the astroid was "large" generate two new fragments
        astroids.add(new astroid(angle-random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
        astroids.add(new astroid(angle+random(PI/5, PI/7), speed+random(0, speed/2), size/2, rotSpeed, 2000+x, 2000+y, id));
      }
      return true;
    } else { 
      return false;
    }
  }
}

void GameOver () {
  background(100);
  textAlign(CENTER);
  fill(7);
  textSize(50);
  text("Game Over", width/2, height/2);
}

yes, you can make a border rectangle, but not need,
just in the movement of the “ship” triangle you need to
check for 0 < x < width and 0 < y < height , one easy way is to use the


but first i assume that the original question,
about moving the player
is solved?

yeah it is resolved thank you but can you add lives so if the ship touches a heart that randomly spawns after surviving 15s

what have you tried so far?