Hi! I’m trying to build a retro top down arena shooter game and I’m struggling to get shooting to work. What I want is to have an ArrayList of bullets that can be shot using a keypressed fucntion and that are shot in the direction of the player is moving or the direction that the player last moved. I sort of have it working now, but I can’t shoot the bullets diagonally and I would really like to be able to do that. If anyone can maybe help me change my code up a bit to solve this problem or if anyone has ant tips to make my code better in general please help
+//classes
player myPlayer;
Bullet myBullets;
void setup() {
size(1600, 1000);
myPlayer = new player(width/2, height/2, 4, 40);
myBullets = new Bullet(0, 0, 0, 0, 10);
}
void draw() {
background(0);
myPlayer.update();
myBullets.update(myBullets.x, myBullets.y, myBullets.speedX,
myBullets.speedY, myBullets.diameter);
myPlayer.draw();
myBullets.draw();
}
void keyPressed() {
myPlayer.keyp();
myBullets.keyp();
}
void keyReleased() {
myPlayer.keyr();
myBullets.keyr();
}
+class player {
float x, y, speed, size;
float xVelocity, yVelocity;
boolean keys = new boolean[4];
//constructor
player(int x1, int y1, int speed1, int size1) {
x = x1;
y = y1;
speed = speed1;
size = size1;
}
void update() {
xVelocity = 0;
// gives the bullet a speed and direction when keys are pressed
if (keys[0] == true) {
yVelocity = -speed;
}
if (keys[1] == true) {
xVelocity = -speed;
}
if (keys[2] == true) {
yVelocity = speed;
}
if (keys[3] == true) {
xVelocity = speed;
}
x = x + xVelocity;
y = y + yVelocity;
collisionDetec();
}
void draw() {
circle(x, y, size);
}
void keyp() {
//allows for character movement when keys are pressed
if (key == ‘w’ || key == ‘W’) {
keys[0] = true;
}
if (key == ‘a’ || key == ‘A’) {
keys[1] = true;
}
if (key == ‘s’ || key == ‘S’) {
keys[2] = true;
}
if (key == ‘d’ || key == ‘D’) {
keys[3] = true;
}
}
void keyr() {
//ensures character doesn’t move when keys are released
if (key == ‘w’ || key == ‘W’) {
keys[0] = false;
}
if (key == ‘a’ || key == ‘A’) {
keys[1] = false;
}
if (key == ‘s’ || key == ‘S’) {
keys[2] = false;
}
if (key == ‘d’ || key == ‘D’) {
keys[3] = false;
}
}
void collisionDetec() {
//speed and direction in which x moves
//collision detection x axis
if (x > width - size/2) {
x = width - size/2;
}
if ( x < size/2 ) {
x = size/2;
}
//speed and direction in which y moves
//collision detection y axis
if ( y >= height - size/2) {
y = height - size/2;
}
if ( y <= size/2) {
y = size/2;
}
}
}
ArrayList bullets = new ArrayList();
class Bullet {
float x, y, speedX, speedY, diameter, startX, startY;
boolean keys = new boolean[4];
//constructor
Bullet(float x1, float y1, float speedX1, float speedY1, float
diameter1) {
x = x1;
y = y1;
speedX = speedX1;
speedY = speedY1;
diameter = diameter1;
}
void draw() {
// – because bullets are removed when it leaves screen
for (int i = bullets.size() - 1; i >= 0; i–) {
Bullet b = bullets.get(i);
b.update(b.x, b.y, b.speedX, b.speedY, diameter);
noStroke();
circle(b.x, b.y, diameter);
//when bullets are beyond screen they are removed
if ( ( b.x >= width || b.x < 0 ) || ( b.y >= height || b.y < 0 ) ) {
bullets.remove(i);
}
}
}
void update( float x2, float y2, float speedX2, float speedY2, float
diameter2 ) {
x = x2 + speedX2;
y = y2 + speedY2;
diameter = diameter2;
}
void keyp() {
if (key == ’ ') {
x = myPlayer.x;
y = myPlayer.y;
if ( startY == 0 ) {
speedX = startX;
speedY = 0;
} else {
speedY = startY;
speedX = 0;
}
//adds new bullet to ArrayList when spacebar is pressed
bullets.add(new Bullet(x, y, speedX, speedY, diameter));
}
if (key == 'w' || key == 'W') {
keys[0] = true;
startY = myPlayer.speed * - 1.5;
startX = 0;
}
//gives bullets direction based on last direction of player
if (key == 'a' || key == 'A') {
keys[1] = true;
startX = myPlayer.speed * - 1.5;
startY = 0;
}
if (key == 's' || key == 'S') {
keys[2] = true;
startY = myPlayer.speed * 1.5;
startX = 0;
}
if (key == 'd' || key == 'D') {
keys[3] = true;
startX = myPlayer.speed * 1.5;
startY = 0;
}
}
void keyr() {
//gives bullet its own speed and direction after it is fired
if (key == ‘w’ || key == ‘W’) {
keys[0] = false;
startY = myPlayer.speed * - 1.5;
}
if (key == ‘a’ || key == ‘A’) {
keys[1] = false;
startX = myPlayer.speed * - 1.5;
}
if (key == ‘s’ || key == ‘S’) {
keys[2] = false;
startY = myPlayer.speed * 1.5;
}
if (key == ‘d’ || key == ‘D’) {
keys[3] = false;
startX = myPlayer.speed * 1.5;
}
}
}