Aircraft of World War II - Add Players

Our players, and a variety can be classified as a class enemy, so we create a new package, modle use and storage of various aircraft. The next step is to create a new Player class to represent our players.

Recall that before the creation of the background image, background image created when we built some constants, such as the length and width of the background image

So when a new player between, we have the players to create length and width, as well as horizontal and vertical coordinates of the player.

package modle;

public class Player {

	public int width = 100;
	
	public int height = 100;
	
	public int x, y;
	
	public Player()
	{
		
		
	}
	
}

The most important images of our players also need to create a new variable, but because our players are multiple pictures from a combination of dynamic effects, so we need to create a picture picture array for storing the player's form animation.
In order to access an array of pictures, so we define an array index variable.
Obtained prior to obtaining background picture function and a function.

package modle;

import java.awt.Image;
import java.awt.Toolkit;

public class Player {

	public int width = 100;
	
	public int height = 100;
	
	public int x, y;
	
	public Image player[] = {
			Toolkit.getDefaultToolkit().getImage("images/player01.png"),
			Toolkit.getDefaultToolkit().getImage("images/player02.png"),
			Toolkit.getDefaultToolkit().getImage("images/player03.png"),
			Toolkit.getDefaultToolkit().getImage("images/player04.png"),
			Toolkit.getDefaultToolkit().getImage("images/player05.png"),
			Toolkit.getDefaultToolkit().getImage("images/player06.png"),
			Toolkit.getDefaultToolkit().getImage("images/player07.png"),
			Toolkit.getDefaultToolkit().getImage("images/player08.png"),
			Toolkit.getDefaultToolkit().getImage("images/player09.png"),
			Toolkit.getDefaultToolkit().getImage("images/player10.png"),
			
	};
	
	public int indexImage = 0;
	
	public Player()
	{
		
		
	}
	
}

The next step is to make our players moving, we need to define the time the timer MyPanel to change the picture depending on the time. The reception MyPanel Player constructor parameter.
Then a new object for the MyPanle constructor MyPanel.

package modle;

import java.awt.Image;
import java.awt.Toolkit;

import view.MyPanel;

public class Player {
	
	public MyPanel myPanel;

	public int width = 100;
	
	public int height = 100;
	
	public int x, y;
	
	public Image player[] = {
			Toolkit.getDefaultToolkit().getImage("images/player01.png"),
			Toolkit.getDefaultToolkit().getImage("images/player02.png"),
			Toolkit.getDefaultToolkit().getImage("images/player03.png"),
			Toolkit.getDefaultToolkit().getImage("images/player04.png"),
			Toolkit.getDefaultToolkit().getImage("images/player05.png"),
			Toolkit.getDefaultToolkit().getImage("images/player06.png"),
			Toolkit.getDefaultToolkit().getImage("images/player07.png"),
			Toolkit.getDefaultToolkit().getImage("images/player08.png"),
			Toolkit.getDefaultToolkit().getImage("images/player09.png"),
			Toolkit.getDefaultToolkit().getImage("images/player10.png"),
			
	};
	
	public int indexImage = 0;
	
	public Player(MyPanel myPanel)
	{
		this.myPanel = myPanel;
		
	}
	
}

Then we can use the time to create animations, then draw our players.

Write drawPlayer () method we use to remember Graphics g parameters of this method to draw.

	public void drawPlayer(Graphics g)
	{
		g.drawImage(this.player[indexImage], x, y, width, height,null);

	}

Then there is the use of myPanel in time to change the picture, animation.


```java
if(myPanel.time % 50 == 0)
		{
			indexImage ++;
			
			if(indexImage == player.length)
			{
				indexImage = 0;
			}
		}

Animation over the last set position of the player (using a window class BaseFrame static constant, to change the width and height of the window position)

public Player(MyPanel myPanel)
	{
		this.myPanel = myPanel;
		this.x = (BaseFrame.frameWidth - this.width) / 2;
		this.y = (BaseFrame.frameHeight - this.height * 2) ;
		
	}

The next step is to call the draw method MyPanel drawPlayer ()
1. Create a Player object,
2.MyPanel constructor add the player object
3. Call the draw method drawPlayer with the player ()

package view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JPanel;

import modle.Player;
import thread.DrawbleThread;

public class MyPanel extends JPanel{
	
	public Image bgImage;
	
	public int time = 0;
	public int top = 0;
	
	public DrawbleThread drawbleThread;
	
	public Player player;
	
	
	public MyPanel()
	{
		//设置背景
		this.bgImage = Toolkit.getDefaultToolkit().getImage("images/bg01.jpg");
		//添加玩家
		this.player = new Player(this);
		//创建线程
		this.drawbleThread = new DrawbleThread(this);
		//启动线程
		this.drawbleThread.start();
		
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(this.bgImage, 0,
				top-this.bgImage.getHeight(this), 
				this.bgImage.getWidth(this),
				this.bgImage.getHeight(this),
				null);
		g.drawImage(this.bgImage, 0,top, 
				this.bgImage.getWidth(this),
				this.bgImage.getHeight(this),
				null);
		
		time ++;
		
		if(time == 10000)
			time = 0;
		
	
		if(time % 10 == 0)
		{
			top ++;
			
			if(top >= this.bgImage.getHeight(this))
			{
				top = 0;
			}
			
		}
		
		this.player.drawPlayer(g);
		
	}
	
	
}

Published 24 original articles · won praise 1 · views 1458

Guess you like

Origin blog.csdn.net/qq_43077318/article/details/104644569