当前位置:首页 » 操作系统 » java坦克大战源码

java坦克大战源码

发布时间: 2022-09-24 08:29:09

‘壹’ 用java写坦克大战难吗

不难,你基础没打好才有这种感觉。
坦克大战,so easy啊。

‘贰’ 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坦克大战,这段代码为什么子弹的坐标在变,却不能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做《坦克大战》需要积累那些java知识

java AWT Swing 还有一些工具类 比如说Random(获取随机数的)一些简单数学计算在程序中的应用 另外还需要线程来控制画面刷新 也需要一点线程知识 另外就是一些游戏用到的基础知识了 比如说碰撞检测 双缓冲 一些paint方法的应用 如根据坦克方向将坦克画出来等等 都比较简单

‘伍’ (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);
}

}
}

‘陆’ 关于JAVA坦克大战中坦克移动的问题,总有问题 不知道哪儿错了,求高手解答

源码[email protected]" data_size="0.01M" data_filelogo="https://gss0.bdstatic.com//yun-file-logo/file-logo-6.png" data_number="1" data_sharelink="https://pan..com/s/1i3pqmDR" data_code="">

用多线程做的坦克大战,这里提供src目录,运行请自己建立工程,并将src目录下的所有包文件导入工程src目录,main包运行; 很多年前写的希望能帮到你

‘柒’ java韩顺平的视频写的坦克大战中的坦克的绘制和移动,不能左右移动,还有坦克移动就会解体

比较贵发生毒皮哦咖啡色的

‘捌’ 我用java做一个坦克大战的小游戏,怎么实现游戏暂停和继续的功能,敌人的坦克还有子弹类是线程

线程设置sleep阻塞,或则使用Util.Timer类和TimerTask来实现暂停功能,继续可以用interupt中断阻塞,我是这样想的,不知道正不正确,希望对你有所帮助。

‘玖’ 在java中编写坦克大战时这样实现子弹发射击中敌方但敌方和子弹不消失

你是否有对子弹和坦克,这2个类进行完好的设计,你是否应该加一个生命状态的属性来实现此功能,比如加一个boolean islive;在绘图的方法中,绘制g.drawXXX之前,if(islive)一下,再然后在子弹命中的判断中,命中后把子弹的生命值和坦克的生命值都改变,而绘图的方法通过线程不断刷新,自然就消失了!

------最后,希望采纳!毕竟我们纯手打!

‘拾’ 我在做韩顺平老师的坦克大战java项目,刚做到能使敌方坦克爆炸时,产生爆炸效果,可是每次运行时,击中第

在你的paint函数中加上下面三句话就ok了。
g.drawImage(image1, 100, 100, 30, 30, this);
g.drawImage(image2, 100, 100, 30, 30, this);
g.drawImage(image3, 100, 100, 30, 30, this);

在super下面写上。

热点内容
跳转页源码 发布:2024-09-17 03:13:05 浏览:542
html文件上传表单 发布:2024-09-17 03:08:02 浏览:783
聊天软件编程 发布:2024-09-17 03:00:07 浏览:725
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207
cf编译后没有黑框跳出来 发布:2024-09-17 01:46:54 浏览:249
安卓怎么禁用应用读取列表 发布:2024-09-17 01:46:45 浏览:524
win10设密码在哪里 发布:2024-09-17 01:33:32 浏览:662
情逢敌手迅雷下载ftp 发布:2024-09-17 01:32:35 浏览:337
安卓如何让软件按照步骤自动运行 发布:2024-09-17 01:28:27 浏览:197