在学习了java入门的课程之后,自己动手跟着老师写的一个小游戏,用的是Frame。总共有七个类。
1.飞机游戏的主窗口(MyGameFrame)继承Frame。
package com.bjsxt.plane;
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.io.Writer;
import java.util.Date;
import javax.swing.JFrame;
/**
* 飞机游戏的主窗口
* @author 机械师
*
*/
public class MyGameFrame extends Frame{
//导入图片
Image planeImage=GameUtil.getImage("images/plane.png");
Image bg=GameUtil.getImage("images/bg.jpg");
Plane plane=new Plane(planeImage,250,250);
Shell[] shells=new Shell[50];
Explode bao;
Date startTime=new Date();
Date endTime;
int period;
//帮助绘制窗口
@Override
public void paint(Graphics g) {//自动调用
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g);//画飞机
// 画出所有的炮弹
for(int i=0;i<shells.length;i++){
shells[i].draw(g);
boolean peng=shells[i].getRect().intersects(plane.getRect());
if (peng) {
plane.live=false;
if (bao==null) {
bao=new Explode(plane.x, plane.y);
endTime=new Date();
period=(int)((endTime.getTime()-startTime.getTime())/1000);
}
}
if (!plane.live) {
Color c=g.getColor();
Font f=new Font("楷体", Font.BOLD, 25);
g.setFont(f);
g.setColor(Color.red);
g.drawString("游戏结束!! 您已存活:"+period+"秒", 110, 250);
bao.draw(g);
g.setColor(c);
}
}
}
class PaintThread extends Thread{
//帮助我们反复的重画窗口
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(40);//一秒画25次窗口
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//定义键盘监听
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
/**
* 初始化窗口
*/
public void launchFrame(){
setTitle("bjsxt_yxf--飞机躲避子弹小游戏游戏");
setVisible(true);//true表示窗口可见
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
setLocation(300,300);
//增加监听事件,关闭动作,点击窗口的关闭按钮,程序便已经结束。
this.addWindowListener(new WindowAdapter() {
@Override
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();
}
}
public static void main(String[] args) {
MyGameFrame f=new MyGameFrame();
f.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);
}
}
2.游戏的图片导入工具类(GameUtil)
package com.bjsxt.plane;
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) {
e.printStackTrace();
}
return bi;
}
}
3.游戏物体的父类,游戏里的炮弹,飞机的属性都是继承这个类(GameObject)
package com.bjsxt.plane;
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);
}
}
4.爆炸类,飞机死亡的一系列动画操作(Explode)
package com.bjsxt.plane;
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 < imgs.length; i++) {
imgs[i]=GameUtil.getImage("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;
}
}
5.飞机类,里面有飞机的速度,键盘按键设置等属性(Plane)
package com.bjsxt.plane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
/**
* 飞机类
* @author 机械师
*
*/
public class Plane extends GameObject {
boolean left,right,down,up;
int speed=4;
boolean live=true;
public void drawSelf(Graphics g){
if (live) {
g.drawImage(image, (int)x, (int)y, null);
if (left) {
x-=speed;
}
if (right) {
x+=speed;
}
if (up) {
y-=speed;
}
if (down) {
y+=speed;
}
}
}
public Plane(Image image,double x,double y){
this.image=image;
this.x=x;
this.y=y;
this.speed=speed;
this.width=image.getWidth(null);
this.height=image.getHeight(null);
}
public void addDirection(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left=true;
speed++;
break;
case KeyEvent.VK_UP:
up=true;
speed++;
break;
case KeyEvent.VK_RIGHT:
right=true;
speed++;
break;
case KeyEvent.VK_DOWN:
down=true;
speed++;
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;
}
}
}
6.炮弹类,里面包含了炮弹在固定位向任意方向发出数个炮弹,以及碰撞边界弹回。(Shell)
package com.bjsxt.plane;
import java.awt.Color;
import java.awt.Graphics;
/**
* 炮弹类
* @author 机械师
*
*/
public class Shell extends GameObject{
double degree;
public Shell(){
x=200;
y=200;
speed=3;
width=10;
height=10;
degree=Math.random()*Math.PI*2;//o-2π之间的随机数
}
public void draw(Graphics g){
Color c=g.getColor();
g.setColor(Color.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;
speed++;
} else if(y<30||y>Constant.GAME_HEIGHT-height){
degree=-degree;
speed++;
}
g.setColor(c);
}
}
7.常数类。存储游戏边界的大小(Constant)
package com.bjsxt.plane;
public class Constant {
public static final int GAME_WIDTH=500;
public static final int GAME_HEIGHT=500;
}
此小游戏在一开始,我们是继承Jframe的,但是由于在写到后面,看运行画面的时候还是有很多点闪烁,因此我们就改回到Frame,并且在主窗口类中使用了双缓冲。
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);
}
改完之后,画面有很大的改善,看着非常的舒服。