How do I make my 'spaceship' shoot projectiles?

edited November 2016 in Questions about Code

I want to make a game about space battles and I need your help. Basically, all I need is a part of code, that makes my object shoot projectiles that fly for ~500 pixels when pressing spacebar. Here's the code.

PVector location = new PVector (400,400);
PVector velocity = new PVector (0,0);
PVector acceleration = new PVector (0,0);
PVector mouse = new PVector (0,0);
float accelerationMax = 0.5;
int speedMax = 5;
int a = 40;
int b = 80;

void setup() {
size (800,800);
noCursor();
}
void draw() {
background (40);
fill(255,0,0);
ellipse(mouseX,mouseY,10,10);
line(mouseX,mouseY-15,mouseX,mouseY+15);
line(mouseX+15,mouseY,mouseX-15,mouseY);
mouse.set(mouseX, mouseY);
acceleration = PVector.sub(mouse,location);
acceleration.limit(accelerationMax);
location.add(velocity);
velocity.limit(speedMax);
velocity.add(acceleration);
if (mousePressed) {
accelerationMax = 0.01;
speedMax = 0;
}
else {
  accelerationMax = 0.5;
  speedMax = 5;
} 
pushMatrix();
rectMode(CENTER);
translate (location.x, location.y);
rotate (PI/2 + velocity.heading());
fill(0,0,0);
beginShape();
vertex(30,10);
vertex(45,35);
vertex(35,30);
vertex(38,40);
vertex(45,45);
vertex(15,45);
vertex(22,40);
vertex(25,30);
vertex(15,35);
endShape();
popMatrix();
}

Answers

Sign In or Register to comment.