1. java的坦克大战程序
java.lang.NoClassDefFoundError: Hstank2 (wrong name:HsTank2)
找不到类Hstank2,确定你的包路径跟实际放置的文件夹路径相同,类名与引用处相同。
这应该是个极低级的错误。
2. (100分)Java写“坦克大战”
package com.bjsxt.tank;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 这个类的作用是坦克游戏的主窗口
* @author mashibing
*
*/
public class TankClient extends Frame {
/**
* 整个坦克游戏的宽度
*/
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(50, 50, true, Direction.STOP, this);
Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);
List<Explode> explodes = new ArrayList<Explode>();
List<Missile> missiles = new ArrayList<Missile>();
List<Tank> tanks = new ArrayList<Tank>();
Image offScreenImage = null;
Blood b = new Blood();
public void paint(Graphics g) {
/*
* 指明子弹-爆炸-坦克的数量
* 以及坦克的生命值
*/
g.drawString("missiles count:" + missiles.size(), 10, 50);
g.drawString("explodes count:" + explodes.size(), 10, 70);
g.drawString("tanks count:" + tanks.size(), 10, 90);
g.drawString("tanks life:" + myTank.getLife(), 10, 110);
if(tanks.size() <= 0) {
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProceTankCount")); i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}
}
for(int i=0; i<missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.hitWall(w2);
m.draw(g);
//if(!m.isLive()) missiles.remove(m);
//else m.draw(g);
}
for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
t.collidesWithWall(w1);
t.collidesWithWall(w2);
t.collidesWithTanks(tanks);
t.draw(g);
}
myTank.draw(g);
myTank.eat(b);
w1.draw(g);
w2.draw(g);
b.draw(g);
}
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
/**
* 本方法显示坦克主窗口
*
*/
public void lauchFrame() {
int initTankCount = Integer.parseInt(PropertyMgr.getProperty("initTankCount"));
for(int i=0; i<initTankCount; i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}
//this.setLocation(400, 300);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);
this.addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
private class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter {
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
}
3. java 编写坦克大战
需要看你的tank的draw方法里的内容代码和碰撞检测代码。
一般二维碰撞检测基本都是以单位格,也就是坦克的大小格作为单位,提前一格判断两个方格是否交叉,如果交叉则在当前,也就是交叉后退后一格这个位置让它停止当前方向的移动。
4. 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;
}
}
}
5. java swing坦克大战,如何实现发子弹
创建子弹形状,初始位置为坦克前方,使用循环在坦克朝向上坐标递加或递减,并重新绘制子弹。就能模拟子弹的行进。判断击中,可以用形状是否相交的函数。