java代码画出tank java代码画图

java程序 。我的问题就在最后一个类中做了个简单测试,把Hero类删了 。
另外改动了以下部分:
//定义一个我的坦克
// Hero hero=null;
Tank hero = null;
//构造函数
public MyPanel()
{
hero = new Tank(100,100);
//hero=new Hero(100,100);
}
正常显示界面 。我的看法是,Hero不是必须的,可写可不写,不写的话,在class MyPanel 类及其构造方法 public MyPanel()中就不写Hero hero , 而是直接创建
//坦克类
class Tank
的对象即可 。理论上 , Hero和Tank是子类和父类的关系,你在这段代码中,有没有对Hero做什么新的功能补充,因此,可以直接用Tank类 。
补充:定义子类Hero是可以的,而且有时候是应该的,就是当你对Tank类里面的方法、属性觉得不够用,那就在Hero中定义新的属性和方法 , 作为对Tank的补充 。第二,创建子类有区别于父类Tank的效果,如果Hero类不是你自己定义的,那就是定义这个类的人打算通过Hero类来对Tank进行补充,还有就是定义特殊坦克:如普通坦克用Tank类创建,大型坦克或超级坦克则用Hero创建,此时,Hero中会添加更多代码--更多的属性和方法 。
这坦克可以上下左右移动!但是不能转向!他的炮筒一直是朝上的! 说的详细的加分!代码得打出来package Tank;
/*
* 坦克 类
*/
//导入相关的包
import java.awt.* ;
import java.awt.event.* ;
import java.util.List ;
import java.util.* ;
public class Tank
{
public static final int WIDTH = 30 ;//坦克的宽度
public static final int HEIGHT = 30 ;//坦克的高度
public static Random r = new Random();//随机数生成器
int temp = r.nextInt(12)3 ;//存放敌军坦克移动的步数
int x , y ;//坦克在窗口中出现的位置
int oldX , oldY ;//用来存放坦克移动前的坐标,也就是坦克的上一步的位置
int speed = 5 ;//坦克的移动速度
private boolean good ;//该辆坦克的好坏,true为我方坦克,false为敌方坦克
private boolean live = true ;//坦克是否活着
private int life = 100 ;//坦克的生命值
private BloodBar bb = new BloodBar() ;//
//用来存放四个方向键是否被按下 , 默认情况下都是false
private boolean bL = false ;
private boolean bU = false ;
private boolean bR = false ;
private boolean bD = false ;
Direction tankDir = Direction.STOP ;//坦克的方向,默认是STOP
Direction ptDir = Direction.R ;//坦克炮筒的方向,默认是向右
public static ListMissile missiles = new ArrayListMissile();//用来存放屏幕上打出去的子弹
//坦克的构造方法
public Tank(int x , int y , boolean good)
{
//初始化坦克的位置 , 坦克的好坏
this.x = x ;
this.y = y ;
this.good = good ;
oldX = x ;
oldY = y ;
}
//画出这辆坦克的方法
public void draw(Graphics g)
{
if (!getLive())//如果坦克活着才画,坦克死了就不画了
{
return ;
}
if (good)
{
bb.draw(g);
}
Color c = g.getColor() ;//拿到默认画笔的颜色
//判断,如果是好坦克,将画笔颜色设置成蓝色,如果是坏坦克,将画笔颜色设置成灰色
if (good)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.GRAY);
}
g.fillOval(x, y, WIDTH, HEIGHT);//画实心圆 , 用来表示坦克
g.setColor(Color.BLACK);
//根据炮筒的方向 , 画出坦克的炮筒
if (tankDir != Direction.STOP)
{
ptDir = tankDir ;
}
if (ptDir == Direction.L)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, x, yHEIGHT/2);
}
else if (ptDir == Direction.LU)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, x, y);
}
else if (ptDir == Direction.U)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, xWIDTH/2, y);
}
else if (ptDir == Direction.RU)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, xWIDTH, y);
}
else if (ptDir == Direction.R)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, xWIDTH, yHEIGHT/2);
}
else if (ptDir == Direction.RD)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, xWIDTH, yHEIGHT);
}
else if (ptDir == Direction.D)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, xWIDTH/2, yHEIGHT);
}
else if (ptDir == Direction.LD)
{
g.drawLine(xWIDTH/2, yHEIGHT/2, x, yHEIGHT);
}
g.setColor(c);//将画笔的颜色设置回默认的画笔颜色
move();//每次画完坦克 , 都根据方向移动一下坦克
}
public void stay()
{
x = oldX ;
y = oldY ;
}
//坦克开火方法
public void fire()
{
if (!this.getLive())
{
return ;
}
Missile m ;
if (good)
{
m = new Missile(xWIDTH/2 - Missile.WIDTH/2 , yHEIGHT/2 - Missile.HEIGHT/2 , true , ptDir);
}
else
{
m = new Missile(xWIDTH/2 - Missile.WIDTH/2 , yHEIGHT/2 - Missile.HEIGHT/2 , false , ptDir);
}
missiles.add(m) ;
}
public void fire (Direction dir)
{
if (!this.getLive())
{
return ;
}
Missile m = new Missile(xWIDTH/2 - Missile.WIDTH/2 , yHEIGHT/2 - Missile.HEIGHT/2 , true , dir);
missiles.add(m) ;
}
public void superFire()
{
Direction[] dirs = Direction.values() ;
for (int x=0 ; xdirs.length - 1 ; x)
{
this.fire(dirs[x]);
}
}
//根据坦克的方向,移动坦克
public void move()
{
oldX = x ;
oldY = y ;
if (tankDir == Direction.L)
{
x = x - speed ;
}
else if (tankDir == Direction.LU)
{
x = x - speed ;
y = y - speed ;
}
else if (tankDir == Direction.U)
{
y = y - speed ;
}
else if (tankDir == Direction.RU)
{
x = xspeed ;
y = y - speed ;
}
else if (tankDir == Direction.R)
{
x = xspeed ;
}
else if (tankDir == Direction.RD)
{
x = xspeed ;
y = yspeed ;
}
else if (tankDir == Direction.D)
{
y = yspeed ;
}
else if (tankDir == Direction.LD)
{
x = x - speed ;
y = yspeed ;
}
//只要坦克出界,那么坦克就回到上一步
if (x0 || y30 || (x WIDTH)GameFrame.WIDTH || (y HEIGHT)GameFrame.HEIGHT)
{
stay();
}
//判断坦克碰撞
for (int x=0 ; xGameFrame.tanks.size() ; x)
{
Tank t = GameFrame.tanks.get(x) ;
if (this != t)
{
if (this.getRect().intersects(t.getRect()))
{
this.stay();
t.stay();
}
}
}
temp-- ;
if (!good)
{
Direction[] dirs = Direction.values() ;
if (temp == 0)
{
temp = r.nextInt(12)3 ;
this.tankDir = dirs[r.nextInt(dirs.length)] ;
}
if (r.nextInt(50)45)
{
this.fire();
}
}
}
//是否撞墙
public boolean hitWalls(Wall w)
{
for (int x=0 ; xGameFrame.tanks.size() ; x)
{
Tank t = GameFrame.tanks.get(x) ;
if (t.getRect().intersects(w.getRect()))
{
t.stay();
return true ;
}
}
return false ;
}
public boolean hitWall(Wall w)
{
if (this.getRect().intersects(w.getRect()))
{
this.stay();
return true ;
}
return false ;
}
public void eat(Blood b)
{
if (goodthis.getRect().intersects(b.getRect())b.getLive())
{
if (this.getLife() = 70this.getLife() 0)
{
this.setLife(this.getLife()30) ;
}
else
{
this.setLife(100);
}
b.setLive(false);
}
}
//根据方向键是否被按下,设置坦克的方向
public void direction()
{
if (!bL!bU!bR!bD)
{
tankDir = Direction.STOP ;
}
else if (bL!bU!bR!bD)
{
tankDir = Direction.L ;
}
else if (bLbU!bR!bD)
{
tankDir = Direction.LU ;
}
else if (!bLbU!bR!bD)
{
tankDir = Direction.U ;
}
else if (!bLbUbR!bD)
{
tankDir = Direction.RU ;
}
else if (!bL!bUbR!bD)
{
tankDir = Direction.R ;
}
else if (!bL!bUbRbD)
{
tankDir = Direction.RD ;
}
else if (!bL!bU!bRbD)
{
tankDir = Direction.D ;
}
else if (bL!bU!bRbD)
{
tankDir = Direction.LD ;
}
}
//按键按下时的处理
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode() ;
switch (key)
{
case KeyEvent.VK_LEFT :
bL = true ;
break ;
case KeyEvent.VK_UP :
bU = true ;
break ;
case KeyEvent.VK_RIGHT :
bR = true ;
break ;
case KeyEvent.VK_DOWN :
bD = true ;
break ;
}
direction();//设置坦克的方向
}
//按键抬起时的处理
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode() ;
switch (key)
{
case KeyEvent.VK_X :
if (!this.getLive())
{
this.setLive(true);
this.setLife(100);
}
break;
case KeyEvent.VK_C :
GameFrame.addTanks();
break ;
case KeyEvent.VK_Z :
superFire();
break ;
case KeyEvent.VK_CONTROL :
fire();
break ;
case KeyEvent.VK_LEFT :
bL = false ;
break ;
case KeyEvent.VK_UP :
bU = false ;
break ;
case KeyEvent.VK_RIGHT :
bR = false ;
break ;
case KeyEvent.VK_DOWN :
bD = false ;
break ;
}
direction();//设置坦克的方向
}
//用来碰撞检测的方法
public Rectangle getRect()
{
return new Rectangle(x , y , WIDTH , HEIGHT);
}
//得到坦克的好坏
public boolean getGood()
{
return good ;
}
//设置坦克的生死
public void setLive(boolean live)
{
this.live = live ;
}
//得到坦克的生死
public boolean getLive()
{
return live ;
}
public void setLife(int life)
{
this.life = life ;
}
public int getLife()
{
return life ;
}
private class BloodBar
{
public void draw(Graphics g)
{
Color c = g.getColor() ;
g.setColor(Color.RED);
g.drawRect(x, y-10, WIDTH, 10);
int w = WIDTH * life / 100 ;
g.fillRect(x, y-10, w, 10);
g.setColor(c);
}
}
}
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();
}
}
}
完整代码如下:
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;
// 键盘获取事件的函数
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();
}
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; iemsize;i) {
EmenyTank em = new EmenyTank((i1) * 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 自动生成的 catch 块
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
super.paint(g);
// 画板,坦克得放在画板后头
g.fillRect(0, 0, 400, 400);
// paint敌人坦克
for (int i = 0; iemeny.size();i) {
EmenyTank em = 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 (int i = 0; ihero.shot.size(); i) {
Shot myShot = hero.shot.get(i);
if (myShot != nullmyShot.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) {
// 判断坦克的颜色(敌我)然后画出坦克
switch (color) {
case 0:
g.setColor(Color.BLUE);
break;
case 1:
g.setColor(Color.YELLOW);
break;
case 2:
g.setColor(Color.GREEN);
break;
}
// 判断坦克的方向然后再画出坦克
switch (direct) {
case 0:
g.fill3DRect(x, y, 10, 30, false);
g.fill3DRect(x26, y, 10, 30, false);
g.fill3DRect(x10, y5, 16, 20, false);
g.drawLine(x18, y15, x18, y);
break;
case 1:
g.fill3DRect(x, y, 10, 30, false);
g.fill3DRect(x26, y, 10, 30, false);
g.fill3DRect(x10, y5, 16, 20, false);
g.drawLine(x18, y15, x18, y30);
break;
case 2:
g.fill3DRect(x3, y - 3, 30, 10, false);
g.fill3DRect(x3, y23, 30, 10, false);
g.fill3DRect(x8, y7, 20, 16, false);
g.drawLine(x18, y15, x3, y15);
break;
case 3:
g.fill3DRect(x3, y - 3, 30, 10, false);
g.fill3DRect(x3, y23, 30, 10, false);
g.fill3DRect(x8, y7, 20, 16, false);
g.drawLine(x18, y15, x33, y15);
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 (x400 || x0 || y400 || y0) {
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 = x18;
shota.y = y;
shota.direct = 0;
shot.add(shota);
break;
case 1:
shota = new Shot();
shota.x = x18;
shota.y = y30;
shota.direct = 1;
shot.add(shota);
break;
case 2:
shota = new Shot();
shota.x = x3;
shota.y = y15;
shota.direct = 2;
shot.add(shota);
break;
case 3:
shota = new Shot();
shota.x = x33;
shota.y = y15;
shota.direct = 3;
shot.add(shota);
break;
}
Thread t = new Thread(shota);
t.start();
}
public void moveUp() {
if (y0) {
y -= speed;
}
}// 我的坦克得在自己的类里定义怎么移动
public void moveDown() {
if (y367) {
y= speed;
}
}
public void moveLeft() {
if (x0) {
x -= speed;
}
}
public void moveRight() {
if (x365) {
x= speed;
}
}
}
【java代码画出tank java代码画图】关于java代码画出tank和java代码画图的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读