189 8069 5689

java坦克代码 java tank

(100分)Java写“坦克大战”

package com.bjsxt.tank;

创新互联建站-专业网站定制、快速模板网站建设、高性价比休宁县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式休宁县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖休宁县地区。费用合理售后完善,十余年实体公司更值得信赖。

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);

ListExplode explodes = new ArrayListExplode();

ListMissile missiles = new ArrayListMissile();

ListTank tanks = new ArrayListTank();

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; iInteger.parseInt(PropertyMgr.getProperty("reProduceTankCount")); i++) {

tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));

}

}

for(int i=0; imissiles.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; iexplodes.size(); i++) {

Explode e = explodes.get(i);

e.draw(g);

}

for(int i=0; itanks.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; iinitTankCount; 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);

}

}

}

java 坦克 打第一辆坦克没有爆炸效果

你提供的代码不完全,无法深入分析,但是照你说的有显示“进入画图”,那么,很可能if(g.drawImage(image1, b.x, b.y, 30, 30, this))这个返回值为false,我刚查了下API,它的返回值说明Returns:

false if the image pixels are still changing; true otherwise.另外这个方法有这么样的说明This method returns immediately in all cases, even if the entire image has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete, then drawImage returns false. 也就是说,现行输出未完成,图像像素仍在变化的时候,你就调用了这方法,那么会返回false。你检查一下代码,在你调用这段代码前,你的初始化工作做好了没。另外,最好就是像1楼说的,把图画到一个bufferImage里,再把bufferImage画到屏幕上

JAVA 坦克大战

import java.awt.*;

import javax.swing.*;

public class Tank extends JFrame {

mypane mp=null;

Obj[] objs=new Obj[0];

public Tank() {

setTitle("坦克大战");

setSize(800,600);

pro();

add(new mypane(objs));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

//在这里添加键盘事件、鼠标事件、让坦克移动,修改objs数组对象让他们移动

setVisible(true);

}

private void pro(){

Obj[] tmp=new Obj[objs.length+1];

System.arraycopy(objs,0,tmp,0,objs.length);

tmp[tmp.length-1]=new Obj(1,1,0,1);

objs=tmp;

int num=(int)(Math.random()*5)+1;

for(int i=0;inum;i++){

int x=(int)(Math.random()*getWidth())+1;

int y=(int)(Math.random()*getHeight())+1;

int dir=(int)(Math.random()*4);

Obj[] dst=new Obj[objs.length+1];

System.arraycopy(objs,0,dst,0,objs.length);

dst[dst.length-1]=new Obj(x,y,1,dir);

objs=dst;

}

}

public static void main(String[] args) {

new Tank();

}

}

class Obj{

int x,y;//坦克坐标

int type;

int dir;

public Obj(int x,int y,int type,int dir){

this.x=x;

this.y=y;

this.type=type;

this.dir=dir;

}

}

class mypane extends JPanel{

Obj[] objs;

public mypane(Obj[] objs){

this.objs=objs;

}

public void paint(Graphics g) {

super.paint(g);

for(int i=0;iobjs.length;i++){

Obj obj=objs[i];

drawtank(obj.x,obj.y, g, obj.type, obj.dir);

}

g.dispose();

}

public void drawtank(int x,int y,Graphics g, int type,int direct) {

/*type 为坦克类型,敌方,我方*/

switch(type) {

case 0://我方坦克,设置为红色

g.setColor(Color.red);

break;

case 1://敌方坦克,设置为蓝色

g.setColor(Color.blue);

break;

}

switch(direct) {

case 0://坦克方向朝上

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;

case 1://坦克方向朝右

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;

case 2://方向向下

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;

case 3://方向向左

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;

}

}

}


网站题目:java坦克代码 java tank
本文来源:http://cdxtjz.cn/article/ddgejpc.html

其他资讯