会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题

问题和BUG:

        飞机死了以后,还在一直进行碰撞检测,而且还有爆炸的效果伴随,想了半天也不知道怎么解决;请问老师,这个问题该怎么解决?

核心代码:

 public void paint(Graphics g) {
  // TODO Auto-generated method stub
  super.paint(g);//如果画出来的窗口是黑色的,请加上此行代码;
  count++;
  g.drawImage(bg, 0, 0, null);   //画出背景
  
  
   plane2.drawSelf(g);   
  
  
  for(int a=0;a<shells.length;a++) {
   Color c = g.getColor();
   shells[a].drawSelf(g);//将所有Shell对象全部画出来,利用 for循环
   
   //检测每个shell对象有没有和plane对象相交
   
   boolean intersect=shells[a].getRect().intersects(plane2.getRect());    
    if(intersect) {  //判断是否相交
     
     System.out.println("相交了............");
     plane2.live=false;  //相交了,则表示飞机死了
     intersect =false;
     if(explode==null) { // 如爆炸对象是null,则创建爆炸对象
      explode = new Explode(plane2.x,plane2.y);
      endTime = new Date();  // 飞机死的时候则时间结束    
      period = (endTime.getTime()-startTime.getTime())/1000;
     }
     explode.draw(g); //画出爆炸对象    
    }
    //计算时间,给出游戏提示
    if(!plane2.live) { //飞机死了,则打印游戏时长
     g.setColor(Color.red); //设置字体颜色
     Font  f= new Font("宋体",Font.BOLD,30);
     g.setFont(f);
     g.drawString("时间:"+period+"秒", Constant.GAME_WIGTH/2-100,
       Constant.GAME_HEIGHT/2); //画出所玩游戏时长    
    }
    g.setColor(c); 
  }
 }

源码如下:

MyPlaneGame.zip


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3511楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3512楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3514楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3515楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3516楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 3517楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3519楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3523楼

炮弹和飞机相撞不输出 “相撞了!!”这个结果 不知道是哪里错了



package cn.sxt.game;


public class Constant {


public static final int GAME_WIDTH=500;

public static final int GAME_HEIGHT=500;

}

——————————————————————————————————————

package cn.sxt.game;


import java.awt.Graphics;

import java.awt.Image;

import java.awt.Rectangle;


/**

 * 游戏物体的父类

 * @author 张紫英

 *

 */

public class GameObject {

Image img;

double x,y;

int speed;

int width,height;

public void drawSelf(Graphics g) {

g.drawImage(img, (int)x, (int)y, null);

}


public GameObject(Image img, double x, double y, int speed, int width, int height) {

super();

this.img = img;

this.x = x;

this.y = y;

this.speed = speed;

this.width = width;

this.height = height;

}


public GameObject(Image img, double x, double y) {

super();

this.img = img;

this.x = x;

this.y = y;

}


public GameObject() {

}

//返回物体所在矩形 便于后续的碰撞检测

public Rectangle getRect() {

return new Rectangle((int)x,(int)y,width,height);

}

}

-------------------------------------------------------------


package cn.sxt.game;

 

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.URL;

import javax.imageio.ImageIO;

 

public class GameUtil {

    // 工具类最好将构造器私有化。

    private GameUtil() {

     

    } 

 

    public static Image getImage(String path) {

        BufferedImage bi = null;

        try {

            URL u = GameUtil.class.getClassLoader().getResource(path);

            bi = ImageIO.read(u);

        } catch (IOException e) {

            e.printStackTrace();

        }

        return bi;

    }

}

————————————————————————————————————————

package cn.sxt.game;


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 javax.swing.WindowConstants;


/**

 * 飞机游戏的主窗口

 * @author 张紫英

 *

 */

public class MyGameFrame extends Frame {

Image planeImg = GameUtil.getImage("images/plane.png");

Image bg = GameUtil.getImage("images/bg.jpg");

Plane plane = new  Plane(planeImg,250,250);

Shell[] shells = new Shell[50];

private Image offScreenImage = null;


@Override

public void paint(Graphics g) { //自动被调用 g相当于一支画笔

super.paint(g);

g.drawImage(bg, 0, 0, null);

plane.drawSelf(g);

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

shells[i].draw(g);

}

}

