游戲名:坦克大戰(zhàn)

在霸州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),霸州網(wǎng)站建設(shè)費(fèi)用合理。
設(shè)計(jì)思想:我軍坦克、敵軍坦克、炮彈、墻、基地
源代碼:class Tank{} class Wall{} class Base{} class Shell{}
運(yùn)行界面:
相應(yīng)解釋:對(duì)不起!您提供的分?jǐn)?shù)有限,暫時(shí)只能提供給您這么多回復(fù)
Mypanel的? run方法里要調(diào)用repaint方法??? 否則你的repaint方法只會(huì)在keyPressed發(fā)生的時(shí)候才調(diào)用
修改一下兩個(gè)地方
(1)
// 鍵盤(pán)獲取事件的函數(shù)
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_J) {
?if (hero.shot.size() 5) {
??? ?hero.shott();
?}
}
if (arg0.getKeyCode() == KeyEvent.VK_W) {
?hero.setSDC(hero.getSpeed(), 0, hero.getColor());
?hero.moveUp();
} else if (arg0.getKeyCode() == KeyEvent.VK_S) {
?hero.setSDC(hero.getSpeed(), 1, hero.getColor());
?hero.moveDown();
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
?hero.setSDC(hero.getSpeed(), 2, hero.getColor());
?hero.moveLeft();
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {
?hero.setSDC(hero.getSpeed(), 3, hero.getColor());
?hero.moveRight();
}
/**
* 這個(gè)repaint注釋掉
*/
//this.repaint();
}
(2)
// 線程
/**
* 一秒鐘60幀
*/
public void run() {
// TODO Auto-generated method stub
while(true){
?this.repaint();
?try {
??? ?
??? ?Thread.sleep(1000 / 60);
?} catch (InterruptedException e) {
??? ?// TODO 自動(dòng)生成的 catch 塊
??? ?e.printStackTrace();
?}
}
}
完整代碼如下:
import?java.awt.*;
import?javax.swing.*;
import?java.util.*;
import?java.awt.event.*;
public?class?aaa?extends?JFrame?{
public?static?void?main(String[]?args)?{
aaa?a1?=?new?aaa();
Thread?t1?=?new?Thread(a1.mp);
t1.start();
}
MyPanel?mp?=?null;
public?aaa()?{
mp?=?new?MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500,?500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class?MyPanel?extends?JPanel?implements?KeyListener,?Runnable?{
MyTank?hero?=?null;
VectorEmenyTank?emeny?=?new?VectorEmenyTank();
int?emsize?=?5;
//?鍵盤(pán)獲取事件的函數(shù)
public?void?keyPressed(KeyEvent?arg0)?{
//?TODO?Auto-generated?method?stub
if?(arg0.getKeyCode()?==?KeyEvent.VK_J)?{
if?(hero.shot.size()??5)?{
hero.shott();
}
}
if?(arg0.getKeyCode()?==?KeyEvent.VK_W)?{
hero.setSDC(hero.getSpeed(),?0,?hero.getColor());
hero.moveUp();
}?else?if?(arg0.getKeyCode()?==?KeyEvent.VK_S)?{
hero.setSDC(hero.getSpeed(),?1,?hero.getColor());
hero.moveDown();
}?else?if?(arg0.getKeyCode()?==?KeyEvent.VK_A)?{
hero.setSDC(hero.getSpeed(),?2,?hero.getColor());
hero.moveLeft();
}?else?if?(arg0.getKeyCode()?==?KeyEvent.VK_D)?{
hero.setSDC(hero.getSpeed(),?3,?hero.getColor());
hero.moveRight();
}
/**
*?這個(gè)repaint注釋掉
*/
//this.repaint();
}
public?void?keyReleased(KeyEvent?arg0)?{
//?TODO?Auto-generated?method?stub
}
public?void?keyTyped(KeyEvent?arg0)?{
//?TODO?Auto-generated?method?stub
}
//?完畢
public?MyPanel()?{
hero?=?new?MyTank(250,?250);
hero.setSDC(5,?2,?2);
for?(int?i?=?0;?i??emsize;?++i)?{
EmenyTank?em?=?new?EmenyTank((i?+?1)?*?60,?20);
em.setSDC(5,?1,?1);
emeny.add(em);
}
}
//?線程
/**
*?一秒鐘60幀
*/
public?void?run()?{
//?TODO?Auto-generated?method?stub
while(true){
this.repaint();
try?{
Thread.sleep(1000?/?60);
}?catch?(InterruptedException?e)?{
//?TODO?自動(dòng)生成的?catch?塊
e.printStackTrace();
}
}
}
public?void?paint(Graphics?g)?{
super.paint(g);
//?畫(huà)板,坦克得放在畫(huà)板后頭
g.fillRect(0,?0,?400,?400);
//?paint敵人坦克
for?(int?i?=?0;?i??emeny.size();?++i)?{
EmenyTank?em?=?null;
em?=?emeny.get(i);
this.drawTank(em.getX(),?em.getY(),?g,?em.getDirect(),
em.getColor());
}
//?畫(huà)我自己的坦克
this.drawTank(hero.getX(),?hero.getY(),?g,?hero.getDirect(),
hero.getColor());
//?畫(huà)出我的子彈
for?(int?i?=?0;?i??hero.shot.size();?i++)?{
Shot?myShot?=?hero.shot.get(i);
if?(myShot?!=?null??myShot.live?==?true)?{
g.draw3DRect(myShot.x,?myShot.y,?2,?2,?false);
}
if?(myShot.live?==?false)?{
hero.shot.remove(myShot);
}
}
}
public?void?drawTank(int?x,?int?y,?Graphics?g,?int?direct,?int?color)?{
//?判斷坦克的顏色(敵我)然后畫(huà)出坦克
switch?(color)?{
case?0:
g.setColor(Color.BLUE);
break;
case?1:
g.setColor(Color.YELLOW);
break;
case?2:
g.setColor(Color.GREEN);
break;
}
//?判斷坦克的方向然后再畫(huà)出坦克
switch?(direct)?{
case?0:
g.fill3DRect(x,?y,?10,?30,?false);
g.fill3DRect(x?+?26,?y,?10,?30,?false);
g.fill3DRect(x?+?10,?y?+?5,?16,?20,?false);
g.drawLine(x?+?18,?y?+?15,?x?+?18,?y);
break;
case?1:
g.fill3DRect(x,?y,?10,?30,?false);
g.fill3DRect(x?+?26,?y,?10,?30,?false);
g.fill3DRect(x?+?10,?y?+?5,?16,?20,?false);
g.drawLine(x?+?18,?y?+?15,?x?+?18,?y?+?30);
break;
case?2:
g.fill3DRect(x?+?3,?y?-?3,?30,?10,?false);
g.fill3DRect(x?+?3,?y?+?23,?30,?10,?false);
g.fill3DRect(x?+?8,?y?+?7,?20,?16,?false);
g.drawLine(x?+?18,?y?+?15,?x?+?3,?y?+?15);
break;
case?3:
g.fill3DRect(x?+?3,?y?-?3,?30,?10,?false);
g.fill3DRect(x?+?3,?y?+?23,?30,?10,?false);
g.fill3DRect(x?+?8,?y?+?7,?20,?16,?false);
g.drawLine(x?+?18,?y?+?15,?x?+?33,?y?+?15);
break;
}
}
}
class?EmenyTank?extends?Tank?implements?Runnable?{
public?EmenyTank(int?x,?int?y)?{
//?TODO?Auto-generated?method?stub
super(x,?y);
}
public?void?run()?{
}
}
class?Shot?implements?Runnable?{
protected?int?x;
protected?int?y;
protected?int?direct;
protected?int?speed?=?4;
protected?boolean?live?=?true;
public?void?setX(int?x)?{
this.x?=?x;
this.y?=?y;
}
public?int?getX()?{
return?x;
}
public?int?getY()?{
return?y;
}
public?void?setDirect(int?direct)?{
this.direct?=?direct;
}
public?int?getDirect()?{
return?direct;
}
public?void?setSpeed(int?speed)?{
this.speed?=?speed;
}
public?int?getSpeed()?{
return?speed;
}
//?子彈的上下左右以及走的速度
public?void?run()?{
//?TODO?Auto-generated?method?stub
while?(true)?{
try?{
Thread.sleep(100);
}?catch?(Exception?e)?{
}
switch?(direct)?{
case?0:
y?-=?speed;
break;
case?1:
y?+=?speed;
break;
case?2:
x?-=?speed;
break;
case?3:
x?+=?speed;
break;
}
if?(x??400?||?x??0?||?y??400?||?y??0)?{
this.live?=?false;
break;
}
}
}
}
class?Tank?{
protected?int?x;
protected?int?y;
protected?int?speed?=?5;
protected?int?direct;
protected?int?color;
boolean?live;
public?Tank(int?x,?int?y)?{
this.x?=?x;
this.y?=?y;
}
public?int?getX()?{
return?x;
}
public?int?getY()?{
return?y;
}
public?void?setSDC(int?speed,?int?direct,?int?color)?{
this.speed?=?speed;
this.direct?=?direct;
this.color?=?color;
}
public?int?getSpeed()?{
return?speed;
}
public?int?getDirect()?{
return?direct;
}
public?int?getColor()?{
return?color;
}
}
class?MyTank?extends?Tank?{
public?MyTank(int?x,?int?y)?{
//?TODO?Auto-generated?method?stub
super(x,?y);
}
VectorShot?shot?=?new?VectorShot();
Shot?shota?=?null;
public?void?shott()?{
switch?(this.direct)?{
case?0:
shota?=?new?Shot();
shota.x?=?x?+?18;
shota.y?=?y;
shota.direct?=?0;
shot.add(shota);
break;
case?1:
shota?=?new?Shot();
shota.x?=?x?+?18;
shota.y?=?y?+?30;
shota.direct?=?1;
shot.add(shota);
break;
case?2:
shota?=?new?Shot();
shota.x?=?x?+?3;
shota.y?=?y?+?15;
shota.direct?=?2;
shot.add(shota);
break;
case?3:
shota?=?new?Shot();
shota.x?=?x?+?33;
shota.y?=?y?+?15;
shota.direct?=?3;
shot.add(shota);
break;
}
Thread?t?=?new?Thread(shota);
t.start();
}
public?void?moveUp()?{
if?(y??0)?{
y?-=?speed;
}
}//?我的坦克得在自己的類里定義怎么移動(dòng)
public?void?moveDown()?{
if?(y??367)?{
y?+=?speed;
}
}
public?void?moveLeft()?{
if?(x??0)?{
x?-=?speed;
}
}
public?void?moveRight()?{
if?(x??365)?{
x?+=?speed;
}
}
}
我就想告訴樓主,提問(wèn)的時(shí)候把代碼貼貼好。這一行代碼叫人怎么看
坦克大戰(zhàn)源代碼應(yīng)該是個(gè)完整的項(xiàng)目吧。
對(duì)于完整的帶項(xiàng)目配置文件的java源碼,按步驟操作即可:
File - Import - General
選擇Existing Projects into Workspace,選擇要導(dǎo)入的文件,點(diǎn)擊“finish",OK。
java 作圖 不一定要繼承JFrame或者Frame,但是所有的組件必須放到頂層容器,Java Swing 有三種頂層容器
JFrame:用來(lái)設(shè)計(jì)類似于Windows系統(tǒng)中的窗口形式的應(yīng)用程序。
·JDialog:和JFrame類似,只不過(guò)JDialog是用來(lái)設(shè)計(jì)對(duì)話框。
·JApplet:用來(lái)設(shè)計(jì)可以在嵌入在網(wǎng)頁(yè)中的Java小程序。
而且你的代碼 Kosese001既然繼承了JFrame 那么他的構(gòu)造就是個(gè)JFrame ,你下面又new JFrame("坦克大戰(zhàn)");這樣何必繼承,直接在構(gòu)造里面實(shí)現(xiàn)操作就可以把Kosese001中,
你的背景色沒(méi)有顯示出來(lái),是由于JFrame 是頂層容器,它默認(rèn)的布局會(huì)設(shè)置它的背景色,除非你在前面 加上 jfrmMain.setLayout(null);使得布局變成空布局,下面的jfrmMain.getContentPane().setBackground(Color.green); 才會(huì)生效,或者你可以通過(guò)設(shè)置JFrame上組件的背景色來(lái)完成同樣的效果 比如 mypaint = new MyPaint();
mypaint.setBackground(Color.green); 還有啥不懂的 可以問(wèn)我。
import?java.awt.*;
import?javax.swing.*;
public?class?Tank?extends?JFrame?{
mypane?mp=null;
Obj[]?objs=new?Obj[0];
public?Tank()?{
setTitle("坦克大戰(zhàn)");
setSize(800,600);
pro();
add(new?mypane(objs));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//在這里添加鍵盤(pán)事件、鼠標(biāo)事件、讓坦克移動(dòng),修改objs數(shù)組對(duì)象讓他們移動(dòng)
setVisible(true);
}
private?void?pro(){
Obj[]?tmp=new?Obj[objs.length+1];
System.arraycopy(objs,0,tmp,0,objs.length);
tmp[tmp.length-1]=new?Obj(1,1,0,1);
objs=tmp;
int?num=(int)(Math.random()*5)+1;
for(int?i=0;inum;i++){
int?x=(int)(Math.random()*getWidth())+1;
int?y=(int)(Math.random()*getHeight())+1;
int?dir=(int)(Math.random()*4);
Obj[]?dst=new?Obj[objs.length+1];
System.arraycopy(objs,0,dst,0,objs.length);
dst[dst.length-1]=new?Obj(x,y,1,dir);
objs=dst;
}
}
public?static?void?main(String[]?args)?{
new?Tank();
}
}
class?Obj{
int?x,y;//坦克坐標(biāo)
int?type;
int?dir;
public?Obj(int?x,int?y,int?type,int?dir){
this.x=x;
this.y=y;
this.type=type;
this.dir=dir;
}
}
class?mypane?extends?JPanel{
Obj[]?objs;
public?mypane(Obj[]?objs){
this.objs=objs;
}
public?void?paint(Graphics?g)?{
super.paint(g);
for(int?i=0;iobjs.length;i++){
Obj?obj=objs[i];
drawtank(obj.x,obj.y,?g,?obj.type,?obj.dir);
}
g.dispose();
}
public?void?drawtank(int?x,int?y,Graphics?g,?int?type,int?direct)?{
/*type?為坦克類型,敵方,我方*/
switch(type)?{
case?0://我方坦克,設(shè)置為紅色
g.setColor(Color.red);
break;
case?1://敵方坦克,設(shè)置為藍(lán)色
g.setColor(Color.blue);
break;
}
switch(direct)?{
case?0://坦克方向朝上
g.drawRect(0+x,?0+y,?5,?30);
g.drawRect(5+x,?5+y,?10,20);
g.drawRect(15+x,0+y,?5,30);
g.drawLine(10+x,?15+y,?10+10+x,?15+y);
break;
case?1://坦克方向朝右
g.drawRect(0+x,?0+y,?30,?5);
g.drawRect(5+x,?5+y,?20,?10);
g.drawRect(0+x,?15+y,?30,?5);
g.drawLine(15+x,?10+y,?30+15+x,?10+10+y);
break;
case?2://方向向下
g.drawRect(0+x,?0+y,?5,?30);
g.drawRect(5+x,?5+y,?10,20);
g.drawRect(15+x,0+y,?5,30);
g.drawLine(10+x,?15+y,?10+10+x,?30+15+y);
break;
case?3://方向向左
g.drawRect(0+x,?0+y,?30,?5);
g.drawRect(5+x,?5+y,?20,?10);
g.drawRect(0+x,?15+y,?30,?5);
g.drawLine(15+x,?10+y,?15+x,?10+10+y);
break;
}
}
}
當(dāng)前名稱:java坦克大戰(zhàn)源代碼的簡(jiǎn)單介紹
轉(zhuǎn)載來(lái)源:http://chinadenli.net/article35/dsspgpi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)