Hello. Im currently working on my first processing project, and im trying to create a very simple game. In the game you move an airplane with your mouse and have to avoid projectiles coming towards you.
The airplane is made of triangles, and its shape is very pointy. The projectiles have a bullet shape, but is made of the vertex class. Will this be a problem when i have to program the collision? And how would i go about coding collision for this game? Any input here would be much appreciated
//Declaring object arrays
Cloud [] clouds;
Bullets [] bullets;
//Declaring the number of skies and projectiles in the scene
int numberOfSkies = 5;
int numberOfProjectiles = 18;
boolean onAndOff = false;
PShape object;
PFont font,font2,font3,font4;
int difficulty = 1;
int counting = 0;
//Varaibles for the airplane - player class()
float vx = 0;
float vy = 0;
float vr = 0;
//Importing sound
import processing.sound.*;
SoundFile file;
Player player = new Player();
void setup () {
frameRate(60);
size(600,600);
//Loading sound file
file = new SoundFile(this, "sample.wav");
file.play();
//Creating an object array of n number of clouds
clouds = new Cloud[numberOfSkies];
for(int i = 0; i < numberOfSkies; i++) {
clouds [i] = new Cloud();
}
//Creating an object array of n number of bullets
bullets = new Bullets[numberOfProjectiles];
for(int i = 0; i < numberOfProjectiles; i++) {
bullets [i] = new Bullets(object);
}
//Creating fonts used for startscreen and score
font = createFont("Arial",32,true); font2 = createFont("Arial",60,true); font3 = createFont("Arial",26,true); font4 = createFont("Arial",36,true);
}
void draw () {
//Declaring background in draw, so animation is possible
background(122,200,255);
//If the game is not turned on, then show the start screen and options
if(onAndOff == false) {
textFont(font2);
textAlign(CENTER);
text("Paper Plane Pilot", width/2, 125);
textFont(font);
textAlign(CENTER);
text("Click to start game!", width/2, height/2+25);
textFont(font3);
textAlign(CENTER);
text("Arrow Up ↑ To Increase Difficulty", width/2, 515);
textFont(font4);
textAlign(CENTER);
text("Current Difficulty: " + difficulty, width/2, 565);
//Creating the airplane for the start screen
translate(-60,-268);
strokeWeight(1.48);
triangle(width/2, height/2+20, width/2+100, height/2, width/2+20, height/2+20);
triangle(width/2, height/2, width/2+100, height/2, width/2+20, height/2+20);
//If mouse is pressed, turn on the game by chaning the boolean to 'TRUE'.
//Multiply and divide the projectile speed with the chosen difficulty
keyPressed();
if(mousePressed == true) {
onAndOff = true;
for(int i = 0; i < numberOfProjectiles; i++) {
bullets[i].speed = (bullets[i].speed * difficulty) / 2.15 ;
}
}
//If the boolean onAndOff is not false then run this section of draw
} else {
//Mouse X movement
float kx = mouseX - vx;
if (abs(kx) > 1) {
vx += kx * player.movementSpeed;
}
//Mouse Y movement
float ky = mouseY - vy;
if (abs(ky) > 1) {
vy += ky * player.movementSpeed;
}
//With a for-loop we spawn n times projectiles and run each individual method
for(int i = 0; i < numberOfProjectiles; i++) {
bullets[i].drawBullet();
bullets[i].showBullet();
bullets[i].moveBullets();
bullets[i].refreshBullets();
}
//With a for loop we spawn the desired number of skies and gives them a small speed difference.
//The movement of the skies is activated in move() and the refreshment of new skies is done in the method refresh()
for(int i = 0; i < numberOfSkies; i++) {
clouds[i].speed = random(0.95,1.35);
clouds[i].drawSky();
clouds[i].onOff = true;
clouds[i].move();
clouds[i].refresh();
}
//Spawning the player and translating the two parameters from here on
player.spawnPlayer(vx,vy);
player.over();
//Text showing how many missiles the player have passed
textFont(font4);
textAlign(CENTER);
text("Missiles avoided: " + counting, width/2, 565);
//Detection
for(int i = 0; i < numberOfProjectiles; i++) {
if(abs(vx-bullets[i].xPos) <= 32 && abs(vy-bullets[i].yPos) < 10) {
//onAndOff = false;
}
}
}
}
//The function used to change the difficulty of the game by pressing 'UP'
void keyReleased() {
if (key == CODED) {
if (keyCode == UP) {
difficulty = difficulty + 1;
} else if (keyCode == DOWN) {
difficulty = difficulty - 1;
}
}
}
Bullet.class
class Bullets {
PShape inputObject;
float xPos = random(300,950);
float yPos = random(-300,300);
int counter = 0;
float speed = 10;
//Auto constructor
Bullets () {}
//Constructor
Bullets (PShape _inputObject) {
this.inputObject = _inputObject;
}
void drawBullet () {
//Drawing the bullet with vertexes. A form of free drawing ;-)
inputObject = createShape();
inputObject.beginShape();
inputObject.fill(255,255,255);
inputObject.noStroke();
inputObject.vertex(300, 300);
inputObject.vertex(275, 300);
inputObject.vertex(273, 298);
inputObject.vertex(268, 295);
inputObject.vertex(270,293);
inputObject.vertex(273,290);
inputObject.vertex(300,290);
inputObject.endShape(CLOSE);
}
void showBullet () {
shape(inputObject,xPos,yPos);
}
void moveBullets () {
//Give the projectiles an animation
xPos -= speed;
}
void refreshBullets () {
//If projectiles reach end of the screen than in this case (-327) because of translate, then respawn at new random position
if(xPos <= -327) {
xPos = random(300,950);
counting = counting + 1;
}
}
}
Cloud.class
class Cloud {
float xPos = random(175,575);
float yPos = random(25,575);
float w = 25;
float h = 25;
boolean onOff = false;
float speed;
//Auto constructor
Cloud () {}
//Constructor
Cloud (float _speed) {
this.speed = _speed;
}
//Drawing the sky with a lot of diffrent ellipses
void drawSky () {
noStroke();
fill(255);
ellipse(xPos,yPos,h,w);
ellipse(xPos+12,yPos-5,h,w);
ellipse(xPos-12,yPos-4,h,w);
ellipse(xPos-12,yPos-20,h,w);
ellipse(xPos,yPos-20,h,w);
ellipse(xPos+6,yPos-23,h,w);
ellipse(xPos-22,yPos-20,h,w);
ellipse(xPos-26,yPos-5,h,w);
}
//Making the skies move if the game is turned on
void move () {
if(onAndOff == true) {
xPos -= speed;
}
}
//Refreshing the skies, at a new random position to the right of the right edge.
void refresh () {
if(xPos < -25) {
xPos = random(650,700);
yPos = random(25,575);
}
}
}
Player.class
class Player {
public float xPos = 0;
public float yPos = 0;
float movementSpeed = 0.025;
float playerLength = 100;
float speed = 0;
//Auto constructor
Player () {}
//Drawing the paper plane with the pushMatrix & popMatrix with translate(). The plane is made of triangles. We transform the coordinate system.
void spawnPlayer (float x, float y) {
pushMatrix();
stroke(1);
strokeWeight(1.48);
translate(x, y);
triangle(0, 20, playerLength, 0, 20, 20);
triangle(0, 0, playerLength, 0, 20, 20);
popMatrix();
}
void over() {
if (key == ' ') {
background(0);
textFont(font2);
textAlign(CENTER);
text("Game Over!", width/2, 125);
onAndOff = false;
background(0);
}
}
}