public void update(Graphics g) {

    if(offScreenImage == null)

        offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度

     

    Graphics gOff = offScreenImage.getGraphics();

    paint(gOff);

    g.drawImage(offScreenImage, 0, 0, null);

}  

//帮我们反复重画窗口

class PaintThread extends Thread{

public void run() {

while(true) {

repaint();//重画

try {

Thread.sleep(40);//1s=1000ms

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

//定义键盘监听内部类

class KeyMonitor extends KeyAdapter{


@Override

public void keyPressed(KeyEvent arg0) {

// TODO Auto-generated method stub

super.keyPressed(arg0);

plane.addDirection(arg0);

}


@Override

public void keyReleased(KeyEvent arg0) {

// TODO Auto-generated method stub

super.keyReleased(arg0);

plane.minusDirection(arg0);

}

}

/**

* 初始化窗口

*/

public void launchFrame() {

this.setTitle("张紫英copy的作品");

this.setVisible(true);

this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);

this.setLocation(300,300);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

new PaintThread().start();//启动重画窗口的线程

addKeyListener(new KeyMonitor());//给键盘增加键盘监听

//初始化50个炮弹

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

shells[i] = new Shell();

boolean peng = shells[i].getRect().intersects(plane.getRect());

            if(peng){

                System.out.println("相撞了!!");

                plane.live = false; 

            }

}

}

public static void main(String[] args) {

MyGameFrame f = new MyGameFrame();

f.launchFrame();

}

}

————————————————————————————————————————

package cn.sxt.game;


import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyEvent;


public class Plane extends GameObject{

boolean left,up,right,down;

boolean live = true;

public void drawSelf(Graphics g) {

if(live) {

g.drawImage(img, (int)x, (int)y, null);

if(left) {

x-=speed;

}

if(right) {

x+=speed;

}

if(up) {

y-=speed;

}

if(down) {

y+=speed;

}

}else {

}

}

public Plane(Image img,double x,double y) {

this.img = img;

this.x = x;

this.y = y;

this.speed = 3;

this.width = img.getWidth(null);

this.height = img.getHeight(null);

}

//按下某个键  增加相应的方向

public void addDirection(KeyEvent arg0) {

switch(arg0.getKeyCode()) {

case KeyEvent.VK_LEFT:

left = true;

break;

case KeyEvent.VK_UP:

up = true;

break;

case KeyEvent.VK_RIGHT:

right = true;

break;

case KeyEvent.VK_DOWN:

down = true;

break;

}

}

//按下某个键  取消相应的方向

public void minusDirection(KeyEvent arg0) {

switch(arg0.getKeyCode()) {

case KeyEvent.VK_LEFT:

left = false;

break;

case KeyEvent.VK_UP:

up = false;

break;

case KeyEvent.VK_RIGHT:

right = false;

break;

case KeyEvent.VK_DOWN:

down = false;

break;

}

}

}

————————————————————————————————

package cn.sxt.game;


import java.awt.Color;

import java.awt.Graphics;


/**

 * 炮弹类

 * @author 张紫英

 *

 */

public class Shell extends GameObject {

double degree;

public Shell() {

x = 200;

y = 200;

width = 10;

height = 10;

speed = 3;

degree = Math.random()*Math.PI*2;

}

public void draw(Graphics g) {

Color c = g.getColor();

g.setColor(Color.GREEN);

g.fillOval((int)x, (int)y, width, height);

//任意角度去飞

x += speed*Math.cos(degree);

y += speed*Math.sin(degree);

if(x<0||x>Constant.GAME_WIDTH-width) {

degree = Math.PI-degree;

}

if(y<30||y>Constant.GAME_HEIGHT-height) {

degree =-degree;

}

g.setColor(c);

}

}


QQ截图20190518170308.png

JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3524楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3525楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637