老师按照代码增加碰撞检测之后,并没有效果,无法检测到碰撞
MyGameFrame类
package com.cyq.plane;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
//增加碰撞检测
public class MyGameFrame extends Frame {
boolean left,up,right,down;//飞机方向的控制
//将背景图片与飞机图片定义为成员变量
Image bgimg = GameUtil.getImage("images/bg.jpg");
Image planeimg = GameUtil.getImage("images/plane.png");
//创建飞机对象
plane p1=new plane(planeimg,300,300,7);
//创建炮弹数组
Shell[] shells = new Shell[50];
//画出窗口
@Override
public void paint(Graphics g) {
g.drawImage(bgimg,0,0,null);
p1.drawMySrlf(g);//画出飞机本身
//画炮弹
for (int i=0;i<shells.length;i++){
shells[i].drawMySrlf(g);
//飞机碰撞检测
boolean peng = shells[i].getRect().intersects(p1.getRect());
if (peng){
// System.out.println("飞机被击中!!!!!!");
p1.live=false;
}else{
System.out.println("飞机还活着");
}
}
if(left){
p1.x -=p1.speed;
}
if (right){
p1.x+=p1.speed;
}
if (up){
p1.y-=p1.speed;
}
if (down){
p1.y+=p1.speed;
}
}
//创建画窗口类
public void launchFrame(){
// 初始化窗口
this.setTitle("Test Game");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
this.setLocation(300,100);//游戏窗口左上角距离电脑窗口的距离分别为横坐标300,纵坐标100
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
// 初始化键盘监听
new PaintThread().start();
this.addKeyListener(new KeyMonitor());//启动键盘监听
//初始化创建50个炮弹对象
for (int i=0;i<50;i++){
shells[i] = new Shell();
}
}
/**
* 定义一个重画窗口的线程类,是一个内部类(方便调用外部类的方法)
*/
class PaintThread extends Thread{
public void run(){
while (true){
repaint();//内部类可以直接调用外部类的成员(repaint为Frame内的方法)
try {
Thread.sleep(50);//程序睡50毫秒 1s=1000ms(毫秒) 1s画20次窗口--》(20*50=1000)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
System.out.println("按下:"+e.getKeyCode());
if (e.getKeyCode()==KeyEvent.VK_LEFT){
left=true;
}
if (e.getKeyCode()==KeyEvent.VK_UP){
up=true;
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT){
right=true;
}
if (e.getKeyCode()==KeyEvent.VK_DOWN){
down=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("抬起:"+e.getKeyCode());
if (e.getKeyCode()==KeyEvent.VK_LEFT){
left=false;
}
if (e.getKeyCode()==KeyEvent.VK_UP){
up=false;
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT){
right=false;
}
if (e.getKeyCode()==KeyEvent.VK_DOWN){
down=false;
}
}
}
private Image offScreenImage = null;
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);
}
//mian方法程序执行入口
public static void main(String[] args) {
MyGameFrame f=new MyGameFrame();//根据类创建对象
f.launchFrame();//通过对象调用类里面的方法
}
}
plane类
package com.cyq.plane;
import java.awt.*;
public class plane extends GameObject {
//飞机方向
boolean left,right,up,down;
boolean live = true; //或者
//绘画本类
@Override
public void drawMySrlf(Graphics g) {
if (live) {
super.drawMySrlf(g);
//飞机上下左右飞行方向
if(left) {
x -= speed;
}
if(right){
x += speed;
}
if(up){
y -= speed;
}
if(down) {
y += speed;
}
}
}
public plane(Image img, double x, double y, int speed) {
super(img, x, y, speed);
}
}
Shell类
package com.cyq.plane;
import java.awt.*;
public class Shell extends GameObject{
double degree;//角度,炮弹沿着指定的角度飞行
public Shell(){
x = 200;
y = 200;
degree=Math.random()*Math.PI*2;
width =5;
height=5;
speed = 3;
}
@Override
public void drawMySrlf(Graphics g) {
// super.drawMySrlf(g);没有炮弹图片,自己画一个
Color c=g.getColor();//获取保存g原始颜色
g.setColor(Color.yellow);//设置炮弹颜色,记得将原来的颜色还给g
g.fillOval((int)x,(int)y,width,height);//该方法是画实心圆,定义出现位置和宽度高度
g.setColor(c);//将原来的颜色还给g
//根据自己算法指定移动的路径
// x++;
// y++;
x += speed*Math.cos(degree);
y += speed*Math.sin(degree);
//如下代码,用来实现碰到边界,炮弹反弹回来(原理和打台球游戏一样)
if(y>Constant.GAME_HEIGHT-height||y<30){
degree = -degree;
}
if(x<0||x>Constant.GAME_WIDTH-width){
degree = Math.PI-degree;
}
}
}
GameUtill类
package com.cyq.plane;
import javax.imageio.ImageIO;//实现图片加载
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
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;
}
}
GameObject类
package com.cyq.plane;
import java.awt.*;
public class GameObject {
Image img;//该物体对应的图片对象
double x,y;//该物体的坐标
int speed;//该物体的运行速度
int width,height;//该物体所在矩形区域的宽度和高度
/**
* 怎么样绘制本对象
* g
*/
public void drawMySrlf(Graphics g){
g.drawImage(img,(int)x,(int)y,null);
}
public GameObject(Image img, double x, double y){
this.img=img;
this.x=x;
this.y=y;
if(img!=null){
this.width=img.getWidth(null);
this.height=img.getHeight(null);
}
}
public GameObject(Image img, double x, double y, int speed, int width, int height){
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, int speed){
this.img=img;
this.x=x;
this.y=y;
this.speed=speed;
}
public GameObject(){
}
/**
* 返回物体对应矩形区域,便于后续在碰撞检测中使用
* return
*/
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);
}
}
Constant类
package com.cyq.plane;
//存放相关常量
public class Constant {
//窗口的宽度
public static final int GAME_WIDTH=500;
// 窗口的高度
public static final int GAME_HEIGHT=500;
}