1、MyGameFrame游戏的主函数包(绘制图像、双缓冲技术)
package cn.ws.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 javax.swing.JFrame;
public class MyGameFrame extends Frame {
Image bg=GameUtil.getImage("images/bg.jpg"); //引入改图片
Image planeImg=GameUtil.getImage("images/plane.png");
Plane plane = new Plane(planeImg,250,250);
Shell[] shells = new Shell[50]; //创建50个炮弹数组
@Override
public void paint(Graphics g) { //相当于画笔工具,g就是画笔
super.paint(g); //必须加的
g.drawImage(bg,0, 0, null); //加载图片
plane.drawSelf(g); //画飞机
//画出所有炮弹
for(int i=0;i
shells[i].draw(g);
//是否飞机矩形框与小球矩形框相撞(碰撞检测)
boolean peng = shells[i].getRect().intersects(plane.getRect());
if(peng) {
plane.live = false;
}
}
}
//窗口重画
class PaintThread extends Thread{
public void run() {
while(true) {
repaint(); //重画窗口
try {
Thread.sleep(40);
}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() { //主窗口启动
this.setTitle("王烁制作的小灰机"); //主窗口标题
this.setVisible(true); //主窗口可见
this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); //主窗口尺寸
this.setLocation(400,100); //主窗口位置
this.addWindowListener(new WindowAdapter() { //主窗口关闭结束
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start(); //启动重画窗口
addKeyListener(new KeyMonitor()); //增加键盘监听
//初始化50个炮弹
for(int i=0;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、Constant设定窗口的大小包
package cn.ws.game;
public class Constant {
public static final int GAME_WIDTH = 500;
public static final int GAME_HEIGHT = 500;
}
3、GameObject初始化飞机大小以及返回所在矩形包(便于后续封装检测)
package cn.ws.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
Image img;
double x,y;
int speed=3;
int width;
int heigth;
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 heigth) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.heigth = heigth;
}
public GameObject(Image img, double x, double y) {
super();
this.img = img;
this.x = x;
this.y = y;
}
//返回物体所在矩形。便于后续碰撞
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,heigth);
}
public GameObject() {
}
}
4、GameUtil图片读取之BufferedImage得到像素矩阵包
package cn.ws.game;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
public class GameUtil {
private GameUtil() {
}
public static Image getImage(String path) {
BufferedImage bi= null;
try {
java.net.URL u = GameUtil.class.getClassLoader().getResource(path);
bi = ImageIO.read(u);
}catch(IOException e) {
e.printStackTrace();
}
return bi;
}
}
5、Plane初始化飞机和飞机控制和飞机矩形大小设定包
package cn.ws.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(img,(int)x,(int)y,null);
if(left) {
x-=speed;
}
if(up) {
y-=speed;
}
if(right) {
x+=speed;
}
if(down) {
y+=speed;
}
}
}
public Plane(Image img,double x,double y) {
this.img=img;
this.x=x;
this.y=y;
this.speed=3;
this.width=3; //img.getWidth(null) 获得图片宽度
this.heigth=3;
}
//按下某个键,增加相应方向
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;
}
}
//按下某个键,减相应方向
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 cn.ws.game;
import java.awt.Color;
import java.awt.Graphics;
public class Shell extends GameObject {
double degree;
public Shell() {
x=200;
y=200;
width=10;
heigth=10;
speed=2;
degree=Math.random()*Math.PI*2; //degree弧度,随机生产(0-2PI)
}
public void draw(Graphics g) {
Color c= g.getColor(); //先保存现在的颜色
g.setColor(Color.green);
g.fillOval((int)x, (int)y, width, heigth);
//炮弹沿着任意角度飞
x+=speed*Math.cos(degree);
y+=speed*Math.sin(degree);
if(x<0||x>Constant.GAME_WIDTH-width) {
degree = Math.PI-degree;
}
if(y<30||y>Constant.GAME_HEIGHT-heigth) {
degree = -degree;
}
g.setColor(c); //用完再换回去
}
}