Java——坦克大戰(2)

本次實現:

  • 讓英雄坦克上下左右轉向
  • 利用集合添加敵軍坦克組
  • 優化相關參數
  • 將坦克類分離爲單獨的.java文件(同包)
TankGame:
package com.tank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TankGame extends JFrame{
	
	MyPanel mp = null;
	
	public static void main(String[] args) {
		TankGame test = new TankGame();

	}
		
		//構造函數
	public TankGame(){
		mp = new MyPanel();
		
		this.add(mp);
		this.addKeyListener(mp);
		
		this.setSize(400, 300);
		this.setTitle("坦克大戰");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	

}

class MyPanel extends JPanel implements KeyListener{
	//定義我的坦克
	Hero hero = null;
	
	//定義敵軍坦克組,vector tanks 簡寫爲vts
	Vector <Enemy> vts= new Vector <Enemy>();
	
	int eSize = 3;
	
	
	public MyPanel(){
		hero = new Hero(186, 235);
		hero.setColor(0);
		
		//初始化敵軍坦克
		for(int i=0; i<eSize; i++){
			Enemy etank = new Enemy(i*186, 0);
			vts.add(etank);
			vts.get(i).setColor(1);
			vts.get(i).setDirect(1);
		}
		
	}
	
	//重寫paint方法
	public void paint(Graphics g){
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		
		//調用畫坦克函數 畫出英雄的坦克
		this.drawTank(this.hero.getX(), this.hero.getY(), g, this.hero.getDirect(), this.hero.getColor());
		
		//畫出敵軍坦克
		for(int i=0; i<vts.size(); i++){
			this.drawTank(vts.get(i).getX(), vts.get(i).getY(), g, vts.get(i).getDirect(), vts.get(i).getColor());
		}
	}
	//畫坦克函數
	public void drawTank(int x, int y, Graphics g, int direct, int color){
			//定義坦克顏色
		switch	(color){
		case 0:
			g.setColor(Color.GREEN);
			break;
		case 1:
			g.setColor(Color.red);
			break;
		}
			////判斷坦克方向
		switch (direct){
			//向上
		case 0:
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x+5, y+5, 10, 20, false);
			g.fillOval(x+5, y+10, 10, 10);
			g.drawLine(x+10, y+10, x+10, y);
			g.fill3DRect(x+15, y, 5, 30, false);
			break;
			//向下
		case 1:
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x+5, y+5, 10, 20, false);
			g.fillOval(x+5, y+10, 10, 10);
			g.drawLine(x+10, y+10, x+10, y+30);
			g.fill3DRect(x+15, y, 5, 30, false);
			break;
			//向左
		case 2:
			g.fill3DRect(x, y+10, 30, 5, false);
			g.fill3DRect(x+5, y+15, 20, 10, false);
			g.fillOval(x+10, y+15, 10, 10);
			g.drawLine(x+15, y+20, x, y+20);
			g.fill3DRect(x, y+25, 30, 5, false);
			break;
			//向右
		case 3:
			g.fill3DRect(x, y+10, 30, 5, false);
			g.fill3DRect(x+5, y+15, 20, 10, false);
			g.fillOval(x+10, y+15, 10, 10);
			g.drawLine(x+15, y+20, x+30, y+20);
			g.fill3DRect(x, y+25, 30, 5, false);
			break;
		}
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO 自動生成的方法存根
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
		if(e.getKeyCode() == KeyEvent.VK_UP){
			this.hero.setDirect(0);
			this.hero.moveUp();
		}else if((e.getKeyCode() == KeyEvent.VK_DOWN)){
			this.hero.setDirect(1);
			this.hero.moveDown();
		}else if((e.getKeyCode() == KeyEvent.VK_LEFT)){
			this.hero.setDirect(2);
			this.hero.moveLeft();
		}else if((e.getKeyCode() == KeyEvent.VK_RIGHT)){
			this.hero.setDirect(3);
			this.hero.moveRight();
		}
		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO 自動生成的方法存根
		
	}
	
}

Tank類:
package com.tank;

class Tank{
	//坦克橫、縱座標 
	int x = 0;	
	int y = 0;	
	//移動速度
	int speed = 2;
	//坦克顏色
	int color = 0;
	
	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	//坦克方向   0 = 上, 1 = 下,2 = 左, 3 = 右
	int direct = 0;

	public int getDirect() {
		return direct;
	}

	public void setDirect(int direct) {
		this.direct = direct;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Tank(int x, int y){
		this.x = x; 
		this.y = y;
	}
}

	//英雄坦克——我的
class Hero extends Tank{
	//坦克的初始位置
	public Hero (int x, int y){
		super(x, y);
		
	}
	//我的坦克 移動方法
	public void moveUp(){
		y-=speed;
	}
	public void moveDown(){
		y+=speed;
	}
	public void moveLeft(){
		x-=speed;
	}
	public void moveRight(){
		x+=speed;
	}
	
}

//敵方坦克
class Enemy extends Tank{

	public Enemy(int x, int y) {
		super(x, y);
	}
	
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章