编程知识 cdmana.com

The first small game written by Java

Realize the idea

1. How to analyze ?

First step : Discovery class ( object )

           	 figure - Clown. ( The enemy - Friendly party ):
	         The bullet - hat :
      wall :
	      explosive :

The second step : Find attributes

	         Clown. :   Wide and high  ,  Location (x y),  Movement speed 
             hat :   Wide and high  ,  Location (x y),  Movement speed 
		 wall :   Wide and high  ,  Location (x y)
		 explosive : Wide and high  ,  Location (x y)

The third step : Find the way

		 Clown. :    Move  ,  attack  ,  Characters hit the border  ,
		 The bullet :    Move  ,  The bullet hit the wall  ,  The bullet hit the border  ,
		 wall :    Stay still 
       explosive :  The explosives are gone 

2. Where is the difficulty ?

1 How to load a picture into a form

  • Background image loading
  • figure - Clown load
  • Missiles - Cap loading Missile
  • wall - load Wall
  • explosive - load Explode

2 How to create a form

3 How to fire a bullet ( How to use the keyboard to trigger events )

3. The second edition

  • Access network , Multi man combat

The realization of the first day

1. Create a project (ylm)

2. Import the image file you need to use to Java

3. In the project src Create a package in (com.ytzl.ylm1)

4. stay com.ytzl.ylm1 Create a subpackage in the package —— tool kit (util) And write call picture method

public static Image getImage(String imgPath) {
   
   
    ImageIcon imageIcon = new ImageIcon(imgPath);
    return imageIcon.getImage();
}

5. stay com.ytzl.ylm1 Create the game client class in the package (GameClient) And inheritance Frame class

6. Realize the game window

// Define the path of the background image 
public static final String BG_PATH = "images/bg.png";
//  Define a static variable for an image 
private static Image image;
//  A static block , All resources ( picture , Audio , video ) Just load once 
static {
   
   
    image =  CommonUtils.getImage(BG_PATH);
}
/**
 *  Game entry 
 */
public static void main(String[] args) {
   
   
    GameClient gameClient = new GameClient();
    //  Start the game 
    gameClient.start();
}

/**
 *  Start the game 
 */
public void start() {
   
   
    System.out.println(" The game will begin soon , Please get ready ");
    //TODO  Game business 
    // Set title 
    this.setTitle(" Forgive the hat belt war ");
    // Set the size and position of the form 
    this.setBounds(0, 0, 700, 500);
    //  Let the form show 
    this.setVisible(true);
    System.out.println("Game Over!!!");
}

7. Insert background image

/**
 *  Drawing a picture , Override the paint Method 
 * Graphics  Brush class 
 *
 * @param g  paint brush 
 */
@Override
public void paint(Graphics g) {
   
   
    //  Draw a background picture 
    g.drawImage(image, 0, 0, 700, 500, this);

}

8. Insert a character picture ( Create character classes Buffoon)

//  Character image properties 
public static final Image buffoonImage= CommonUtils.getImage("images/body/s-left.png");
//  figure - Abscissa 
private int x;
//  figure - Ordinate 
private int y;
//  picture - Width 
private int width;
//  picture - Height 
private int height;
//  figure - Speed 
private int speed;

8.1 Insert a character picture ( Client implementation )

//  Create a character - Clown. 
private Buffoon buffoon = new Buffoon();
//  Drawing figures - Clown. 
g.drawImage(Buffoon.buffoonImage, 160, 100, 50, 50, this);

9. Insert a picture of the hat ( Create a hat class )

// Missile image path properties 
public static final Image missileImage = CommonUtils.getImage("images/missile.png");
//  hat - Height 
private int height;
//  hat - length 
private int width;
//  hat - Speed 
private int speed;
//  hat - Abscissa 
private int x;
//  hat - Ordinate 
private int y;

9.1 Insert a picture of the hat ( Client implementation )

//  Create the missile - hat 
private Missile missile = new Missile();
//  Draw a picture of the projectile - hat 
g.drawImage(Missile.missileImage,200,200,20,20,this);

10. Insert an obstacle image ( Create an obstacle class Wall)

// Horizontal wall image properties 
public static final Image wallImageH = CommonUtils.getImage("images/wall-h.png");
// Vertical wall image properties 
public static final Image wallImageZ = CommonUtils.getImage("images/wall-v.png");
// wall - Height 
private int height;
// wall - length 
private int width;
// wall - Abscissa 
private int x;
// wall - Ordinate 
private int y;

10.1 Insert an obstacle image ( On the client side )

//  Create walls 
private Wall wall=new Wall();
//  Draw the wall 
g.drawImage(Wall.wallImageH,220,120,70,30,this);
g.drawImage(Wall.wallImageZ,430,230,30,70,this);

11. Insert an explosive picture ( Create an explosive class Explode)

// Missile image path properties 
public static final Image explodeImage = CommonUtils.getImage("images/explode.png");
// wall - Height 
private int height;
// wall - length 
private int width;
// wall - Abscissa 
private int x;
// wall - Ordinate 
private int y;

11.1 Insert an explosive picture ( On the client side )

//  Create explosives 
private Exception exception=new Exception();
//  Painting explosives 
g.drawImage(Explode.explodeImage,500,300,70,70,this);

The first day ( complete ) The renderings are as follows :

 Insert picture description here

版权声明
本文为[osc_ jmtenr3d]所创,转载请带上原文链接,感谢

Scroll to Top