太空游戏编码碰撞检测

时间:2015-03-11 11:29:06

标签: arraylist collision-detection game-development



ArrayList<bullet> bulletList = new ArrayList<bullet>();

spaceShip s;


//array of stars 
ArrayList stars;
ArrayList Planets;


//Planet[] planets = new Planet[20];//number of planets in array

void setup() {
  //position new spaceship in the centre of the screen, pointing upwards
  s = new spaceShip(width/2, height/2, HALF_PI);

  size(600, 600, P3D);//3D coordinate systemsize(500, 500);
  background(0);
  //planets
  Planets = new ArrayList();
  for (int j = 1; j<= 20; j++) {
    Planets.add(new Planets(60+j*20, 5+j));
  }
  //background stars array list
  stars = new ArrayList();
  for (int i = 1; i <= 300; i++) {
    //for every variable in the list, add a new star
    stars.add(new star());
  }
}


//collision check for bullet and planet

boolean checkCollision(ArrayList Planet) {
  if (Bullet.position == Planets.diameter) {
    Planets.remove(i);
    return true;
  }
}

void draw() {

  noCursor();//no cursor 
  lights();//standard lights to enhance 3D planets
  background(0);
  sphereDetail(8);//detailing of sphere

  //initialising the bullet array list, getting elements of the array
  for (int i = 0; i < bulletList.size (); i ++) {
    bullet b = bulletList.get(i);
    b.run();
    if (bulletList.get(i).checkCollision(Planet)) {
      bullets.remove(i);
      i--;
      Planet.remove(i);
    }
  }
  s.run();
  //background stars
  for (int i = 0; i <= stars.size ()-1; i++) {//iterating through star array
    star starUse = (star) stars.get(i); //get element from star arraylist
    starUse.display();//display element
  }
  translate(width/2, height/2);//translate coordinate system of planets to centre of the screen
  for (int j = 0; j <= Planet.size ()-1; j++) {
    Planet P = (Planet) Planet.get(j);
    P.run();
  }
}

void keyPressed() {
  if (key==' ') {
    //add new bullet in relation to the position and angle coordinates of the spaceship
    //so that it appears to be shooting out of it
    bullet b = new bullet(s.p.x, s.p.y, s.ang);
    //add the bullet to the bulletList, to keep track of it
    bulletList.add(b);
  }
  //creating interaction with the movements of the spaceship according to the boolean
  //of whether a key press is true or false
  if (keyCode==RIGHT)s.turningRight = true;
  if (keyCode==LEFT) s.turningLeft = true;
  if (keyCode==UP) s.movingForward = true;
  if (keyCode==DOWN) s.movingBackwards = true;
}

void keyReleased() {
  if (keyCode==RIGHT)s.turningRight = false;
  if (keyCode==LEFT) s.turningLeft = false;
  if (keyCode==UP) s.movingForward = false;
  if (keyCode==DOWN) s.movingBackwards = false;
}
class bullet {
 
  //bullet variables, identical to the spaceship class
  PVector p;//position
  float ang;//initial angle
  float radius = 10; //movement speed for bullet
 
 
  bullet(float x, float y, float angle) {
    p = new PVector(x, y);
    ang = angle;
  }
 
  void run() {
    move();
    display();
  }
 
  void move() {
    float moveX = radius * sin(ang);
    float moveY = -radius * cos(ang);
    
    //creating the movement in x and y axis,
    //by adding the bullets position to the float moveX and moveY
    p.x += moveX;
    p.y += moveY;
  }
 
  void display() {
    //draw bullet
    pushMatrix();
    //translation and rotation according to its class variables
    translate(p.x,p.y);
    rotate(ang);
    fill(255);
    rect(-2, -10, 2, 20);
    popMatrix();
  }
}
class Planets {
  int c;
  float theta;
  float diameter;
  float distance;
  float planetSpeed;
  //setting variables for planet class


Planets(float distance, float diameter) {
    this.distance = distance;
    this.diameter = diameter;
    theta = random(distance);//theta according to a random size in distance variable
    planetSpeed = random(0.01, 0.04);//random orbit speed of moons within boundaries
  }
  
