Greetings, Proceessing community.
I’m working on a Spacewar iteration assignment for my university which is due in 5 days by now. I ran into a problem where I’m attempting to declare a scoring system (in which for each bullet hit, it adds one score, and it must reach the targeted score, such as 20; if the first ship hits the enemy ship 20 times, the game ends) and a reaction to the ships getting hit by the opposing ship. However, I’m having trouble trying to fix these lines of code. Does anyone know how to solve the problem? I have my code displayed as of now:
Main File
int x;
int y;
int i = 0;
float angle = 0;
Ship myShip;
Ship2 myShip2;
Starfield myStarfield = new Starfield(50);
ArrayList<Missile> missiles = new ArrayList<Missile>();
void setup() {
size(500,500);
x = width/2;
y = height/2;
myStarfield.StarfieldSetup(width, height);
myShip = new Ship(x, y, 0);
myShip2 = new Ship2(x, y, 0);
}
void draw() {
background(10);
myShip.move();
myShip.show();
myShip2.move();
myShip2.show();
myShip.getScore(20);
myShip2.getScore(20);
updateMissile();
stroke(255);
strokeWeight(5);
i = 0;
while(i < 10)
{
myStarfield.StarfieldDraw(i, width);
i++;
}
}
void updateMissile(){
for(int i = missiles.size()-1; i>= 0 ; i--){
Missile missile = missiles.get(i);
if(missile.offscreen()) {
missiles.remove(missile);
}
else {
missile.move();
missile.show();
}
}
}
void keyPressed()
{
if(keyCode == LEFT)
{
myShip.changeDirX(-1);
}
else if(keyCode == RIGHT)
{
myShip.changeDirX(1);
}
else if(keyCode == UP)
{
myShip.changeDirY(-1);
}
else if(keyCode == DOWN)
{
myShip.changeDirY(1);
}
else if(keyCode == 76) //L Key
{
myShip.turnAngle(-5);
}
else if(keyCode == 82) // R Key
{
myShip.turnAngle(5);
}
else if(keyCode == 32) // Spacebar
{
PVector mp = new PVector(0, 0);
mp = myShip.currentPos();
PVector ms = new PVector(0, 0) ;
ms = myShip.currentSpeed();
PVector ma = new PVector(0, 0) ;
ma = myShip.currentSpeed();
ma.div(2);
missiles.add(new Missile(mp, ms, ma, 8));
}
if(keyCode == 65) // A Key
{
myShip2.changeDirX(-1);
}
else if(keyCode == 68) // D Key
{
myShip2.changeDirX(1);
}
else if(keyCode == 87) // W Key
{
myShip2.changeDirY(-1);
}
else if(keyCode == 83) // S Key
{
myShip2.changeDirY(1);
}
else if(keyCode == 81) //Q Key
{
myShip2.turnAngle(-5);
}
else if(keyCode == 69) // E Key
{
myShip2.turnAngle(5);
}
else if(keyCode == 9) // Tab
{
PVector mp = new PVector(0, 0);
mp = myShip2.currentPos();
PVector ms = new PVector(0, 0) ;
ms = myShip2.currentSpeed();
PVector ma = new PVector(0, 0) ;
ma = myShip2.currentSpeed();
ma.div(2);
missiles.add(new Missile(mp, ms, ma, 8));
}
}
Missile Class
class Missile{
PVector pos = new PVector(0, 0);
PVector speed = new PVector(0, 0);
PVector accel = new PVector(0, 0);
int prop;
int radio;
// Missile (PVector P_pos, PVector P_speed, int P_accel, int r) {
Missile (PVector P, PVector s, PVector a, int r) {
pos = P;
speed = s;
// speed.mult(5);
accel = a;
radio = r;
}
void move() {
speed.add(accel);
pos.add(speed);
}
void show() {
noStroke();
ellipse(pos.x, pos.y, radio, radio);
fill(106,67,15);
}
boolean offscreen(){
boolean b = false;
if (pos.x >= width || pos.y >= height || pos.x <= 0 || pos.y <= 0) {b = true; }
return b;
}
}
Ship Class
class Ship {
PVector pos = new PVector(0,0);
PVector speed = new PVector(1, 0);
int angle = 0;
int accel;
int prop = (1);
int score = 20;
int relocate;
int missile;
Ship(int x, int y, int a){
pos.x = x;
pos.y = y;
angle = a % 180;
pos.setMag(width/2);
}
void relocate(){
pos = PVector.random2D();
pos.setMag(width/2);
}
void show(){
stroke(0);
pushMatrix();
translate(pos.x, pos.y);
rotate(radians(angle));
fill(237,255,0);
rectMode(CENTER);
triangle(-10,15,10,15,0,-15);
rect(0,0,35,10);
rect(20,10, 10,10);
rect(-20,10, 10,10);
popMatrix();
}
void getScore(){
}
void addScore() {
int addScore = 20;
updateMissile();
if (missile.prop == 1) {
if (myShip2.collisionBullet(missile.pos)) { //This indicates the Ship reacting to getting hit by the opposing ship.
myShip.addScore();
score ++;
myShip2.relocate();
missile.remove(missile);
// If Ship2 gets hit by Ship, a score is added for Ship.
}
}
}
void move(){
speed.limit(4);
pos.add(speed);
pos.x = pos.x % width;
pos.y = pos.y % height;
}
void changeDirX(int v){
speed.x = speed.x + v;
}
void changeDirY(int v){
speed.y = speed.y + v;
}
PVector currentPos () {
PVector cp = new PVector(pos.x, pos.y);
return cp;
}
PVector currentSpeed () {
PVector cs = new PVector(speed.x, speed.y);
return cs;
}
void turnAngle(int a){
angle = angle + (a % 180) ;
}
boolean collisionBullet(PVector P_pos){
float dist = PVector.dist(P_pos, pos);
return(dist < 15);
}
}
//void printScore(){
// for (int = 0; i< score; i+)
Ship2 Class
class Ship2 {
PVector pos = new PVector(0,0);
PVector speed = new PVector(1, 0);
int angle = 0;
int accel;
int prop = (2);
int score = 20;
PVector collisionMissile;
PVector relocate;
PVector missile;
Ship2(int x, int y, int a){
pos.x = x;
pos.y = y;
angle = a % 180;
pos.setMag(width/2);
}
void show(){
stroke(0);
pushMatrix();
translate(pos.x, pos.y);
rotate(radians(angle));
fill(137,255,0);
rectMode(CENTER);
triangle(-10,15,10,15,0,-15);
rect(0,0,35,10);
rect(20,10, 10,10);
rect(-20,10, 10,10);
popMatrix();
}
void addScore() {
int addScore = 20;
updateMissile();
if (missile.prop == 2)
if (myShip.collisionBullet(missile.pos));
myShip2.addScore();
score ++;
myShip.relocate();
missile.remove(missile);
// If Ship gets hit by Ship2, a score is added for Ship2.
}
void move(){
speed.limit(4);
pos.add(speed);
pos.x = pos.x % width;
pos.y = pos.y % height;
}
void changeDirX(int v){
speed.x = speed.x + v;
}
void changeDirY(int v){
speed.y = speed.y + v;
}
PVector currentPos () {
PVector cp = new PVector(pos.x, pos.y);
return cp;
}
PVector currentSpeed () {
PVector cs = new PVector(speed.x, speed.y);
return cs;
}
void turnAngle(int a){
angle = angle + (a % 180) ;
}
boolean collisionBullet(PVector P_pos){
float dist = PVector.dist(P_pos, pos);
return(dist < 15);
}
}
void collisionMissile() {
}
I tried my best on solving the issue by looking at previous examples, but they didn’t help. I even tried to declare some of the errors displayed on the code. I am currently declaring collisionMissile() and getScore() in my code.The errors on my current attempt at making a scoreboard will delay the rest of the goals I have to complete until they are fixed somehow. If any knows how to solve this, let me know. I will appreciate any form of help that can fit in with my project accordingly.
-Christian