原因是我按了开子弹键后 ,子弹黏在炮筒上 不是我想要的停在那里不动 我是新手 刚刚学不到一个月package MyTank.copy;
/**
* 功能:坦克大战 前面部分
* @author Administrator
*
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.Event;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyTank1 extends JFrame implements KeyListener{ MyPanel mp=null;
public static void main(String[] args) {
MyTank1 mt=new MyTank1();
}
public MyTank1()
{
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class MyPanel extends JPanel implements KeyListener
{
Hero hero=null;
Vector<EnemyTank> ets=new Vector<EnemyTank>();
int ensize=3;
public MyPanel()
{
hero=new Hero(100,100);
for(int i=0;i<ensize;i++)
{
EnemyTank et=new EnemyTank((i+1)*50,0);
et.setColor(0);
ets.add(et);
et.setDirect(2);
}
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(3);
this.hero.moveLeft();
}
if(e.getKeyCode()==KeyEvent.VK_J);
{
this.hero.ShotEnemy();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
//画出坦克
this.drawTank(hero.getX(), hero.getY(), g,this.hero.direct , 1);
if(hero.s!=null)
{
g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false);
}
for(int i=0;i<ets.size();i++)
{
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);
}
}
public void drawTank(int x,int y,Graphics g,int direct,int type) {
{
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch(direct)
{
case 0:
//上
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fill3DRect(x+15, y, 5, 30, false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15, x+10, y);
break;
case 1:
//右
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x+30, y+10);
break;
case 2:
//下
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fill3DRect(x+15, y,5, 30, false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15, x+10, y+30);
break;
case 3:
// 左
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x, y+10);
break;
}
}
}
}package MyTank.copy;
class Shot
{
int x;
int y;
int direct;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
}
class Tank
{
//表示横坐标
int x=0;
int y=0;
int direct=0;
int speed=1;
public int getColor() {
return color;
} public void setColor(int color) {
this.color = color;
} int color;
public int getSpeed() {
return speed;
} public void setSpeed(int speed) {
this.speed = speed;
} 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 EnemyTank extends Tank
{ public EnemyTank(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
}
class Hero extends Tank
{
//子弹
Shot s=null;
public Hero(int x, int y)
{
super(x,y);
}
//开火
public void ShotEnemy()
{
switch(this.direct)
{
case 0:
s=new Shot(x+10,y,0);
break;
case 1:
s=new Shot(x+30,y+10,1);
break;
case 2:
s=new Shot(x+10,y+30,2);
break;
case 3:
s=new Shot(x,y+10,3);
break;
}
}
public void moveUp()
{
y-=speed+2;
}
public void moveDown()
{
y+=speed+2;
}
public void moveLeft()
{
x-=speed+2;
}
public void moveRight()
{
x+=speed+2;
}
}
/**
* 功能:坦克大战 前面部分
* @author Administrator
*
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.Event;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyTank1 extends JFrame implements KeyListener{ MyPanel mp=null;
public static void main(String[] args) {
MyTank1 mt=new MyTank1();
}
public MyTank1()
{
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class MyPanel extends JPanel implements KeyListener
{
Hero hero=null;
Vector<EnemyTank> ets=new Vector<EnemyTank>();
int ensize=3;
public MyPanel()
{
hero=new Hero(100,100);
for(int i=0;i<ensize;i++)
{
EnemyTank et=new EnemyTank((i+1)*50,0);
et.setColor(0);
ets.add(et);
et.setDirect(2);
}
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(3);
this.hero.moveLeft();
}
if(e.getKeyCode()==KeyEvent.VK_J);
{
this.hero.ShotEnemy();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
//画出坦克
this.drawTank(hero.getX(), hero.getY(), g,this.hero.direct , 1);
if(hero.s!=null)
{
g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false);
}
for(int i=0;i<ets.size();i++)
{
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);
}
}
public void drawTank(int x,int y,Graphics g,int direct,int type) {
{
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch(direct)
{
case 0:
//上
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fill3DRect(x+15, y, 5, 30, false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15, x+10, y);
break;
case 1:
//右
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x+30, y+10);
break;
case 2:
//下
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fill3DRect(x+15, y,5, 30, false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+15, x+10, y+30);
break;
case 3:
// 左
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x+5, y+5, 20, 10, false);
g.fill3DRect(x, y+15, 30, 5, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x, y+10);
break;
}
}
}
}package MyTank.copy;
class Shot
{
int x;
int y;
int direct;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
}
class Tank
{
//表示横坐标
int x=0;
int y=0;
int direct=0;
int speed=1;
public int getColor() {
return color;
} public void setColor(int color) {
this.color = color;
} int color;
public int getSpeed() {
return speed;
} public void setSpeed(int speed) {
this.speed = speed;
} 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 EnemyTank extends Tank
{ public EnemyTank(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
}
class Hero extends Tank
{
//子弹
Shot s=null;
public Hero(int x, int y)
{
super(x,y);
}
//开火
public void ShotEnemy()
{
switch(this.direct)
{
case 0:
s=new Shot(x+10,y,0);
break;
case 1:
s=new Shot(x+30,y+10,1);
break;
case 2:
s=new Shot(x+10,y+30,2);
break;
case 3:
s=new Shot(x,y+10,3);
break;
}
}
public void moveUp()
{
y-=speed+2;
}
public void moveDown()
{
y+=speed+2;
}
public void moveLeft()
{
x-=speed+2;
}
public void moveRight()
{
x+=speed+2;
}
}
解决方案 »
- 急求:怎样解决JSplitPane与FlowLayout冲突?
- 一个改变字体大小的applet的程序
- 初学者-哪位高手能给我解答一下一个连接SQL server2000的问题?急!
- 大家怎么阅读代码,主要运用什么辅助工具?
- 一个简单的对象申明和定义的问题
- 高手指点:异步输入输出流的实现问题(关于socket,selector....)
- 请教vj or jbuilder 还是其它?
- 请问高手们,怎样把int 转换成String 型
- 在java应用程序中,可不可以等待某个线程执行完后才执行后面的代码
- java 文本信息处理(向一对标签中插入文字内容,或读出文本内容)
- Java 怎么生成双击可以直接运行的程序啊
- JAVA 做坦克大战(请求指导)
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(3);
this.hero.moveLeft();
}
if(e.getKeyCode()==KeyEvent.VK_J);
{
this.hero.ShotEnemy();
}
this.repaint();
}
多了个分号,这样写你按什么键都是发射还有,子弹最好别和坦克关联上,自己开个线程自己跑,挂坦克上,你开一次枪,就一直存在这个子弹了的引用了。