Java原創(chuàng)創(chuàng)意,自制素材,加音效坦克大戰(zhàn),原創(chuàng)up主游戲,較完整簡單版2【詩書畫唱】
含部分自己寫的注釋,讓你更容易看懂代碼!求三連關(guān)注!制作不易!

package img;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class img {
// 如果文件在別的文件夾里,就把別的文件夾的名字
// 寫在下面的英文的雙引號""里面
private static final String RESOURCE_PATH = "";
/** 讀取圖片 */
public static BufferedImage loadImage(String fileName) {
try {
// 同包之內(nèi)的圖片讀取
BufferedImage img = ImageIO.read
(new File(RESOURCE_PATH+fileName));
return img;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}


package mains;
import img.img;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import duiXiang.*;
/**
?* 主函數(shù)的類:
?*/
public class mains extends JPanel {
private static final long serialVersionUID?
= 2547093170915849250L;
// 下面設(shè)置一個可以全局通過"mains.quanJvWidth"等就可調(diào)用的寬度:
public static final int quanJvWidth = 400;
// 下面設(shè)置一個可以全局通過"mains.quanJvHeigh"等就可調(diào)用的高度:
public static final int quanJvHeight = 700;
/*
* 鼠標(biāo)點擊事件:
* 開始--運行? ?結(jié)束---開始
* 鼠標(biāo)移動事件:
* 啟動狀態(tài):結(jié)束----啟動
* 暫停狀態(tài):鼠標(biāo)從屏幕里劃到外
* 運行狀態(tài):從外到內(nèi)的時候
* 結(jié)束狀態(tài):英雄機的生命為0
*/
// 開始狀態(tài)
public static final int START = 0;
public static BufferedImage start;
// 運行狀態(tài)
public static final int RUNNING = 1;
// 暫停狀體
public static final int PAUSE = 2;
public static BufferedImage pause;
// 結(jié)束狀態(tài)
public static final int GAME_OVER = 3;
public static BufferedImage gameover;
//默認(rèn)狀態(tài)
public int state = START;
//靜態(tài)代碼塊
static{
start = img.loadImage("開始的圖片.png");
pause = img.loadImage("時間靜止之術(shù).png");
gameover = img.loadImage("沒電了.png");
}
private 背景 sky = new 背景();
private lex hero = new lex();
private duiXiangFuLeiFangFa[] duiXiangArray = {};
private 閃電[] bullets = {};
/**
* 隨機數(shù)產(chǎn)生目標(biāo)對象
* @return 目標(biāo)對象
*/
public duiXiangFuLeiFangFa nextOne() {
Random rand = new Random();
int type = rand.nextInt(20);
if (type < 15) {
return new 封茗囧茵();
} else if (type < 18) {
return new Warma();
} else {
return new 雙笙();
}
}
/** 實現(xiàn)敵人入場 */
int enterIndex = 0;
public void enterAction() {
enterIndex++;
if (enterIndex % 40 == 0) {
// 獲取到敵人:
duiXiangFuLeiFangFa f = nextOne();
// 敵人添加到數(shù)組的最后一位上:
duiXiangArray = Arrays.copyOf
(duiXiangArray, duiXiangArray.length + 1);
duiXiangArray[duiXiangArray.length - 1] = f;
}
}
/** 子彈入場 */
int shootIndex = 0;
public void shootAction() {
shootIndex++;
if (shootIndex % 40 == 0) {
閃電[] bs = hero.shoot();// 獲取子彈數(shù)組
bullets = Arrays.copyOf(bullets, bullets.length + bs.length);
// 將產(chǎn)生的子彈數(shù)組放到源數(shù)組中的最后一個元素
System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
bs.length);
}
}
// 對象移動方法:
public void duiXiangYiDongFangFa() {
sky.yiDongFangFa();
for (int i = 0; i < duiXiangArray.length; i++) {
duiXiangArray[i].yiDongFangFa();
}
for (int i = 0; i < bullets.length; i++) {
bullets[i].yiDongFangFa();
}
}
/**刪除移除越過頁面邊界的飛行物:*/
public void outOfBoundsAction(){
int index = 0;//存放不越界數(shù)組的下標(biāo),個數(shù)
//新建不越界的敵人數(shù)組
duiXiangFuLeiFangFa[] flysLive = new?
duiXiangFuLeiFangFa
[duiXiangArray.length];
for (int i = 0; i < duiXiangArray.length; i++) {
//獲取到每一個敵人
duiXiangFuLeiFangFa f = duiXiangArray[i];
//判斷敵人是否不越界
if (!f.outOfBounds()) {//如果不越界,
flysLive[index] = f;
index++;
}
//將不越界的敵人存放到不越界的數(shù)組中
}
duiXiangArray = Arrays.copyOf(flysLive, index);
//System.out.println("index == " + index);
index = 0;
閃電[] bulletLive = new 閃電[bullets.length];
for (int i = 0; i < bullets.length; i++) {
閃電 b = bullets[i];
if (!b.outOfBounds()) {
bulletLive[index] = b;
index++;
}
}
bullets = Arrays.copyOf(bulletLive, index);
}
/**閃電與敵人的碰撞*/
int score = 0;//玩家的得分記錄
public void bulletBangAction(){
for (int i = 0; i < bullets.length; i++) {
閃電 b = bullets[i];
//遍歷所有的敵人
for (int j = 0; j < duiXiangArray.length;?
j++) {
//獲取每一個敵人
duiXiangFuLeiFangFa f = duiXiangArray[j];
//判斷碰撞
if (f.panDuanLife() && b.panDuanLife()?
&& f.hit(b)) {
f.goDead();//敵人over
b.goDead();//子彈over
if (f instanceof diRen) {
//如果撞上的是敵人,能得分
diRen e = (diRen)f;
score += e.getScoreFangFa();
}
//如果撞上的是獎勵:
if (f instanceof jiangLi) {
jiangLi a = (jiangLi)f;
int type = a.getAwardType();
System.out.println(type);
switch (type) {
case 0://火力
hero.addDoubleFire();
break;
case 1://生命
hero.addLife();
break;
}
}
}
}
}
}
public void heroBangAction(){
for (int i = 0; i < duiXiangArray.length; i++) {
//獲取每一個敵人
duiXiangFuLeiFangFa f = duiXiangArray[i];
if (hero.panDuanLife() && f.panDuanLife()
&& f.hit(hero)) {
f.goDead();
new Thread(new shengyin( "hit.wav")).start() ;
hero.substractLife();//減少生命
hero.clearDoubleFire();//清空火力
}
}
}
/**檢測游戲是否結(jié)束*/
public void checkGameOverAction(){
//判斷LEX的電力值,
//如果小于0的話,游戲結(jié)束,修改狀態(tài)
if(hero.getLife() <= 0){
state = GAME_OVER;
}
}
/** 測試方法 */
public void action() {// 數(shù)組越界、數(shù)組空指針異常
//鼠標(biāo)適配器
MouseAdapter mouseAdapter = new MouseAdapter() {
/**重寫鼠標(biāo)的移動事件*/
public void mouseMoved(MouseEvent e){
int x = e.getX();
int y = e.getY();
hero.movedTo(x, y);
}
/**重寫鼠標(biāo)的點擊事件*/
@Override
public void mouseClicked(MouseEvent e) {
//根據(jù)當(dāng)前狀態(tài)的不同,進行相應(yīng)的處理
new Thread(new shengyin( "start.wav"))
.start() ;
switch (state) {
case START:
state = RUNNING;
break;
case GAME_OVER:
score = 0;
sky = new 背景();
hero = new lex();
duiXiangArray = new?
duiXiangFuLeiFangFa[0];
bullets = new 閃電[0];
state = START;//修改狀態(tài)為開始狀態(tài)
break;
}
}
/**重寫鼠標(biāo)的移入事件*/
@Override
public void mouseEntered(MouseEvent e) {
if (state == PAUSE) {
state = RUNNING;
}
}
/**重寫鼠標(biāo)的移出事件*/
@Override
public void mouseExited(MouseEvent e) {
if (state == RUNNING) {
state = PAUSE;
}
}
};
this.addMouseListener(mouseAdapter);
//處理鼠標(biāo)的操作事件
this.addMouseMotionListener(mouseAdapter);
//處理鼠標(biāo)的移動事件
// 定時器對象
Timer timer = new Timer();
int inters = 10;
timer.schedule(new TimerTask() {
@Override
public void run() {
if (state == RUNNING) {
enterAction();// 敵人入場
shootAction();// 子彈入場
duiXiangYiDongFangFa();// 飛行物移動
outOfBoundsAction();//刪除越界的飛行物
bulletBangAction();//閃電與敵人的碰撞
heroBangAction();//LEX與敵人發(fā)生碰撞
checkGameOverAction();//檢測游戲是都結(jié)束
}
repaint();// 重繪,調(diào)用paint方法
}
}, inters, inters);// 計劃任務(wù)
/* 7.30 20 第一次 10 第一次---第二次
*/
}
/** 畫對象 */
@Override
public void paint(Graphics g) {
super.paint(g);
// g.setColor(Color.blue);(這里沒效果)
// g.fill3DRect(0, 0, 10, 10,true);(這里沒效果)
sky.paintObjectDuiXiangFangFa(g);
hero.paintObjectDuiXiangFangFa(g);
// 畫敵人
for (int i = 0; i < duiXiangArray.length; i++) {
duiXiangArray[i].paintObjectDuiXiangFangFa(g);
}
for (int i = 0; i < bullets.length; i++) {
bullets[i].paintObjectDuiXiangFangFa(g);
}
g.setColor(Color.red);
g.setFont(new Font("微軟雅黑",Font.BOLD,20));
g.drawString("分?jǐn)?shù):" + score, 20, 60);
g.setColor(Color.blue);
g.setFont(new Font("楷體",Font.BOLD,20));
g.drawString("電力值:" + hero.getLife()+"千瓦",
20, 80);
switch (state) {//根據(jù)不同狀態(tài)畫不同的狀態(tài)圖
case START://開始狀態(tài)的啟動圖
g.drawImage(start, 0, 0, null);
break;
case PAUSE://暫停狀態(tài)的圖
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER://游戲結(jié)束的時候狀態(tài)圖
g.drawImage(gameover, 0, 0, null);
break;
}
}
/**在 主函數(shù)里實例化這個文件的本身的mains類,
* 之后實例化JFrame類,把mains類添加到JFrame類里,
* 再去設(shè)置JFrame的大小等。
* 也可用mains類繼承JFrame類的方法*/
public static void main(String[] args) {
mains mainsClass = new mains();
JFrame JFrame = new JFrame();
JFrame.add(mainsClass);// 將畫布裝在窗體上
// JFrame.setSize(quanJvWidth, quanJvHeight);
// (也可以這樣寫,有效果)
// JFrame.setBackground(Color.blue);(這里沒效果)
JFrame.setSize(400,720);// 窗體的大小
JFrame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
JFrame.setLocationRelativeTo(null);// 居中顯示
JFrame.setVisible(true);
mainsClass.action();
}
}


