㈠ 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;
}
}
}