Horizontal Movement with Ground

hundredcarriageΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

63 εμφανίσεις

/**


* @(#)FirstGame.java


*


* FirstGame Applet application


*


* @author


* @version 1.00 2010/11/9


*/



import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;



public

class FirstGame4HorizontalMovement extends Applet implements Runnable, KeyListener {


Thread t1;


Point p;


Ball b;


Image img,dbImage;


String file;


Ship ship;


Graphics dbg;


Ship[] myList;


ground grnd;


int secs, mins, hrs, elapse;






public

void init() {



mins = 0;



hrs = 0;



secs = 0;



elapse = 0;



t1 = new Thread(this);



addKeyListener(this);



img = getImage(getCodeBase(), "ship.gif");



p = new Point(250,5);



//

b = new Ball(1,100,100,Color.blue,50,50);



myList = new Ship[5];



f
or(int i=0;i<myList.length;i++){




myList[i] = new Ship(p.x,p.y+i*50,img);







}



img = getImage(getCodeBase(), "ground.gif");



grnd = new ground(img,0,0);





}


public void start(){



System.out.println("starting...");



t1.start();





}


public vo
id run(){



while(true){




try{





elapse++;





secs += elapse%2;





for (int k=0;k<myList.length;k++){







myList[k].fly();





}





if (myList[0].location.x >= 300)






grnd.moveLeft();





repaint();





Thread.sleep(500);









}

catch(
Exception e){




}



}


}


public void paint(Graphics g) {






// secs /=2;


if(secs == 60)


{


mins++;


secs = 0;


}


if(mins == 60)


{


hrs++;



secs = 0;


mins = 0;


}


g.drawImage(grnd.img,grnd.p.x,grnd.p.y,this);


//gr.setFont(bigFont);


g.setColor(Color.red);


//gr.drawString(msg1,10,100);


g.drawString(" "+ hrs + " Hours " +
mins + " Minutes " + secs + " Seconds", 10, 130);







for(int j=0; j<myList.length;j++){





g.drawImage(myList[j].pic,myList[j].location.x,myList[j].location.y,this);



}//for loop


}

public void update (Graphics g)

{



// Initializing DoubleBuffers



i
f (dbImage == null)



{




dbImage = createImage (this.getSize().width, this.getSize().height);




dbg = dbImage.getGraphics ();



}





dbg.setColor (getBackground ());



dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);





dbg.setColor (
getForeground());



paint (dbg);





g.drawImage (dbImage, 0, 0, this);



}


public void keyPressed(KeyEvent e){



System.out.println("Code: " + e.getKeyCode());



if(e.getKeyCode()==40)




ship.location.y+=5;



else if(e.getKeyCode()==38)




ship.location.y
-
=5;



else if(e.getKeyCode()==39)




ship.location.x+=5;



else if(e.getKeyCode()==37)




ship.location.x
-
=5;






}


public void keyTyped(KeyEvent e){


}


public void keyReleased(KeyEvent e){


}

}