Professional Documents
Culture Documents
Java Code For Video Game
Java Code For Video Game
// You may use this class for the satellite and bomber machine (or any
other future enemies to combat them)
class Added_enemy{
Particle bomb;
int bomb_count;
PVector enemy;
float drop_location;
boolean is_alive;
boolean is_shot;
int r;
Added_enemy(float x, float y,int r, float drop){
enemy=new PVector(x,y);
bomb_count=(int)random(1,5);
is_alive=true;
this.r=r;
drop_location=drop;
is_shot=false;
}
///checking if the enemy has reached the location its suppposed to drop its
payload at
boolean check_drop(){
if((int)enemy.x<=(int)drop_location && bomb_count>0 && is_alive==true){
drop_location=(float)random(enemy.x/2,enemy.x);
return true;
}
else
return false;
}
City
class City{
float x1,y1,x2,y2;
City(float x1, float y1, float x2, float y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
Level setup
PVector gravity;
class Level_Setup{
int wave;
int noParticles;
Particle []p;
int Width;
int Height;
PVector gravity;
Level_Setup(int w, int x, int y){
wave=w;
Width=x;
Height=y;
noParticles=30+(wave*10);
p=new Particle[noParticles];
for(int i=0; i<noParticles;i++){
Missile command
int wave;
Particle p[];
PVector g;
float scrHeight;
int counter, rcounter;
City cities[];
float distance;
float altitude;
float velx;
int numb;
int c;
boolean city_dead[];
boolean particle_dead[];
int score;
int particle_count;
Silo s[];
boolean silo_dead[];
Particle missiles[];
int active_missile_counter;
int detonation_counter[];
int detonation_counter_temp[];
int detonation_updater[];
int explosion_counter[];
int explosion_counter_temp[];
int explosion_updater[];
int dead_missiles;
boolean start;
boolean reset;
int extra_particles;
int splitting_particles;
int[] child_particles;
boolean[] is_splitable;
boolean is_killable[];
int immortality_timer[];
boolean explosion_flag[];
Added_enemy bomber;
Added_enemy satellite;
int enemy_counter;
boolean bomber_is_alive;
boolean satellite_is_alive;
int smart_bomb;
boolean smart_bomb_activated;
float smart_bomber_originalvelx,smart_bomber_originalvely;
float smart_timer;
boolean frame;
int score_reduction;
void setup(){
// frameRate(30);
size(800,600);
noCursor();
reset=false;
//calling the start function to setup the global variables to their default value
start();
}
// setting the global variables that are not reset every frame to their default value
void start(){
score_reduction=0;
enemy_counter=0;
bomber_is_alive=false;
satellite_is_alive=false;
smart_timer=1;
start=reset;
score=0;
c=0;
distance=width/11f;
altitude=height;
wave=0;
extra_particles=0;
add_particles();
cities=new City[6];
city_dead= new boolean[cities.length];
for(int i=0;i<city_dead.length;i++){
city_dead[i]=false;
}
s=new Silo[3];
silo_dead=new boolean[3];
missiles=new Particle[31];
active_missile_counter=0;
scrHeight=(float)height/18000000f;
g=new PVector(0 ,9.8*scrHeight);
detonation_counter= new int[30];
detonation_updater= new int[30];
detonation_counter_temp= new int[30];
for(int i=0;i<30;i++){
detonation_counter[i]=1;
detonation_counter_temp[i]=1;
detonation_updater[i]=1;}
add_silos();
dead_missiles=0;
}
if(wave>1){
handle_bomber();
handle_satellite();
}
check_wave();
particle_count=0;
shoot_missile();
}
//initializing and drawing the bomber object and initializing its flag
void add_bomber(){
bomber=new
Added_enemy((float)random(width+50,width+250+(200*wave)),(float)height/2,40,
(float)random(width/2,width));
bomber.draw_enemy();
bomber_is_alive=bomber.is_alive;
}
//moving the bomber every frame, checking if it reached the drop location
//dropping a particle if it reached the drop location
void handle_bomber(){
bomber.move(wave,0);
bomber.draw_enemy();
float velx;
float vely;
int target=(int)random(0,5);
switch(target){
case 0:velx=(2*distance+distance/192f);break;
case 1:velx=(3.1*distance+distance/192f);break;
case 2:velx=(4.2*distance+distance/192f);break;
case 3:velx=(6.3*distance+distance/192f);break;
case 4:velx=(7.4*distance+distance/192f);break;
case 5:velx=(8.5*distance+distance/192f);break;
default: velx=0;
}
//setting the particle's initial velocity's y coordinate as per the screen size
constraints
vely=2+(float)wave/32;
if(bomber.drop_bomb(velx,vely)){
int original_length=p.length;
//temporary variables to store the original values of the particle array and
the various arrays that work with the particle array
Particle temp[]=p;
boolean tparticle_dead[]=particle_dead, tis_splitable[]=is_splitable,
tis_killable[]=is_killable,texplosion_flag[]=explosion_flag;
int texplosion_counter[]=explosion_counter,
texplosion_counter_temp[]=explosion_counter_temp,
texplosion_updater[]=explosion_updater, timmortality_timer[]=immortality_timer;
p=new Particle[original_length+1];
particle_dead=new boolean[p.length];
explosion_counter=new int[p.length];
explosion_counter_temp=new int[p.length];
explosion_updater=new int[p.length];
is_splitable=new boolean[p.length];
immortality_timer=new int[p.length];
is_killable=new boolean[p.length];
explosion_flag=new boolean[p.length];
for(int i=0;i<original_length;i++){
is_killable[i]=tis_killable[i];
immortality_timer[i]=timmortality_timer[i];
particle_dead[i]=tparticle_dead[i];
explosion_counter[i]=texplosion_counter[i];
explosion_counter_temp[i]=texplosion_counter_temp[i];
explosion_updater[i]=texplosion_updater[i];
explosion_flag[i]=texplosion_flag[i];
p[i]=temp[i];
is_splitable[i]=tis_splitable[i];
}
//adding the extra particle and its flags at the end of each array
explosion_counter[p.length-1]=1;
explosion_counter_temp[p.length-1]=1;
explosion_updater[p.length-1]=1;
particle_dead[p.length-1]=false;
is_splitable[p.length-1]=false;
immortality_timer[p.length-1]=0;
p[p.length-1]=bomber.bomb;
is_killable[p.length-1]=true;
}
}
//initializing and drawing the satellite object and initializing its flag
void add_satellite(){
satellite=new
Added_enemy((float)random(width+100,width+250+(200*wave)),(float)heig
ht/4,30, (float)random(width/2,width));
satellite.draw_enemy();
satellite_is_alive=satellite.is_alive;
}
//basically doing the same as the bomber handling function with different
values to move the satellite at a different speed
void handle_satellite(){
satellite.move(wave,0.5);
satellite.draw_enemy();
float velx;
float vely;
int target=(int)random(0,5);
switch(target){
case 0:velx=(2*distance+distance/192f);break;
case 1:velx=(3.1*distance+distance/192f);break;
case 2:velx=(4.2*distance+distance/192f);break;
case 3:velx=(6.3*distance+distance/192f);break;
case 4:velx=(7.4*distance+distance/192f);break;
case 5:velx=(8.5*distance+distance/192f);break;
default: velx=0;
}
velx=(velx+((distance/4f)-(distance/96f)-
satellite.enemy.x))/(280+(abs(satellite.enemy.y)/30));
vely=2+(float)wave/32;
if(satellite.drop_bomb(velx,vely)){
int original_length=p.length;
Particle temp[]=p;
boolean tparticle_dead[]=particle_dead, tis_splitable[]=is_splitable,
tis_killable[]=is_killable,texplosion_flag[]=explosion_flag;
int texplosion_counter[]=explosion_counter,
texplosion_counter_temp[]=explosion_counter_temp,
texplosion_updater[]=explosion_updater, timmortality_timer[]=immortality_timer;
p=new Particle[original_length+1];
particle_dead=new boolean[p.length];
explosion_counter=new int[p.length];
explosion_counter_temp=new int[p.length];
explosion_updater=new int[p.length];
is_splitable=new boolean[p.length];
immortality_timer=new int[p.length];
is_killable=new boolean[p.length];
explosion_flag=new boolean[p.length];
for(int i=0;i<original_length;i++){
is_killable[i]=tis_killable[i];
immortality_timer[i]=timmortality_timer[i];
particle_dead[i]=tparticle_dead[i];
explosion_counter[i]=texplosion_counter[i];
explosion_counter_temp[i]=texplosion_counter_temp[i];
explosion_updater[i]=texplosion_updater[i];
explosion_flag[i]=texplosion_flag[i];
p[i]=temp[i];
is_splitable[i]=tis_splitable[i];
}
explosion_counter[p.length-1]=1;
explosion_counter_temp[p.length-1]=1;
explosion_updater[p.length-1]=1;
particle_dead[p.length-1]=false;
is_splitable[p.length-1]=false;
immortality_timer[p.length-1]=0;
p[p.length-1]=satellite.bomb;
is_killable[p.length-1]=true;
}
}
void crosshair(){
stroke(0);
line(mouseX-6,mouseY-6,mouseX-2,mouseY-2);
line(mouseX+6,mouseY+6,mouseX+2,mouseY+2);
line(mouseX+6,mouseY-6,mouseX+2,mouseY-2);
line(mouseX-6,mouseY+6,mouseX-2,mouseY+2);
noStroke();
fill(255,0,0);
circle(mouseX,mouseY,1);
}
//checking the wave condition and handling what happens when the wave
is passed or failed
void check_wave(){
while(missiles[i]!=null){
//just to confirm we havent crossed the limit of missiles alllowed per wave
if(active_missile_counter!=31){
PVector position=missiles[i].position;
//moving the missile and applying gravity on it
missiles[i].integrate(g);
if(particle_dead[smart_bomb]==false){
///checking if the smart bomb is less than 50 pixels away from the
incoming missile
if(missiles[i].position.y - p[smart_bomb].position.y <= 50
&& smart_bomb_activated==false
&& ((tvelx>0 && missiles[i].position.x - p[smart_bomb].position.x <= 50)||(tvelx<0
&& p[smart_bomb].position.x - missiles[i].position.x <= 50))){
//checking if the smart bomb is approaching the incomng missile and is not too close
to expected explosion(150 pixels)
else if(missiles[i].position.y - p[smart_bomb].position.y <= 150
&& missiles[i].position.y - p[smart_bomb].position.y >= 50
&& smart_bomb_activated==false
&& ((tvelx>0 && missiles[i].position.x - p[smart_bomb].position.x <= 50)
||(tvelx<0 && p[smart_bomb].position.x - missiles[i].position.x <= 50))){
smart_bomber_originalvelx=tvelx;
smart_bomber_originalvely=tvely;
smart_bomb_activated=true;
//just to keep tract of its velocity and overwrite any false updations
p[smart_bomb].velocity=new PVector(tvelx,tvely);
if(frame==true){
smart_timer--;
frame=false;
}
}
}
}
}
//changing the updater for the explosion radius and timer to start
reducing the respective value to save a step and variable in updation
if(abs(detonation_counter_temp[i])==80)
detonation_updater[i]*=(-1);
}
}
else{
fill(255,10,100);
rect(position.x,position.y,3,2);
}
i++;
}
else
return;
}
}
//if a particle collides with the missile explosion, explode the particle in a
similar way
void explode_particle(int index){
noStroke();
fill(255,243,166);
circle(p[index].position.x,p[index].position.y,explosion_counter[index]);
if(explosion_counter_temp[index]<40 && explosion_counter[index]>0)
explosion_counter[index]+=explosion_updater[index];
explosion_counter_temp[index]+=explosion_updater[index];
if(abs(explosion_counter_temp[index])==80)
explosion_updater[index]*=(-1);
if(wave>1){
boolean tempa = bomber_is_alive, tempb = satellite_is_alive;
bomber_is_alive=bomber.check_collision(p[index].position.x,p[index].position.y,explosio
n_counter[index]);
satellite_is_alive=satellite.check_collision(p[index].position.x,p[index].position.y,explosio
n_counter[index]);
if(bomber_is_alive==false && tempa==true)
score+= (100*(wave>10?6:((wave+1)/2)));
if(satellite_is_alive==false && tempb==true)
score+= (100*(wave>10?6:((wave+1)/2)));
}
for(int i=0;i<p.length;i++){
boolean ptemp=particle_dead[i];
if(is_killable[i])
particle_dead[i]=p[i].particle_dead(p[index].position.x,p[index].position.y,explosion_co
unter[index]);
else if(immortality_timer[i]==0){is_killable[i]=true;}
else if(immortality_timer[i]>0) {immortality_timer[i]--;}
if((particle_dead[i]==true && ptemp!=true)){
score+= (25*(wave>10?6:((wave+1)/2)));
//to set the flag for dead particles so they wont be used in any other
function and wont be drawn
void remove_particles(){
for(int i=0;i<p.length;i++)
particle_dead[i]=p[i].particle_dead();
}
//to add particles to the particlee array and to initialize all the arrays
supporting the main particle arrays with default values
void add_particles(){
p= new Particle[15+(wave++*5)];
is_killable=new boolean[p.length]; //to store if a particle is killable or not (helps while
splitting particles to give them time to escape the explosion they were born from)
immortality_timer=new int[p.length]; //to keep track of the time left before the split
particles are killable
is_splitable=new boolean[p.length]; //to keep track of all the splitable particles
explosion_flag=new boolean[p.length]; // flag to make sure explosion starts once foor each
particle
for(int i=0;i<p.length;i++)
is_splitable[i]=false;
if(wave>=2){
splitting_particles=wave*2;
for(int i=0;i<splitting_particles;i++){
int split_random=(int)random(0,p.length);
if(is_splitable[split_random]==true)
i--;
else
is_splitable[split_random]=true;
}
}
particle_dead=new boolean[p.length];
explosion_counter=new int[p.length];
explosion_counter_temp=new int[p.length];
explosion_updater=new int[p.length];
for(int i=0;i<p.length;i++){
is_killable[i]=true;
immortality_timer[i]=0;
explosion_counter[i]=1;
explosion_counter_temp[i]=1;
explosion_updater[i]=1;
explosion_flag[i]=false;
particle_dead[i]=false;
int target=6*location/800;
//checking to see if therre are any particles or cities left to decide if the wave has
been won(1) or lost(2) or still going on(0)
int check_wave_condition(){
for(int j=0;j<city_dead.length;j++)
if(city_dead[j]==false){
for(int i=0;i<particle_dead.length;i++)
if(particle_dead[i]==false || bomber_is_alive==true ||
satellite_is_alive==true)
return 0;
return 1;}
return 2;
//add the silos each round and reset the missile counter sso that each silo
has 10 missile
void add_silos(){
dead_missiles=0;
float dimensions[]= new float[4];
for(int i=0;i<s.length;i++){
silo_dead[i]=false;
switch(i){
case 0: {
dimensions[0]=0.5*distance+(distance/192f);
dimensions[1]=altitude-25;
dimensions[2]=(distance)-(distance/96f);
dimensions[3]= altitude/20f;
}break;
case 1: {
dimensions[0]=5*distance+(distance/192f);
dimensions[1]=altitude-25;
dimensions[2]=(distance)-(distance/96f);
dimensions[3]= altitude/20f;
}break;
case 2: {
dimensions[0]=9.4*distance+(distance/192f);
dimensions[1]=altitude-25;
dimensions[2]=(distance)-(distance/96f);
dimensions[3]= altitude/20f;
}break;
}
s[i]=new
Silo(dimensions[0],dimensions[1],dimensions[0]+dimensions[2],dimensions[1]+dimensions
[3]);
}
}
//drawing the silos each frame with the updated missile counter aand their
state(alive or dead)
void draw_silos(float px,float py){
for(int i=0;i<s.length;i++){
if(s[i].isAlive(px,py) && silo_dead[i]==false){
noStroke();
fill(58,74,44);
rect(s[i].x1,s[i].y1,s[i].x2-s[i].x1,s[i].y2-s[i].y1);
}
else{
noStroke();
fill(94,44,29);
rect(s[i].x1,s[i].y1,s[i].x2-s[i].x1,s[i].y2-s[i].y1);
silo_dead[i]=true;
if(s[i].missile_counter!=0)
dead_missiles=s[i].missile_counter;
s[i].missile_counter=0;
}
textAlign(CENTER);
fill(255);
text(s[i].missile_counter,((s[i].x1+s[i].x2)/2),(s[i].y1+s[i].y2)/2);
}
}
//handling the mouse click event to store the coordinates of the click to
launch a missile to the destination from the nearest silo with usaable
missiles
void mouseClicked(){
if(active_missile_counter+dead_missiles<30 && check_wave_condition()!=2 &&
start==true && s[0].missile_counter+s[1].missile_counter+s[2].missile_counter >0 ){
float mtargetx=mouseX;
float mtargety=(600-mouseY);
int origin=3*(int)(mtargetx-2)/800;
shoot(origin, mtargetx, mtargety);
}
}
//handling key press events to give the user control over while silo they
want the missile to be launched from
void keyPressed(){
if((keyCode == UP ||keyCode == RIGHT ||keyCode == LEFT) && start==true ){
if(active_missile_counter+dead_missiles<30 && check_wave_condition()!=2){
float mtargetx=mouseX;
float mtargety=(600-mouseY);
int origin;
if(keyCode == UP) origin = 1;
else if(keyCode == RIGHT) origin = 2;
else origin = 0;
shoot(origin, mtargetx, mtargety);
}
}
}
//the function actually creating the missile object and giving it initial
velocity (which is slightly higher if shot from the center missile silo)
void shoot(int origin, float mtargetx, float mtargety){
if(s[origin].missile_counter==0){
switch(origin){
case 0:{if(s[1].missile_counter!=0) origin=1; else if(s[2].missile_counter!=0) origin=2;
}break;
case 1:{ origin= (int)mtargetx/400; if(origin==1) origin=2; if(s[0].missile_counter==0)
origin=2; else if(s[2].missile_counter==0) origin=0;}break;
case 2:{if(s[1].missile_counter!=0) origin=1; else if(s[0].missile_counter!=0) origin=0;
}break;
}
}
if(origin==1)
s[origin].shoot_missile((s[origin].x1+s[origin].x2)/2,s[origin].y1,(mtargetx-
((s[origin].x1+s[origin].x2)/2))/23,-mtargety/23,1,mouseX,mouseY);
else
s[origin].shoot_missile((s[origin].x1+s[origin].x2)/2,s[origin].y1,(mtargetx-
((s[origin].x1+s[origin].x2)/2))/28,-mtargety/29,1,mouseX,mouseY);
missiles[active_missile_counter++]=s[origin].missile;
Particle
//particle class tweaked from the particle class used in the tutorial sketch for
drag
class Particle{
//using the same way to calculate motion and gravity but different
collisions as per the world
void integrate(PVector force) {
if (invMass <= 0f) return ;
position.add(velocity) ;
acceleration.add(force) ;
acceleration.mult(1/invMass) ;
if(velocity.mag()<2)
velocity.add(acceleration) ;
velocity.mult(drag);
if ((position.x < 0) || (position.x > width)) velocity.x = -velocity.x*0.9 ;
if (position.y > height-5) {velocity.y = 0 ; velocity.x = 0;}
}
//class silo that uses the basic instances and methods of the city class but
adds arguments and a method for missiles
Particle missile;
int missile_counter;
missile_counter=10;
//creating a new object for missile each time this function is called
void shoot_missile(float x, float y, float velx, float vely, float m, float dx, float
if(missile_counter>0){
missile=new Particle(x,y,velx,vely,m,dx,dy);
missile_counter--;
else
return;