/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package shootballs;
/**
*
* @author pet
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import sun.audio.*;
import java.io.*;
public class ShootBalls extends Applet implements MouseListener, Runnable
{
// VARS
//**********************
private Image dbImage;
private Graphics dbg;
Thread th; //Thread
GameTimer mytimer; //Timer object
private Ball ball; //ball object
private Color ballcolor; //Ball color object
Cursor c; //cursor object
Random rnd = new Random (); //Random generator object
private Color [] color_array; //Array of colors for balls
private Ball [] ball_array; //Array to store balls in
int level=1; //Game level being played
int maxballs; //Max number of balls
int maxspeed; //must be at least 2..
private int x,y =0; //x & y position of ball
public int ball_width=20; // inital ball width
public int ball_height = 20; // inital ball height
public int appletsize_x = 310;// x size of applet drawing area for balls
public int appletsize_y = 150;// y size of applet drawing area for balls
int x_speed,y_speed =0; //x & y speed of the balls
private int count = 0; // used for counting loops
private int test =0; // used in test for no more balls
int numcolors = 6; //used to initilize the colors array
public int timeleft; //Time till end of game
int colorIndex=0; //used in filling color array
private int score,hits,miss =0;
public boolean movetonextlevel= false;
private boolean gotHit =false;
public boolean end_of_game = false;
private boolean alive = true;
public AudioClip hitnoise; //Sound objects
public AudioClip gunnoise;
public AudioClip startnoise;
public AudioClip gameovernoise;
Font small = new Font("TimesRoman", Font.PLAIN, 12);
Font large = new Font("TimesRoman", Font.PLAIN, 25);
Font realsmall = new Font("TimesRoman", Font.PLAIN, 10);
//INITILIZE APPLET
//**************************
public static void music()
{
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM;
AudioData MD;
ContinuousAudioDataStream loop = null;
try
{
BGM = new AudioStream(new FileInputStream("wew.au"));
MD = BGM.getData();
loop = new ContinuousAudioDataStream(MD);
}
catch(IOException error){}
MGP.start(loop);
}
public void init()
{
//set inital game level
//***************************
//Init Game
//***********
end_of_game = false;
setGameLevel(level);
int timeleft=60; //Set length of game
//Start game timer
mytimer = new GameTimer(timeleft);
c = new Cursor (Cursor.CROSSHAIR_CURSOR); //Change cursor to cross hair
this.setCursor (c);
setBackground (Color.black); //Set background color
color_array = new Color[] { //Initlize ball color array
Color.blue, Color.yellow, Color.orange,
Color.white, Color.green,Color.red
};
ball_array = new Ball[maxballs];
gunnoise = getAudioClip (getCodeBase() , "gun.au");
hitnoise = getAudioClip (getCodeBase() , "score.au");
startnoise = getAudioClip (getCodeBase() , "teach.au");
gameovernoise = getAudioClip (getCodeBase() , "gameover.au");
startnoise.play(); //Play Game intro music
//Initilize Balls Array
//**********************************
for (count = 0; count < maxballs; count++){
if(count < maxballs){
// Make Ball
//*********
x=(int)(Math.random()*appletsize_x);
if(x < 20){x = 25;}
y=(int)(Math.random()*appletsize_y);
if(y < 20){y = 25;}
x_speed =(int)(Math.random()*maxspeed);
if(x_speed < 1){x_speed=1;}
y_speed = (int)(Math.random()*maxspeed);
if(y_speed < 1){y_speed=1;}
// rnd get color form color array
colorIndex=(int)(Math.random()*numcolors);
if(colorIndex < 0){colorIndex=0;}
ballcolor= color_array[colorIndex];
ball = new Ball(x,y,ball_width,ball_height,ballcolor,x_speed, y_speed,
maxspeed,alive,appletsize_x,appletsize_y);
ball_array[count] = ball;
}
}
}//end init
public void start ()
{
Thread th = new Thread (this); //Create thread for game
th.start ();
music();
}
public void stop()
{
}
public void destroy()
{
removeMouseListener(this);
}
public void run ()
{
addMouseListener(this);
while (end_of_game == false)
{
// Check time remaining
timeleft = mytimer.getTime();
if(timeleft == 0){end_of_game = true;}
// Test for end of game
//test for all balls shot
//******************************************
test=0;
for(count =0; count < maxballs ; count++){
if(ball_array[count].alive == false){
test++;
// test for all balls hit
if(test == maxballs){
if(level<5){
level++;
movetonextlevel= true;
repaint();
delay(6000);
movetonextlevel = false; // reset level flag
setGameLevel(level);
init();
}else end_of_game = true;
}
}
}
// Move balls
for (count = 0; count < maxballs ; count++){
if(ball_array[count].alive == true){
ball_array[count].move();
}
}
repaint();
delay(20);
}
//End of game
//***********************
delay(2000);
gameovernoise.play();
repaint();
destroy();
}//end run
public void delay(int time){
try
{
// Stop Threads time ms
th.sleep (time);
}
catch (InterruptedException ex)
{
// do nothing
}
}
// Mouse implementintation
//*************************
@Override
public void mousePressed(MouseEvent e) {
double x = e.getX();
double y = e.getY();
gunnoise.play();
for(count=0; count< maxballs ; count++){
if(ball_array[count].userHit(x,y)){
ball_array[count].killBall();
gotHit = true;
//CHECK BALLS COLOR
//red=100, white=50 yellow=10, orange =5 cyan=1000 green = 200
//************************************************** **********
if(ball_array[count].color == Color.red){
score +=5;
}else if(ball_array[count].color == Color.white){
score +=10;
}else if(ball_array[count].color == Color.yellow){
score +=50;
} else if(ball_array[count].color == Color.orange){
score +=100;
} else if(ball_array[count].color == Color.blue){
score +=200;
} else if(ball_array[count].color == Color.green){
score +=1000;
}
hitnoise.play();
} else {gotHit=false;}
}//end for
// loose 10 point for miss
//*******************************
if(gotHit == false){score+= -10;}
if(score < 0){score=0;}
}//end mouse pressed
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
repaint();
}
@Override
public void mouseExited(MouseEvent e) {
repaint();
}
//Set Level of play
//*****************************
public void setGameLevel(int level){
if(level == 1){
maxballs = 10;
maxspeed = 3;
ball_height=40;
ball_width=40;
}
if(level == 2){
maxballs = 20;
maxspeed = 4;
ball_height=30;
ball_width=30;
}
if(level == 3){
maxballs = 30;
maxspeed = 5;
ball_height=20;
ball_width=20;
}
if(level == 4){
maxballs = 40;
maxspeed = 5;
ball_height=15;
ball_width=15;
}
if(level == 5){
maxballs = 50;
maxspeed = 5;
ball_height=10;
ball_width=10;
}
}
/** Update - double buffering */
//******************************
@Override
public void update (Graphics g)
{
// Init DoubleBuffers
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// Bildsceen
dbg.setColor (getBackground ());
dbg.fillRect (0, 0,this.getSize().width, this.getSize().height);
// set foreground
dbg.setColor (getForeground());
paint (dbg);
// build offscreen image
g.drawImage (dbImage, 0, 0, this);
}//end update
//PAINT SCREEN
//*******************************
@Override
public void paint (Graphics g)
{
if( movetonextlevel == true){
g.setColor(Color.black);
g.fillRect(0,0,appletsize_x,appletsize_y);
g.setColor(Color.yellow);
g.setFont(large);
g.drawString("Moving to Level: "+level,60,150);
}
g.setColor(Color.yellow);
g.setFont(realsmall);
g.drawString("Ver 1.0, Copyright(c) 2002 By Charles L. Wilson",40,190);
String s = Integer.toString(score);
String l = Integer.toString(level);
String t = Integer.toString(timeleft);
g.setColor(Color.white);
g.setFont(small);
g.drawString("SCORE= "+s,10,180);
g.drawString("SKILL LEVEL= "+l,200,180);
g.drawString("Time:"+t,120,180);
for (count = 0; count < maxballs ; count++){
if(ball_array[count].alive == true){
ball_array[count].drawBall(g);
}
}
if(end_of_game == true){
g.setColor(Color.blue);
g.fillRect(60,117,190,50);
g.setFont(large);
g.setColor(Color.yellow);
g.drawString("END OF GAME",70,150);
}
}
}
---------------------------------------------------------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package shootballs;
/**
*
* @author pet
*/
import java.awt.*;
import java.util.*;
public class Ball{
//Vars here
public boolean alive = true;
public Color color;
private int ballwidth,ballheight;
private int x_pos,y_pos;
private int x_speed;
private int y_speed;
private int aim_adjust;
private int appletsize_x;
private int appletsize_y;
private int maxspeed, first_x,first_y;
private Random rnd = new Random (); //Initilize random generator
//CONTROCTER THE BALL
//**********************************
public Ball(
int x,
int y,
int x_size,
int y_size,
Color color,
int xsp,int ysp,
int max,
boolean state,
int appsize_x,
int appsize_y) {
x_pos = x;
y_pos = y;
first_x=x;
first_y=y;
ballwidth = x_size;
ballheight = y_size;
this.color = color;
x_speed = xsp;
y_speed = ysp;
maxspeed = max;
alive = state;
appletsize_x = appsize_x;
appletsize_y = appsize_y;
}
//LETS MOVE SOMETHING
//********************
public void move(){
y_pos += y_speed;
x_pos += x_speed;
isOut();
}
//DID WE HIT THE BALL?
//******************
public boolean userHit(double maus_x, double maus_y){
if (alive == true){
aim_adjust =(ballwidth/2); // Adjust for bad aim..
double x = maus_x - x_pos - aim_adjust;
double y = maus_y - y_pos-aim_adjust;
double distance = Math.sqrt ((x*x) + (y*y));
if (distance < aim_adjust)
{
return true;
}else return false;
}
return false;
}
//IS THE BALL GOING OUT OF RANGE
//IF SO CHANGE SPEED AND REVERSE DIRECTION
//*******************************
private boolean isOut(){
if(x_pos < 5 ){
if(x_speed <1){
x_speed = (int)(Math.random()*maxspeed);
}
return(true);
}
else if(y_pos < 5 ){
if(y_speed <1){
y_speed = (int)(Math.random()*maxspeed);
}
return(true);
}
else if(y_pos > appletsize_y ){
if(y_speed > -1){
y_speed = (int)(Math.random()*maxspeed);
y_speed= y_speed -(y_speed *2);
}
return(true);
}
else if(x_pos > appletsize_x ){
if(x_speed > -1){
x_speed = (int)(Math.random()*maxspeed);
x_speed= x_speed -(x_speed *2);
}
return(true);
}
return(true);
}
//IF BALL WAS HIT MAKE BALL DIE
//*****************************
public void killBall(){
alive=false;
}
//DRAW THE CURRENT BALL
//*******************************
public void drawBall(Graphics g){
g.setColor(color);
g.fillOval(x_pos,y_pos,ballwidth,ballheight);
}
}//end of class
It would be a great help if somebody could instruct me which codes should i change in this project file in order to change the balls into an Image. I was planning to change the balls into an image of angry bird.