0% found this document useful (0 votes)
42 views

Game Code

This document contains the code for a simple alien shooting game. It defines classes for key game elements - Alien, Beam, and Ship. The setup function initializes the background image, screen size, and creates initial instances of the Alien, Ship, and collections to hold Beams and Aliens. The draw function animates the game by redrawing the background, moving and drawing Aliens and Beams each frame. Mouse clicks create new Beam projectiles from the Ship. The classes define properties and behaviors for each type - Aliens bounce off walls and teleport, Beams move and check for collisions with Aliens, and the Ship moves with mouse position.

Uploaded by

api-294576305
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Game Code

This document contains the code for a simple alien shooting game. It defines classes for key game elements - Alien, Beam, and Ship. The setup function initializes the background image, screen size, and creates initial instances of the Alien, Ship, and collections to hold Beams and Aliens. The draw function animates the game by redrawing the background, moving and drawing Aliens and Beams each frame. Mouse clicks create new Beam projectiles from the Ship. The classes define properties and behaviors for each type - Aliens bounce off walls and teleport, Beams move and check for collisions with Aliens, and the Ship moves with mouse position.

Uploaded by

api-294576305
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FLOATING ALIEN///////////////////////////////////////////

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);

boolean collision(Alien al)//bullets hit alien


{
if((bX>al.cX-60) && (bX<al.cX+60) && (bY>al.cY-22) && (bY<al.cY+160))
{
return true;
}
else
return false;
}
}

SHIP///////////////////////////////////////////////////////
class Ship
{
public int sX;
public int sY;
public int sVelo = 5;

Ship(int a,int b)//


{
sX = a;
sY = b;
}
void setX(int xIn)
{
sX = xIn;
}
int getY()
{
return sY;
}
void draw()//Movable Ship
{
fill(255,0,0);
rect(sX,sY,40,20);

}
}

You might also like