Game Code
Game Code
import java.awt.*;
int x = 100;
int y = 50;
int red =0;
int green = 0;
int blue = 0;
PImage img = new PImage();
Alien al;//declare new Alien
Ship player;//declare new ship
Beam ray;
ArrayList<Beam> rays;
ArrayList<Alien> als;
void setup()
{
img = loadImage("spaceback.jpg");//background image
size(600,600);//screen size
background(img);//apply background
fill(red,green,blue);
al = new Alien(x,y);//establish new Alien
player = new Ship(width/2,height-50);//establish Ship
rays = new ArrayList<Beam>();
als = new ArrayList<Alien>();
als.add(new Alien(x,y));
}
void draw()
{
background(img);
for (Alien al: als)
{
al.collide();
al.draw();
al.cX = al.cX + al.xVelo;
al.cY = al.cY + al.yVelo;
}
player.draw();
for(Beam ray: rays)
{
ray.draw();
}
}
void mouseClicked()
{
//ray = new Beam(mouseX,mouseY);
rays.add(new Beam(mouseX + 20,player.getY()));
}
void mouseMoved()
{
player.setX(mouseX);
}
void mouseDragged()
{
player.setX(mouseX);
}
ALIEN//////////////////////////////////////////////////////
class Alien
{
public int
public int
public int
public int
cX;
cY;
xVelo = 3;
yVelo = 0;
Alien(int x, int y)
{
cX = x;
cY = y;
}
void draw()
{
noCursor();
fill(195,0,255);//alien body color
ellipse(cX+20,cY+30,50,10);//right arm
ellipse(cX-20,cY+30,50,10);//left arm
ellipse(cX,cY+74,20,65);//legs
line(cX,cY+70,cX,cY+107);//leg line
ellipse(cX-12,cY+105,25,10);//left foot
ellipse(cX+12,cY+105,25,10);//right foot
ellipse(cX,cY+40,20,60);//body
ellipse(cX,cY,35,40);//head
fill(255,255,255);//eye color
ellipse(cX,cY-7,12,15);//eye
fill(0,0,0);//pupil color
ellipse(cX,cY-7,5,5);//pupil
fill(0,255,0);//mouth color
ellipse(cX,cY+12,15,7);//mouth
fill(255,0,0);//tongue color
ellipse(cX,cY+14,7,5);//tongue
if(cY >=505)//teleport to top once bottom is reached
{
cX = 100;
cY = 50;
}
}
boolean collide()//bounces of walls
{
if(cX >= width-45)
{
xVelo = xVelo*-1;
cY=cY+40;
return true;
}
else if(cX <= width-555)
{
xVelo = xVelo*-1;
cY=cY+40;
return true;
}
else if(cY >= 575)
{
yVelo = yVelo*-1;
return true;
}
else if(cY <= 25)
{
yVelo = yVelo*-1;
return true;
}
else
return false;
}
boolean spawn(Alien al)//spawn once space opens up
{
if(cX >= 150)
return true;
else
return false;
}
int getX()//return x coord
{
return cX;
}
int getY()//return y coord
{
return cY;
}
BEAM//////////////////////////////////////////////////////
class Beam
{
public int bX;
public int bY;
public int bVelo = -5;
Beam(int xIn, int yIn)
{
bX = xIn;
bY = yIn;
}
void draw()//draw beam
{
bY += bVelo;
fill(5,242,5);
ellipse(bX,bY,10,30);
SHIP///////////////////////////////////////////////////////
class Ship
{
public int sX;
public int sY;
public int sVelo = 5;
}
}