最近闲来无事复习下java基础,没有特别复杂的功能,一切从简,旨在回顾知识点!!!
游戏效果如下:
我们通过键盘控制飞机前后移动躲避炮弹,如果碰到炮弹则会爆炸,游戏结束并显示游戏坚持的时间
基本的功能实现
MyGameFrame类
package com.xiaodao.game;
import java.awt.Color;
import java.awt.Font;
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.util.Date;
/**
* 飞机游戏的主窗口
* @author 刘小叨
*
*/
public class MyGameFrame extends Frame {
Image plane = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
Date startTime = new Date();//游戏起始时刻
Date endTime;
int gameTime;
Plane p = new Plane(plane,250,250);
Shell s = new Shell();
Shell[] shells = new Shell[50];
Explode bao;
@Override
public void paint(Graphics g) {//自动被调用 g 就是一支画笔
Color c = g.getColor();
g.drawImage(bg, 0, 0, null);
p.drawSelf(g);//画飞机
//画出所有炮弹
for(int i=0;i<shells.length;i++) {
shells[i].draw(g);
//飞机和炮弹的碰撞检测
boolean peng = shells[i].getRect().intersects(p.getRect());
if(peng) {
p.live = false;
if(bao == null) {
bao = new Explode(p.x, p.y);
endTime = new Date();
gameTime = (int)((endTime.getTime()-startTime.getTime())/1000);
}
bao.draw(g);
}
if(!p.live) {
g.setColor(Color.red);
Font f = new Font("宋体",Font.BOLD,50);
g.setFont(f);
g.drawString("时间:"+ gameTime + "秒",125,250);
}
}
g.setColor(c);
}
//帮助我们反复重画窗口
class PaintThread extends Thread{
@Override
public void run() {
while(true) {
// System.out.println("窗口画一次");
repaint();//重画
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//键盘监听的内部类
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
p.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
p.minusDirection(e);
}
}
/**
* 初始化窗口
*/
public void launchFrame() {
this.setTitle("只想敲个代码的作品");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
this.setLocation(700, 300);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start();//启动重画窗口的线程
addKeyListener(new KeyMonitor());//给窗口增加键盘监听
//初始化五十个炮弹
for(int i=0;i<shells.length;i++) {
shells[i] = new Shell();
}
}
public static void main(String[] args) {
MyGameFrame mf = new MyGameFrame();
mf.launchFrame();
}
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);
}
}
GameUtil工具类
package com.xiaodao.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() {
}
/**
* 返回指定路径的图片对象
* @param path
* @return
*/
public static Image getImage(String path) {
BufferedImage bi = null;
try {
URL u = GameUtil.class.getClassLoader().getResource(path);
bi = ImageIO.read(u);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bi;
}
}
GameObject类
package com.xiaodao.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
/**
* 游戏物体的父类
* @author 刘小叨
*
*/
public class GameObject {
Image image;
double x,y;
int speed;
int width,height;
public void drawSelf(Graphics g) {
g.drawImage(image, (int)x, (int)y,null);
}
public GameObject(Image image, double x, double y, int speed, int width, int height) {
super();
this.image = image;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public GameObject(Image image, double x, double y) {
super();
this.image = image;
this.x = x;
this.y = y;
}
public GameObject() {
}
/**
* 返回物体所在的矩形,便于后续的碰撞检测
* @return
*/
public Rectangle getRect() {
return new Rectangle((int)x, (int)y, width, height);
}
}
Plane类
package com.xiaodao.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(image, (int)x, (int)y,null);
if(x>0 && x<Constant.GAME_WIDTH-width && y>40 && y<Constant.GAME_HEIGHT-height) {
if(left) {
x-=speed;
}
if(right) {
x+=speed;
}
if(up) {
y-=speed;
}
if(down) {
y+=speed;
}
}else if(x<0 && y>40 && y<Constant.GAME_HEIGHT-height) {
if(up) {
y -=speed;
}
if(right) {
x +=speed;
}
if(down) {
y +=speed;
}
}
else if(x>Constant.GAME_WIDTH-width && y>40 && y<Constant.GAME_HEIGHT-height) {
if(left) {
x -=speed;
}
if(up) {
y -=speed;
}
if(down) {
y +=speed;
}
}
else if(x>0 && x<Constant.GAME_WIDTH-width && y>Constant.GAME_HEIGHT-height) {
if(left) {
x -=speed;
}
if(right) {
x +=speed;
}
if(up) {
y -=speed;
}
}
else if(x>0 && x<Constant.GAME_WIDTH-width && y<40) {
if(left) {
x -=speed;
}
if(right) {
x +=speed;
}
if(down) {
y +=speed;
}
}
else if(x<0 && y>Constant.GAME_HEIGHT-height) {
if(right) {
x +=speed;
}
if(up) {
y -=speed;
}
}
else if(x>Constant.GAME_WIDTH-width && y>Constant.GAME_HEIGHT-height) {
if(up) {
y -=speed;
}
if(left) {
x -=speed;
}
}
else if(x>Constant.GAME_WIDTH-width && y<40) {
if(down) {
y +=speed;
}
if(left) {
x -=speed;
}
}
else if(x<0 && y<40) {
if(down) {
y +=speed;
}
if(right) {
x +=speed;
}
}else {
}
}
}
public Plane(Image image,double x, double y) {
this.image = image;
this.x = x;
this.y = y;
this.speed = 4;
this.width = image.getWidth(null);
this.height = image.getHeight(null);
}
//按下某个键增加相应的方向
public void addDirection(KeyEvent e) {
switch(e.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;
default:
break;
}
}
//按下某个键取消相应的方向
public void minusDirection(KeyEvent e) {
switch(e.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;
default:
break;
}
}
}
Shell类
package com.xiaodao.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 = 4;
degree = Math.random()*Math.PI*2;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(c.YELLOW);
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<40||y>Constant.GAME_HEIGHT-height) {
degree =-degree;
}
g.setColor(c);
}
}
Constant类
package com.xiaodao.game;
public class Constant {
public static final int GAME_WIDTH = 500;
public static final int GAME_HEIGHT = 500;
}
Explode类
package com.xiaodao.game;
import java.awt.Graphics;
import java.awt.Image;
/**
* 爆炸类
* @author 刘小叨
*
*/
public class Explode {
double x,y;
static Image[] imgs= new Image[16];
static {
for(int i=0;i<16;i++) {
imgs[i] =GameUtil.getImage("images/explode/e"+(i+1)+".gif");
imgs[i].getWidth(null);
}
}
int count;
public void draw(Graphics g) {
if(count<=15) {
g.drawImage(imgs[count], (int)x, (int)y, null);
count++;
}
}
public Explode(double x,double y) {
this.x = x;
this.y = y;
}
}
总结与思考
使用AWT和Swing GUI技术 画出窗口和图形加载,JFrame类java.swing中的主类,画的窗口必须继承JFrame,在继承java.swing.JFrame之后仍发现有轻微闪烁,只能使用继承java.awt.Frame类,并使用“双缓冲技术”来解决闪烁问题
用addWindowListener()来增加窗口监听事件,System.exit(0)表示应用正常结束,使用GameUtil工具类来加载图片代码,设计GameObject类,并且用它作为所有游戏体的父类,以便于Plane类和Shell类继承,Plane类需要键盘和程序交互,每次按下松开键都应该触发相应的键盘事件,通过上下左右来控制飞机,程序根据四个方向的状态进行移动,只需要对飞机的X,Y坐标进行加减法,并且需要注意飞机移动不应该越过边界,我们需要在Plane类中限制飞机活动的范围。
在MyGameFrame类中使用KeyMonitor内部类来实现键盘监听功能,Shell类在固定处生成炮弹,方向随机,并遇到边界会自动反弹,在MyGameFrame类中增加数组用来添加一定数量的炮弹,然后进行矩形检测来判断飞机和炮弹是否有过碰撞,当飞机和炮弹碰撞后会出现爆炸效果,而爆炸效果实际上是一系列爆炸的图片进行轮播。我们在初始化窗口时定义一个起始时间,当飞机与炮弹碰撞游戏结束时,保存一个结束时间,两者相减就是游戏存活的时间。