Pvector bullets

edited November 2016 in Questions about Code

I got this little code working to shoot bullet from an entity which can move around a circle and shoot a bullet and then the bullet maintains its angle

I know how to use the basic Pvector but I can't figure out how to use them in this case, so I had to code it in such a way that you can only shoot a new one once the previous one is of screen so I was hoping someone could show me how to use the Pvector in this case

int basesize = 250;
int growth;
int direction = 0;
int speedfactor = 1;
int space;
int line1; 
int line2;
float bullet1;
int bulle1 = 220;
int open1;
int millis1;
int bullet;
float t = PI+PI/2;
float r = 165;

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

void keyPressed() {
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = -1;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 1;
  if (key == ' ') space = 1;
}

void keyReleased() {
  if (key == ' ') space = 0;
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = 0;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 0;
}

void draw() {

  background(255);

  stroke(0);
  fill(255);
  ellipseMode(CENTER);

  if (growth == 1) basesize += 2 * speedfactor;
  if (growth == 0) basesize -= 2 * speedfactor;

  if (basesize > 275) growth = 0;
  if (basesize < 225) growth = 1;

  ellipse(width/2, height/2, basesize, basesize);

  t += direction * speedfactor * 0.03;

  int x = (int)(width/2+r*cos(t));
  int y = (int)(height/2+r*sin(t));

  pushMatrix();
  translate(x, y);
  rotate(t-PI/2);
  stroke(0);
  fill(255);
  triangle(-30, -10, 0, +50, +30, -10);
  popMatrix();



  //bullet 1
  if (space == 1 && open1 == 0) {
    bullet1 = t;
    millis1 = millis();
    open1 = 1;
    space = 0;
  }
  if (open1 == 1) {
    bulle1 += 20 * speedfactor;
    line(width/2+bulle1*cos(bullet1), height/2+bulle1*sin(bullet1), width/2+(bulle1+10)*cos(bullet1), height/2+(bulle1+10)*sin(bullet1));
  }
  if (width/2+(bulle1+10)*cos(bullet1) < -20 || width/2+(bulle1+10)*cos(bullet1) > width + 20 || height/2+(bulle1+10)*sin(bullet1) < 20 || height/2+(bulle1+10)*sin(bullet1) > height) {
    bulle1 = 220;
    millis1 = 0;
    bullet = 1;
    open1 = 0;
  }
}
Tagged:

Answers

Sign In or Register to comment.