㈠ java坦克大戰
修改你的paint方法
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GREEN);
g.fillRect(0, 0, GAME_WIGTH, GAME_HEIGTH);
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
x += 5;
}
㈡ java 坦克大戰 一段代碼 關於圖像繪制 不懂 求解
這個是雙緩沖的繪制方法
if (offScreenImage == null)
{
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
這里是先在內存中創建一個 offScreenImage 的緩沖圖像
gOffScreen.drawImage(imgs, 0, 0, GAME_WIDTH, GAME_HEIGHT,null);
這里是將背景圖片繪制到剛才創建的這塊緩沖區上
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
這里是將緩沖區再繪制到實際屏幕上
採用雙緩沖可以保證畫面不會閃爍 因為圖片的刷新都是發生在內存區上的 實際屏幕上用戶是感覺不出來的 所以人眼感覺不到閃爍
㈢ java版本坦克大戰源代碼
給你一個猜數字游戲代碼。辛辛苦苦打的,希望採納。謝謝。 package caishuzi.java; import javax.swing.JOptionPane; public class caishuzi { public static void main (String args[ ]) { JOptionPane.showMessageDialog(null,"給你一個1至100...
㈣ JAVA坦克大戰,這段代碼為什麼子彈的坐標在變,卻不能repaint,但是按下任意鍵盤的建卻重繪了呢
Mypanel的 run方法里要調用repaint方法 否則你的repaint方法只會在keyPressed發生的時候才調用
修改一下兩個地方
(1)
// 鍵盤獲取事件的函數
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();
}
/**
* 這個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 自動生成的 catch 塊
e.printStackTrace();
}
}
}
完整代碼如下:
importjava.awt.*;
importjavax.swing.*;
importjava.util.*;
importjava.awt.event.*;
publicclassaaaextendsJFrame{
publicstaticvoidmain(String[]args){
aaaa1=newaaa();
Threadt1=newThread(a1.mp);
t1.start();
}
MyPanelmp=null;
publicaaa(){
mp=newMyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
,Runnable{
MyTankhero=null;
Vector<EmenyTank>emeny=newVector<EmenyTank>();
intemsize=5;
//鍵盤獲取事件的函數
publicvoidkeyPressed(KeyEventarg0){
//TODOAuto-generatedmethodstub
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();
}elseif(arg0.getKeyCode()==KeyEvent.VK_S){
hero.setSDC(hero.getSpeed(),1,hero.getColor());
hero.moveDown();
}elseif(arg0.getKeyCode()==KeyEvent.VK_A){
hero.setSDC(hero.getSpeed(),2,hero.getColor());
hero.moveLeft();
}elseif(arg0.getKeyCode()==KeyEvent.VK_D){
hero.setSDC(hero.getSpeed(),3,hero.getColor());
hero.moveRight();
}
/**
*這個repaint注釋掉
*/
//this.repaint();
}
publicvoidkeyReleased(KeyEventarg0){
//TODOAuto-generatedmethodstub
}
publicvoidkeyTyped(KeyEventarg0){
//TODOAuto-generatedmethodstub
}
//完畢
publicMyPanel(){
hero=newMyTank(250,250);
hero.setSDC(5,2,2);
for(inti=0;i<emsize;++i){
EmenyTankem=newEmenyTank((i+1)*60,20);
em.setSDC(5,1,1);
emeny.add(em);
}
}
//線程
/**
*一秒鍾60幀
*/
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
this.repaint();
try{
Thread.sleep(1000/60);
}catch(InterruptedExceptione){
//TODO自動生成的catch塊
e.printStackTrace();
}
}
}
publicvoidpaint(Graphicsg){
super.paint(g);
//畫板,坦克得放在畫板後頭
g.fillRect(0,0,400,400);
//paint敵人坦克
for(inti=0;i<emeny.size();++i){
EmenyTankem=null;
em=emeny.get(i);
this.drawTank(em.getX(),em.getY(),g,em.getDirect(),
em.getColor());
}
//畫我自己的坦克
this.drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),
hero.getColor());
//畫出我的子彈
for(inti=0;i<hero.shot.size();i++){
ShotmyShot=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);
}
}
}
publicvoiddrawTank(intx,inty,Graphicsg,intdirect,intcolor){
//判斷坦克的顏色(敵我)然後畫出坦克
switch(color){
case0:
g.setColor(Color.BLUE);
break;
case1:
g.setColor(Color.YELLOW);
break;
case2:
g.setColor(Color.GREEN);
break;
}
//判斷坦克的方向然後再畫出坦克
switch(direct){
case0:
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;
case1:
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;
case2:
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;
case3:
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;
}
}
}
{
publicEmenyTank(intx,inty){
//TODOAuto-generatedmethodstub
super(x,y);
}
publicvoidrun(){
}
}
classShotimplementsRunnable{
protectedintx;
protectedinty;
protectedintdirect;
protectedintspeed=4;
protectedbooleanlive=true;
publicvoidsetX(intx){
this.x=x;
this.y=y;
}
publicintgetX(){
returnx;
}
publicintgetY(){
returny;
}
publicvoidsetDirect(intdirect){
this.direct=direct;
}
publicintgetDirect(){
returndirect;
}
publicvoidsetSpeed(intspeed){
this.speed=speed;
}
publicintgetSpeed(){
returnspeed;
}
//子彈的上下左右以及走的速度
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
try{
Thread.sleep(100);
}catch(Exceptione){
}
switch(direct){
case0:
y-=speed;
break;
case1:
y+=speed;
break;
case2:
x-=speed;
break;
case3:
x+=speed;
break;
}
if(x>400||x<0||y>400||y<0){
this.live=false;
break;
}
}
}
}
classTank{
protectedintx;
protectedinty;
protectedintspeed=5;
protectedintdirect;
protectedintcolor;
booleanlive;
publicTank(intx,inty){
this.x=x;
this.y=y;
}
publicintgetX(){
returnx;
}
publicintgetY(){
returny;
}
publicvoidsetSDC(intspeed,intdirect,intcolor){
this.speed=speed;
this.direct=direct;
this.color=color;
}
publicintgetSpeed(){
returnspeed;
}
publicintgetDirect(){
returndirect;
}
publicintgetColor(){
returncolor;
}
}
classMyTankextendsTank{
publicMyTank(intx,inty){
//TODOAuto-generatedmethodstub
super(x,y);
}
Vector<Shot>shot=newVector<Shot>();
Shotshota=null;
publicvoidshott(){
switch(this.direct){
case0:
shota=newShot();
shota.x=x+18;
shota.y=y;
shota.direct=0;
shot.add(shota);
break;
case1:
shota=newShot();
shota.x=x+18;
shota.y=y+30;
shota.direct=1;
shot.add(shota);
break;
case2:
shota=newShot();
shota.x=x+3;
shota.y=y+15;
shota.direct=2;
shot.add(shota);
break;
case3:
shota=newShot();
shota.x=x+33;
shota.y=y+15;
shota.direct=3;
shot.add(shota);
break;
}
Threadt=newThread(shota);
t.start();
}
publicvoidmoveUp(){
if(y>0){
y-=speed;
}
}//我的坦克得在自己的類里定義怎麼移動
publicvoidmoveDown(){
if(y<367){
y+=speed;
}
}
publicvoidmoveLeft(){
if(x>0){
x-=speed;
}
}
publicvoidmoveRight(){
if(x<365){
x+=speed;
}
}
}
㈤ 關於java坦克大戰里的一個小問題,這是其中的一部分代碼
this.barrelDir = this.dir;
假設坦克向右行駛,這時——
this.barrelDir = this.dir = 「R」
它們的值都指向了R。
當坦克停止時,校驗
if(this.dir != Direction.STOP)
無法通過,所以不會重置 barrelDir 的值,也就是說它仍然等於R。
這時你發射子彈,barrelDir 值不為STOP,所以它就可以往右邊飛出去了。
㈥ java 編寫坦克大戰
需要看你的tank的draw方法里的內容代碼和碰撞檢測代碼。
一般二維碰撞檢測基本都是以單位格,也就是坦克的大小格作為單位,提前一格判斷兩個方格是否交叉,如果交叉則在當前,也就是交叉後退後一格這個位置讓它停止當前方向的移動。
㈦ JAVA 坦克大戰
importjava.awt.*;
importjavax.swing.*;
publicclassTankextendsJFrame{
mypanemp=null;
Obj[]objs=newObj[0];
publicTank(){
setTitle("坦克大戰");
setSize(800,600);
pro();
add(newmypane(objs));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//在這里添加鍵盤事件、滑鼠事件、讓坦克移動,修改objs數組對象讓他們移動
setVisible(true);
}
privatevoidpro(){
Obj[]tmp=newObj[objs.length+1];
System.array(objs,0,tmp,0,objs.length);
tmp[tmp.length-1]=newObj(1,1,0,1);
objs=tmp;
intnum=(int)(Math.random()*5)+1;
for(inti=0;i<num;i++){
intx=(int)(Math.random()*getWidth())+1;
inty=(int)(Math.random()*getHeight())+1;
intdir=(int)(Math.random()*4);
Obj[]dst=newObj[objs.length+1];
System.array(objs,0,dst,0,objs.length);
dst[dst.length-1]=newObj(x,y,1,dir);
objs=dst;
}
}
publicstaticvoidmain(String[]args){
newTank();
}
}
classObj{
intx,y;//坦克坐標
inttype;
intdir;
publicObj(intx,inty,inttype,intdir){
this.x=x;
this.y=y;
this.type=type;
this.dir=dir;
}
}
classmypaneextendsJPanel{
Obj[]objs;
publicmypane(Obj[]objs){
this.objs=objs;
}
publicvoidpaint(Graphicsg){
super.paint(g);
for(inti=0;i<objs.length;i++){
Objobj=objs[i];
drawtank(obj.x,obj.y,g,obj.type,obj.dir);
}
g.dispose();
}
publicvoiddrawtank(intx,inty,Graphicsg,inttype,intdirect){
/*type為坦克類型,敵方,我方*/
switch(type){
case0://我方坦克,設置為紅色
g.setColor(Color.red);
break;
case1://敵方坦克,設置為藍色
g.setColor(Color.blue);
break;
}
switch(direct){
case0://坦克方向朝上
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;
case1://坦克方向朝右
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;
case2://方向向下
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;
case3://方向向左
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;
}
}
}