  void run(){
  update();
  drawPlanet();
  }
 
  void update() {
    theta += planetSpeed;
  }
  
  void drawPlanet() {
    pushMatrix();
    c = color(random(100, 255));
    rotate(theta);//rotate according to initialised theta variable
    translate(distance, 0);//translate according to distance variable
    fill(c, 150, 0);
    noStroke();//no outline on moons
    sphere(diameter);
    
    popMatrix();
  }
}
class spaceShip {

  //spaceShip variables
  float ang; //initial rotation
  PVector p;//initial position
  float rotationSpeed = 0.08; // rotation speed
  float radius = 8; //movement speed
  int size = 25;//size of spaceship
  //moving variables using booleans
  boolean movingForward = false;
  boolean movingBackwards = false;
  boolean turningRight = false;
  boolean turningLeft = false;

  spaceShip(float x, float y, float ang) {
    p = new PVector(x, y);
  }

  void run() {
    checkMove();
    display();
  }

  void checkMove() {
    //check movement of spaceShip
    float moveX = radius * sin(ang);
    float moveY = -radius * cos(ang);

    if (movingForward) {
      //to move forward, adding the movement to x and y position
      p.x += moveX;
      p.y += moveY;
    }
    if (movingBackwards) {
      //to move backwards subtracting the movement from the x and y position
      p.x -= moveX;
      p.y -= moveY;
    }
    if (turningRight) {
      //to turn right, adding the rotationSpeed to the angle of which the spaceship sits
      ang += rotationSpeed;
    }
    if (turningLeft) {
      //to turn left, subtracting the rotationSpeed from the angle of which the spaceship sits
      ang -= rotationSpeed;
    }
    // creating spaceship boundaries so it wraps around the edges of the screen
    //if the ship(according to its size in relation to x and y position goes over the width or below 0 
    //it continues travelling from the opposite side of the screen, this is the same for the ypos according
    //to height and 0
    if (p.x<-size) {
      p.x =width+size;
    }
    if (p.x>width+size) {
      p.x =-size;
    }
    if (p.y<-size) {
      p.y = height+size;
    }
    if (p.y>height+size) {
      p.y = -size;
    }
  }

  void display() {
    //draw spaceship
    pushMatrix();
    //translation and rotation according to its class variables
    translate(p.x, p.y);
    rotate(ang);
    //fill green
    fill(255);
    noStroke();
    //triangle coordinates according to global variable size
    triangle(0, -size, -size/3, size/2, size/3, size/2);
    popMatrix();
  }
}
class star {
  int xPos, yPos, starSize;
  float flickerRate, light;
  boolean fade;
  star() {
    flickerRate = random(2, 5);
    starSize = int(random(2, 5));
    xPos = int(random(0, width));
    yPos = int(random(0, height));
    light = random(10, 245);
    fade = true;
  }
  void display() {
    if (light >= 240) {
      fade = false;
    }
    if (light <= 10) {
      flickerRate = random(0, 10);
      starSize = int(random(0, 5));
      fade = true;
      xPos = int(random(0, width));
      yPos = int(random(0, height));
    }
    if (fade == true) {
      light += flickerRate;
    }
    if (fade == false) {
      light -= flickerRate;
    }
    fill(light);
    ellipse(xPos, yPos, starSize, starSize);
  }
}
&#13;
&#13;
&#13;

我正在编写一个游戏,其中涉及到一系列行星的射击。我已经实现了拍摄和除碰撞检测之外的所有其他功能。我很困惑,我将如何使用行星的数组列表和子弹数组列表来做到这一点。这甚至可能吗? 有什么建议吗?

提前致谢。

0 个答案:

没有答案