GameObject类的定义
我们发现,窗口中所有的对象(飞机、炮弹等等)都有很多共性:“图片对象、坐标位置、运行速度、宽度和高度”。为了方便程序开发,我们需要设计一个GameObject类,它可以作为所有游戏物体的父类,方便我们编程。
GameObject类:
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() {
}
/**
* 返回物体所在的矩形。便于后续的碰撞检测
* @return
*/
public Rectangle getRect(){
return new Rectangle((int)x, (int)y, width, height);
}
}
设计飞机类
有了GameObject这个父类,我们设计飞机类特别简单,目前飞机类没有特别复杂的要求。我们只需简单的继承,即可使用:
Plane类:
import java.awt.Graphics;
import java.awt.Image;
public class Plane extends GameObject {
public void drawSelf(Graphics g){
g.drawImage(img, (int)x,(int) y, null);
x++;
}
public Plane(Image img, double x, double y){
this.img = img;
this.x = x;
this.y = y;
}
}
通过继承,我们发现实现新的类,爽了很多!
MyGameFrame类调用方式的调整
我们将Plane类封装后,也无需在MyGameFrame类中添加那么多飞机的属性,我们全部封装到了Plane类里面,因此,调用也变得更加简单。
封装后的MyGameFrame类:
import java.awt.Color;
import java.awt.Font;
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.JFrame;
/**
* 飞机游戏的主窗口
* @author 高淇
*
*/
public class MyGameFrame extends JFrame {
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
Plane plane = new Plane(planeImg,250,250);
@Override
public void paint(Graphics g) { //自动被调用。 g相当于一只画笔
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g); //画飞机
}
//帮助我们反复的重画窗口!
class PaintThread extends Thread {
@Override
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 e) {
System.out.println("按下:"+e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("抬起:"+e.getKeyCode());
}
}
/**
* 初始化窗口
*/
public void launchFrame(){
this.setTitle("尚学堂学员_程序猿作品");
this.setVisible(true);
this.setSize(500, 500);
this.setLocation(300, 300);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start(); //启动重画窗口的线程
addKeyListener(new KeyMonitor()); //给窗口增加键盘的监听
}
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame();
f.launchFrame();
}
}
通过面向对象封装后,如果我们要再创建多个飞机,也变得异常简单。
创建多个飞机:
public class MyGameFrame extends JFrame {
Image bgImg = GameUtil.getImage("images/bg.jpg");
Image planeImg = GameUtil.getImage("images/plane.png");
Plane plane = new Plane(planeImg,300,300);
Plane plane2 = new Plane(planeImg,300,350);
Plane plane3 = new Plane(planeImg,300,400);
//paint方法作用是:画出整个窗口及内部内容。被系统自动调用。
@Override
public void paint(Graphics g) {
g.drawImage(bgImg, 0, 0, null);
plane.drawMySelf(g); //画出飞机本身
plane2.drawMySelf(g); //画出飞机本身
plane3.drawMySelf(g); //画出飞机本身
}
//其余代码,和上个版本一致,为节省篇幅突出重点,不在附上。
}
运行效果:
赵广陆 发布了90 篇原创文章 · 获赞 6 · 访问量 1万+ 私信 关注