GameObject defined class
We found that all the window objects (aircraft, artillery, etc.) have a lot in common: "Picture Object, coordinate position, speed, width and height." In order to facilitate program development, we need to design a GameObject class, it can serve as the parent of all game objects, to facilitate our programming.
GameObject categories:
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);
}
}
Aircraft design class
With GameObject the parent class, we designed the aircraft type is particularly simple, there is no particular aircraft type complex requirements. We simply inherited, you can use:
Plane class:
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;
}
}
Through inheritance, we found that to achieve a new class, a lot of cool!
Adjust the invocation of class MyGameFrame
After class we will Plane package, there is no need to add the class attribute in MyGameFrame so many aircraft, we all packed into the Plane class which, therefore, also calls easier.
Encapsulated MyGameFrame categories:
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();
}
}
By the object-oriented package, if we again create multiple planes, it is easier than ever.
Create multiple aircraft:
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); //画出飞机本身
}
//其余代码,和上个版本一致,为节省篇幅突出重点,不在附上。
}
running result: