Assistance in Alien Game
in
Programming Questions
•
1 year ago
Hey guys, I am in need of some help I have a game where my Alien isnt appearing and i cant figure out why. The main part of the problem is the last section of my code. Any help would be much appreciated
class Bullet
{
private PVector loc = new PVector();
private float angle;
private Cannon c;
private int age = 0;
PVector center;
PVector turret;
public Bullet(Cannon c, float x, float y)
{
this.c = c;
loc.x = x;
loc.y = y;
center = new PVector(c.loc.x,c.loc.y);
turret = new PVector(c.cX,c.cY);
}
void run()
{
noStroke();
fill(200);
ellipse(loc.x,loc.y,10,10);
PVector vel = PVector.sub(turret,center);
loc.add(new PVector(vel.x/15,vel.y/15));
age++;
if (age>100) bullets.remove(this);
}
}
class Cannon
{
public PVector loc = new PVector();
private float r = 90;
float cX;
float cY;
public Cannon(float x, float y)
{
loc.x = x;
loc.y = y;
}
void show()
{
smooth();
float angle = atan2(mouseY-loc.y,mouseX-loc.x);
cX = (r * cos(angle)) + loc.x;
cY = (r * sin(angle)) + loc.y;
fill(255,0,0);
noStroke();
ellipse(loc.x,loc.y,80,80);
noFill();
stroke(255,0,0);
strokeWeight(20);
line(loc.x,loc.y,cX,cY);
fill(0,200,0);
noStroke();
ellipse(loc.x,loc.y,70,70);
noFill();
stroke(0,200,0);
strokeWeight(10);
line(loc.x,loc.y,cX,cY);
}
}
public PFont font = createFont("Arial-MT",15);
ArrayList cannons = new ArrayList();
ArrayList bullets = new ArrayList();
ArrayList targets = new ArrayList();
public int points = 0;
int targetInterval = 150;
int counter = 0;
int bulletCounter = 0;
int hitCounter = 0;
float accuracy;
int seconds = 0;
int sc = 0;
int c = 0;
int speed = 3;
boolean playing = false;
import ddf.minim.* ;
Minim minim;
AudioPlayer mySound;
void setup()
{
minim = new Minim(this) ;
mySound = minim.loadFile("war2_x.wav");
cannons = new ArrayList();
bullets = new ArrayList();
targets = new ArrayList();
points = 0;
targetInterval = 150;
counter = 0;
bulletCounter = 0;
hitCounter = 0;
accuracy = 0;
seconds = 0;
sc = 0;
c = 0;
speed = 3;
size(500,500);
cursor(CROSS);
textFont(font);
background(250);
cannons.add(new Cannon(150,150));
targets.add(new Target(random(50,425),random(50,425),int(random(5,9))));
playing = true;
frameRate(50);
}
void draw()
{
if (playing)
play();
else
lose();
}
void mousePressed()
{
if (playing)
{
for (int k=0;k<cannons.size();k++)
{
bullets.add(new Bullet(getC(k),(getC(k)).cX,(getC(k)).cY));
bulletCounter++;
}
}
}
public Cannon getC(int index)
{
if (index<cannons.size())
return ((Cannon)cannons.get(index));
else
return null;
}
void play()
{
font = createFont("Arial-MT",15);
textFont(font);
background(0);
counter++;
sc++;
c++;
if (sc == 50)
{
seconds++;
sc=0;
}
if (c>=100)
{
targetInterval--;
c=0;
}
if (counter>=targetInterval)
{
targets.add(new Target(random(50,425),random(50,425),int(random(5,9))));
counter = 0;
}
for(int k=0;k<bullets.size();k++)
{
((Bullet)bullets.get(k)).run();
}
for(int j=0;j<targets.size();j++)
{
((Target)targets.get(j)).draw();
}
for(int i=0;i<cannons.size();i++)
{
((Cannon)cannons.get(i)).show();
}
if (keyPressed&&key==CODED)
{
if (keyCode == UP)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.y-=speed;
}
}
if (keyCode == DOWN)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.y+=speed;
}
}
if (keyCode == LEFT)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.x-=speed;
}
}
if (keyCode == RIGHT)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.x+=speed;
}
}
}
for (int k=0;k<cannons.size();k++)
{
if (((Cannon)cannons.get(k)).loc.x>width)
((Cannon)cannons.get(k)).loc.x=width;
else if (((Cannon)cannons.get(k)).loc.x<0)
((Cannon)cannons.get(k)).loc.x=0;
else if (((Cannon)cannons.get(k)).loc.y>height)
((Cannon)cannons.get(k)).loc.y=height;
else if (((Cannon)cannons.get(k)).loc.y<0)
((Cannon)cannons.get(k)).loc.y=0;
}
textAlign(LEFT);
fill(255);
text("Points: " + points,5,15);
if (bulletCounter > 0)
{
accuracy = int((hitCounter/bulletCounter)*100);
int h = bulletCounter - hitCounter;
int p = int(h/bulletCounter);
}
else if (bulletCounter == 0)
{
accuracy = 0;
}
//text("Accuracy: " + accuracy + "%", 5, 50);
text("Seconds: " + seconds, 5, 35);
if (targets.size()>5)
{
playing = false;
}
}
void lose()
{
int score = int(((points*seconds)*54.73)-((bulletCounter-hitCounter)*10));
background(0);
font = createFont("Arial-MT",50);
textFont(font);
textAlign(CENTER);
text("You Lost!!", width/2, height/2-50);
font = createFont("Arial-MT",25);
textFont(font);
text("Your score was: " + score, width/2, height/2-25);
text("Press 'r' to Play Again!" , width/2, height/2+10);
}
void keyPressed()
{
if (!playing)
{
if (key == 'r')
setup();
}
}
int BodyX;
int BodyY;
int BodyLength;
int BodyHeight;
int AntX;
int AntY;
int AntLength;
int AntHeight;
int LineX;
int LineY;
int LineLength;
int LineHeight;
int EyeX;
int EyeY;
int EyeLength;
int EyeHeight;
int EyeColX;
int EyeColY;
int EyeColLength;
int EyeColHeight;
int IrisX;
int IrisY;
int IrisLength;
int IrisHeight;
int Eye2X;
int Eye2Y;
int Eye2Length;
int Eye2Height;
int EyeCol2X;
int EyeCol2Y;
int EyeCol2Length;
int EyeCol2Height;
int Iris2X;
int Iris2Y;
int Iris2Length;
int Iris2Height;
int Eye3X;
int Eye3Y;
int Eye3Length;
int Eye3Height;
int EyeCol3X;
int EyeCol3Y;
int EyeCol3Length;
int EyeCol3Height;
int Iris3X;
int Iris3Y;
int Iris3Length;
int Iris3Height;
int MouthX;
int MouthY;
int MouthLength;
int MouthWidth;
class Target
{
float x;
float y;
int lives;
Target(float X, float Y, int LIVES)
{
x = X;
y = Y;
this.lives = LIVES;
}
void draw()
{
//body
ellipse(BodyX,BodyHeight - (BodyLength/2),BodyLength,BodyHeight);
fill(0);
//ellipse(250,40,60,10)
ellipse (AntX, AntY, AntLength, AntHeight); //top of antenna
AntX = BodyX;
AntY = (BodyLength / 10)*4;
AntLength = BodyLength /2;
AntHeight = (BodyLength / 10);
//line(250,100,250, 40);//antenna
line(LineX, LineY, LineLength, LineHeight);
LineX = BodyX;
LineY = BodyLength;
LineLength = BodyX;
LineHeight = (BodyLength / 10)*4;
//eye 1
fill(255);
//ellipse(250,100,50,60);
ellipse(EyeX,EyeY,EyeLength,EyeHeight);//white of eye
EyeX = BodyX;
EyeY = BodyLength;
EyeLength = BodyLength/2;
EyeHeight = BodyLength/2;
fill(119,240,239);
//ellipse(250,100,40,30);
ellipse(EyeColX,EyeColY,EyeColLength,EyeColHeight);//colour of eye
EyeColX = BodyX;
EyeColY = BodyLength;
EyeColLength = (BodyLength / 10)*4;
EyeColHeight = (BodyLength / 10)*3;
fill(0);
//ellipse(250,100,30,20);
ellipse(IrisX,IrisY,IrisLength,IrisHeight); //iris
IrisX = BodyX;
IrisY = BodyLength;
IrisLength = (BodyLength / 10)*3;
IrisHeight = (BodyLength / 10)*2;
//eye2
fill(255); //Fill for Eye(White)
//ellipse(320,250,50,60);
ellipse(Eye2X,Eye2Y,Eye2Length,Eye2Height);//white of eye
Eye2X = BodyX +(BodyLength - 25);
Eye2Y = BodyHeight - BodyLength;
Eye2Length = (BodyLength / 10)*5;
Eye2Height = (BodyLength / 10)*6;
fill(119,240,239);// fill for colour of eye
//ellipse(320,250,40,30);
ellipse(EyeCol2X,EyeCol2Y,EyeCol2Length,EyeCol2Height);//colour of eye
EyeCol2X =BodyX +(BodyLength - 25);
EyeCol2Y = BodyHeight - BodyLength;
EyeCol2Length = (BodyLength / 10)*4;
EyeCol2Height = (BodyLength / 10)*3;
fill(0);//Iris color
//ellipse(320,250,30,20);
ellipse(Iris2X,Iris2Y,Iris2Length,Iris2Height); //iris
Iris2X =BodyX +(BodyLength - 25);
Iris2Y = BodyHeight - BodyLength;
Iris2Length = (BodyLength / 10)*3;
Iris2Height = (BodyLength / 10)*2;
//eye3
fill(255);
//ellipse(180,250,50,60)
ellipse(Eye3X,Eye3Y,Eye3Length,Eye3Height);//white of eye
Eye3X = BodyX - 70;
Eye3Y = BodyHeight - BodyLength;
Eye3Length = (BodyLength / 10)*5;
Eye3Height = (BodyLength / 10)*6;
fill(119,240,239);//Fill of eye
//ellipse(180,260,40,30)
ellipse(EyeCol3X,EyeCol3Y,EyeCol3Length,EyeCol3Height);//colour of eye
EyeCol3X = BodyX - 70;
EyeCol3Y = BodyHeight - BodyLength;
EyeCol3Length = (BodyLength / 10)*4;
EyeCol3Height = (BodyLength / 10)*3;
fill(0);
//ellipse(180,260,30,20);
ellipse(Iris3X,Iris3Y,Iris3Length,Iris3Height); //iris
Iris3X = BodyX - 70;
Iris3Y = BodyHeight - BodyLength;
Iris3Length = (BodyLength / 10)*3;
Iris3Height = (BodyLength / 10)*2;
//mouth
fill(255,0,0);
//ellipse(250,300,100,150);
ellipse(MouthX,BodyHeight - (BodyLength/2),MouthLength,MouthWidth);
MouthX = BodyX;
MouthY = BodyY;
MouthLength = BodyLength;
MouthWidth = (BodyLength / 10)*15;
fill(0);
textAlign(CENTER);
text(lives,x+38,y+43);
for (int i=0;i<bullets.size();i++)
{
PVector l = new PVector(((Bullet)bullets.get(i)).loc.x,((Bullet)bullets.get(i)).loc.y);
if (l.x>=x&&l.x<=x+75&&l.y >=y&&l.y<=y+75)
{
lives--;
hitCounter++;
bullets.remove(((Bullet)bullets.get(i)));
if (lives == 0)
{
targets.remove(this);
points++;
}
}
}
}
}
class Bullet
{
private PVector loc = new PVector();
private float angle;
private Cannon c;
private int age = 0;
PVector center;
PVector turret;
public Bullet(Cannon c, float x, float y)
{
this.c = c;
loc.x = x;
loc.y = y;
center = new PVector(c.loc.x,c.loc.y);
turret = new PVector(c.cX,c.cY);
}
void run()
{
noStroke();
fill(200);
ellipse(loc.x,loc.y,10,10);
PVector vel = PVector.sub(turret,center);
loc.add(new PVector(vel.x/15,vel.y/15));
age++;
if (age>100) bullets.remove(this);
}
}
class Cannon
{
public PVector loc = new PVector();
private float r = 90;
float cX;
float cY;
public Cannon(float x, float y)
{
loc.x = x;
loc.y = y;
}
void show()
{
smooth();
float angle = atan2(mouseY-loc.y,mouseX-loc.x);
cX = (r * cos(angle)) + loc.x;
cY = (r * sin(angle)) + loc.y;
fill(255,0,0);
noStroke();
ellipse(loc.x,loc.y,80,80);
noFill();
stroke(255,0,0);
strokeWeight(20);
line(loc.x,loc.y,cX,cY);
fill(0,200,0);
noStroke();
ellipse(loc.x,loc.y,70,70);
noFill();
stroke(0,200,0);
strokeWeight(10);
line(loc.x,loc.y,cX,cY);
}
}
public PFont font = createFont("Arial-MT",15);
ArrayList cannons = new ArrayList();
ArrayList bullets = new ArrayList();
ArrayList targets = new ArrayList();
public int points = 0;
int targetInterval = 150;
int counter = 0;
int bulletCounter = 0;
int hitCounter = 0;
float accuracy;
int seconds = 0;
int sc = 0;
int c = 0;
int speed = 3;
boolean playing = false;
import ddf.minim.* ;
Minim minim;
AudioPlayer mySound;
void setup()
{
minim = new Minim(this) ;
mySound = minim.loadFile("war2_x.wav");
cannons = new ArrayList();
bullets = new ArrayList();
targets = new ArrayList();
points = 0;
targetInterval = 150;
counter = 0;
bulletCounter = 0;
hitCounter = 0;
accuracy = 0;
seconds = 0;
sc = 0;
c = 0;
speed = 3;
size(500,500);
cursor(CROSS);
textFont(font);
background(250);
cannons.add(new Cannon(150,150));
targets.add(new Target(random(50,425),random(50,425),int(random(5,9))));
playing = true;
frameRate(50);
}
void draw()
{
if (playing)
play();
else
lose();
}
void mousePressed()
{
if (playing)
{
for (int k=0;k<cannons.size();k++)
{
bullets.add(new Bullet(getC(k),(getC(k)).cX,(getC(k)).cY));
bulletCounter++;
}
}
}
public Cannon getC(int index)
{
if (index<cannons.size())
return ((Cannon)cannons.get(index));
else
return null;
}
void play()
{
font = createFont("Arial-MT",15);
textFont(font);
background(0);
counter++;
sc++;
c++;
if (sc == 50)
{
seconds++;
sc=0;
}
if (c>=100)
{
targetInterval--;
c=0;
}
if (counter>=targetInterval)
{
targets.add(new Target(random(50,425),random(50,425),int(random(5,9))));
counter = 0;
}
for(int k=0;k<bullets.size();k++)
{
((Bullet)bullets.get(k)).run();
}
for(int j=0;j<targets.size();j++)
{
((Target)targets.get(j)).draw();
}
for(int i=0;i<cannons.size();i++)
{
((Cannon)cannons.get(i)).show();
}
if (keyPressed&&key==CODED)
{
if (keyCode == UP)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.y-=speed;
}
}
if (keyCode == DOWN)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.y+=speed;
}
}
if (keyCode == LEFT)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.x-=speed;
}
}
if (keyCode == RIGHT)
{
for (int k=0;k<cannons.size();k++)
{
((Cannon)cannons.get(k)).loc.x+=speed;
}
}
}
for (int k=0;k<cannons.size();k++)
{
if (((Cannon)cannons.get(k)).loc.x>width)
((Cannon)cannons.get(k)).loc.x=width;
else if (((Cannon)cannons.get(k)).loc.x<0)
((Cannon)cannons.get(k)).loc.x=0;
else if (((Cannon)cannons.get(k)).loc.y>height)
((Cannon)cannons.get(k)).loc.y=height;
else if (((Cannon)cannons.get(k)).loc.y<0)
((Cannon)cannons.get(k)).loc.y=0;
}
textAlign(LEFT);
fill(255);
text("Points: " + points,5,15);
if (bulletCounter > 0)
{
accuracy = int((hitCounter/bulletCounter)*100);
int h = bulletCounter - hitCounter;
int p = int(h/bulletCounter);
}
else if (bulletCounter == 0)
{
accuracy = 0;
}
//text("Accuracy: " + accuracy + "%", 5, 50);
text("Seconds: " + seconds, 5, 35);
if (targets.size()>5)
{
playing = false;
}
}
void lose()
{
int score = int(((points*seconds)*54.73)-((bulletCounter-hitCounter)*10));
background(0);
font = createFont("Arial-MT",50);
textFont(font);
textAlign(CENTER);
text("You Lost!!", width/2, height/2-50);
font = createFont("Arial-MT",25);
textFont(font);
text("Your score was: " + score, width/2, height/2-25);
text("Press 'r' to Play Again!" , width/2, height/2+10);
}
void keyPressed()
{
if (!playing)
{
if (key == 'r')
setup();
}
}
int BodyX;
int BodyY;
int BodyLength;
int BodyHeight;
int AntX;
int AntY;
int AntLength;
int AntHeight;
int LineX;
int LineY;
int LineLength;
int LineHeight;
int EyeX;
int EyeY;
int EyeLength;
int EyeHeight;
int EyeColX;
int EyeColY;
int EyeColLength;
int EyeColHeight;
int IrisX;
int IrisY;
int IrisLength;
int IrisHeight;
int Eye2X;
int Eye2Y;
int Eye2Length;
int Eye2Height;
int EyeCol2X;
int EyeCol2Y;
int EyeCol2Length;
int EyeCol2Height;
int Iris2X;
int Iris2Y;
int Iris2Length;
int Iris2Height;
int Eye3X;
int Eye3Y;
int Eye3Length;
int Eye3Height;
int EyeCol3X;
int EyeCol3Y;
int EyeCol3Length;
int EyeCol3Height;
int Iris3X;
int Iris3Y;
int Iris3Length;
int Iris3Height;
int MouthX;
int MouthY;
int MouthLength;
int MouthWidth;
class Target
{
float x;
float y;
int lives;
Target(float X, float Y, int LIVES)
{
x = X;
y = Y;
this.lives = LIVES;
}
void draw()
{
//body
ellipse(BodyX,BodyHeight - (BodyLength/2),BodyLength,BodyHeight);
fill(0);
//ellipse(250,40,60,10)
ellipse (AntX, AntY, AntLength, AntHeight); //top of antenna
AntX = BodyX;
AntY = (BodyLength / 10)*4;
AntLength = BodyLength /2;
AntHeight = (BodyLength / 10);
//line(250,100,250, 40);//antenna
line(LineX, LineY, LineLength, LineHeight);
LineX = BodyX;
LineY = BodyLength;
LineLength = BodyX;
LineHeight = (BodyLength / 10)*4;
//eye 1
fill(255);
//ellipse(250,100,50,60);
ellipse(EyeX,EyeY,EyeLength,EyeHeight);//white of eye
EyeX = BodyX;
EyeY = BodyLength;
EyeLength = BodyLength/2;
EyeHeight = BodyLength/2;
fill(119,240,239);
//ellipse(250,100,40,30);
ellipse(EyeColX,EyeColY,EyeColLength,EyeColHeight);//colour of eye
EyeColX = BodyX;
EyeColY = BodyLength;
EyeColLength = (BodyLength / 10)*4;
EyeColHeight = (BodyLength / 10)*3;
fill(0);
//ellipse(250,100,30,20);
ellipse(IrisX,IrisY,IrisLength,IrisHeight); //iris
IrisX = BodyX;
IrisY = BodyLength;
IrisLength = (BodyLength / 10)*3;
IrisHeight = (BodyLength / 10)*2;
//eye2
fill(255); //Fill for Eye(White)
//ellipse(320,250,50,60);
ellipse(Eye2X,Eye2Y,Eye2Length,Eye2Height);//white of eye
Eye2X = BodyX +(BodyLength - 25);
Eye2Y = BodyHeight - BodyLength;
Eye2Length = (BodyLength / 10)*5;
Eye2Height = (BodyLength / 10)*6;
fill(119,240,239);// fill for colour of eye
//ellipse(320,250,40,30);
ellipse(EyeCol2X,EyeCol2Y,EyeCol2Length,EyeCol2Height);//colour of eye
EyeCol2X =BodyX +(BodyLength - 25);
EyeCol2Y = BodyHeight - BodyLength;
EyeCol2Length = (BodyLength / 10)*4;
EyeCol2Height = (BodyLength / 10)*3;
fill(0);//Iris color
//ellipse(320,250,30,20);
ellipse(Iris2X,Iris2Y,Iris2Length,Iris2Height); //iris
Iris2X =BodyX +(BodyLength - 25);
Iris2Y = BodyHeight - BodyLength;
Iris2Length = (BodyLength / 10)*3;
Iris2Height = (BodyLength / 10)*2;
//eye3
fill(255);
//ellipse(180,250,50,60)
ellipse(Eye3X,Eye3Y,Eye3Length,Eye3Height);//white of eye
Eye3X = BodyX - 70;
Eye3Y = BodyHeight - BodyLength;
Eye3Length = (BodyLength / 10)*5;
Eye3Height = (BodyLength / 10)*6;
fill(119,240,239);//Fill of eye
//ellipse(180,260,40,30)
ellipse(EyeCol3X,EyeCol3Y,EyeCol3Length,EyeCol3Height);//colour of eye
EyeCol3X = BodyX - 70;
EyeCol3Y = BodyHeight - BodyLength;
EyeCol3Length = (BodyLength / 10)*4;
EyeCol3Height = (BodyLength / 10)*3;
fill(0);
//ellipse(180,260,30,20);
ellipse(Iris3X,Iris3Y,Iris3Length,Iris3Height); //iris
Iris3X = BodyX - 70;
Iris3Y = BodyHeight - BodyLength;
Iris3Length = (BodyLength / 10)*3;
Iris3Height = (BodyLength / 10)*2;
//mouth
fill(255,0,0);
//ellipse(250,300,100,150);
ellipse(MouthX,BodyHeight - (BodyLength/2),MouthLength,MouthWidth);
MouthX = BodyX;
MouthY = BodyY;
MouthLength = BodyLength;
MouthWidth = (BodyLength / 10)*15;
fill(0);
textAlign(CENTER);
text(lives,x+38,y+43);
for (int i=0;i<bullets.size();i++)
{
PVector l = new PVector(((Bullet)bullets.get(i)).loc.x,((Bullet)bullets.get(i)).loc.y);
if (l.x>=x&&l.x<=x+75&&l.y >=y&&l.y<=y+75)
{
lives--;
hitCounter++;
bullets.remove(((Bullet)bullets.get(i)));
if (lives == 0)
{
targets.remove(this);
points++;
}
}
}
}
}
1