来源:http://www.bjsxt.com/
1、S01E084-087太阳系模型实操
package com.test.util;
/**
* 游戏中关于窗口大小的常量
*/
public class Constant {
public static final int GAME_WIDTH = 500;
public static final int GAME_HEIGHT = 500;
}
//
package com.test.util;
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() {} //工具类通常会将构造方法私有,直接调用static方法
public static Image getImage(String path) {
URL url = GameUtil.class.getClassLoader().getResource(path);
BufferedImage img = null;
try {
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}
package com.test.util;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 游戏窗口类
*/
public class MyFrame extends Frame { //GUI编程:AWT,SWING等
//参考http://www.cnblogs.com/S-E-P/archive/2010/01/27/2045076.html
@Override
public void update(Graphics g) {//覆盖update方法,截取默认的调用过程
//创建图形缓冲区
Image imageBuffer = createImage(this.getWidth(),this.getHeight());
Graphics graImage = imageBuffer.getGraphics();//获取图形缓冲区的图形上下文
paint(graImage);//用paint方法中编写的绘图过程对图形缓冲区绘图
graImage.dispose();//释放图形上下文资源
g.drawImage(imageBuffer,0,0,this);//将图形缓冲区绘制到屏幕上
}
/**
* 加载窗口
*/
public void launchFrame() {
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//窗口大小
setLocation(100,100);//窗口位置
setVisible(true);//默认不可见
new PaintThread().start();//启动重画线程
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {//点击右上角"X"退出
System.exit(0);
}
});
}
/**
* 定义一个重画窗口的线程类
*/
class PaintThread extends Thread {
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.test.plane;
import java.awt.Color;
import java.awt.Graphics;
import com.test.util.Constant;
public class Bullet extends GameObject{
public Bullet(){
degree = Math.random() * Math.PI * 2;
x = Constant.GAME_WIDTH/2;
y = Constant.GAME_HEIGHT/2;
width = 10;
height =10;
}
public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x, (int)y, (int)width, (int)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 < 30 || y > Constant.GAME_HEIGHT - height){
degree = -degree;
}
g.setColor(c);
}
}
/
package com.test.plane;
import java.awt.Graphics;
import java.awt.Image;
import com.test.util.GameUtil;
/**
* 爆炸类
*/
public class Explode {
double x,y;
int count;
static Image[] imags = new Image[16];
static{
for (int i = 0; i < 16; i++) {
imags[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif");
imags[i].getWidth(null);//懒加载,要加上此代码才会马上加载图片
}
}
public Explode(double x,double y){
this.x = x;
this.y = y;
}
public void draw(Graphics g){
if(count < 16){
g.drawImage(imags[count], (int)x, (int)y, null);
count++;
}
}
}
///
package com.test.plane;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
Image img;
double x,y;
int speed = 3;
double width,height;
double degree;
public Rectangle getRect(){//碰撞检测中用到的矩形框
return new Rectangle((int)x, (int)y, (int)width, (int)height);
}
}
///
package com.test.plane;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import com.test.util.GameUtil;
public class Plane extends GameObject{
private boolean left,right,up,down;
private boolean live = true;
public Plane(){}
public Plane(String imgpath, double x, double y) {
this.img = GameUtil.getImage(imgpath);
this.width = img.getWidth(null);
this.height = img.getHeight(null);
this.x = x;
this.y = y;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
/**
* 画图和移动
* @param g
*/
public void draw(Graphics g){
if(live){
g.drawImage(img, (int)x, (int)y, null);
move();
}
}
/**
* 移动图片
*/
public void move(){
if(left){
x -= speed;
}
if(right){
x += speed;
}
if(up){
y -= speed;
}
if(down){
y += speed;
}
}
/**
* 根据按键设置相应方向的布尔值为ture
* @param e
*/
public void setDirectionTrue(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;
}
}
/**
* 根据按键设置相应方向的布尔值为false
* @param e
*/
public void setDirectionFalse(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;
}
}
}
//
package com.test.plane;
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.util.ArrayList;
import java.util.Date;
import com.test.util.GameUtil;
import com.test.util.MyFrame;
public class PlaneGameFrame extends MyFrame{
Image bg = GameUtil.getImage("images/bg.jpg");
Plane p = new Plane("images/plane.png", 50, 50);
ArrayList bulletList = new ArrayList();//暂不加泛型
Date startTime,endTime;
Explode bao;
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
p.draw(g);
for (int i = 0; i < bulletList.size(); i++) {
Bullet b = (Bullet) bulletList.get(i);
b.draw(g);
//检测跟飞机的碰撞
boolean peng = b.getRect().intersects(p.getRect());
if (peng){
p.setLive(false);//飞机死掉
if(bao == null){
endTime = new Date();//游戏结束
bao = new Explode(p.x,p.y);//最好用容器来处理,以后再优化
}
bao.draw(g);
break;//碰撞一下
}
}
if(!p.isLive()){
int period = (int)((endTime.getTime() - startTime.getTime())/1000);//获取毫秒时间/1000
printInfo(g,"时间:" + period + "秒",20,120,150,Color.WHITE);
switch (period/10) {
case 0://不足10秒
case 1:
printInfo(g,"菜鸟!",50,100,100,Color.WHITE);
break;
case 2:
printInfo(g,"小鸟!",50,100,100,Color.WHITE);
case 3:
printInfo(g,"大鸟!",50,100,100,Color.YELLOW);
case 4:
printInfo(g,"鸟王",50,100,100,Color.YELLOW);
default:
printInfo(g,"鸟人!",50,100,100,Color.WHITE);
break;
}
}
}
/**
* 飞机死掉,在窗口上打印信息
* @param g
* @param str
* @param size
* @param x
* @param y
* @param color
*/
public void printInfo(Graphics g,String str,int size,int x,int y,Color color){
Font font = new Font("宋体", Font.BOLD, size);
g.setFont(font);
Color c = g.getColor();
g.setColor(color);
g.drawString(str, x, y);
g.setColor(c);
}
//定义为内部类,方便使用外部类的普通属性
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
//System.out.println("按下:" + e.getKeyCode());
p.setDirectionTrue(e);
}
@Override
public void keyReleased(KeyEvent e) {
//System.out.println("释放:" + e.getKeyCode());
p.setDirectionFalse(e);
}
}
@Override
public void launchFrame() {
super.launchFrame();
addKeyListener(new KeyMonitor());//增加键盘的监听
//生成一堆字弹
for (int i = 0; i < 25; i++) {
Bullet b = new Bullet();
bulletList.add(b);
}
startTime = new Date();//游戏开始
}
public static void main(String[] args) {
new PlaneGameFrame().launchFrame();
}
}
2、S01E088-097飞机游戏实操
package com.test.util;
/**
* 游戏中关于窗口大小的常量
*/
public class Constant {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
}
package com.test.util;
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() {} //工具类通常会将构造方法私有,直接调用static方法
public static Image getImage(String path) {
URL url = GameUtil.class.getClassLoader().getResource(path);
BufferedImage img = null;
try {
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}
//
package com.test.util;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 游戏窗口类
*/
public class MyFrame extends Frame { //GUI编程:AWT,SWING等
//参考http://www.cnblogs.com/S-E-P/archive/2010/01/27/2045076.html
@Override
public void update(Graphics g) {//覆盖update方法,截取默认的调用过程
//创建图形缓冲区
Image imageBuffer = createImage(this.getWidth(),this.getHeight());
Graphics graImage = imageBuffer.getGraphics();//获取图形缓冲区的图形上下文
paint(graImage);//用paint方法中编写的绘图过程对图形缓冲区绘图
graImage.dispose();//释放图形上下文资源
g.drawImage(imageBuffer,0,0,this);//将图形缓冲区绘制到屏幕上
}
/**
* 加载窗口
*/
public void launchFrame() {
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//窗口大小
setLocation(100,100);//窗口位置
setVisible(true);//默认不可见
new PaintThread().start();//启动重画线程
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {//点击右上角"X"退出
System.exit(0);
}
});
}
/**
* 定义一个重画窗口的线程类
*/
class PaintThread extends Thread {
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.test.solar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import com.test.util.GameUtil;
public class Planet extends Star{
//除了图片、坐标。行星还沿着某个椭圆运行:长轴、短轴、速度(角度的增量)、角度。绕着某个Stars飞。
double longAxis;//椭圆的长轴
double shortAxis;//椭圆的短轴
double speed;//飞行的速度
double degree;
Star center;
boolean isSatellite;
@Override
public void draw(Graphics g) {
super.draw(g);//调用父类方法,优化代码结构
move();
if(!isSatellite){
drawTrace(g);
}
}
public void move(){
//沿着椭圆轨迹飞行
x = (center.x + center.width/2) + longAxis * Math.cos(degree) - this.width/2;
y = (center.y + center.height/2) + shortAxis * Math.sin(degree) - this.height/2;
degree += speed;
}
public void drawTrace(Graphics g){
double ovalX,ovalY,ovalWidth,ovalHeight;
ovalX = (center.x + center.width/2) - longAxis;
ovalY = (center.y + center.height/2) -shortAxis;
ovalWidth = longAxis * 2;
ovalHeight = shortAxis * 2;
Color c = g.getColor();
g.setColor(Color.blue);
g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth, (int)ovalHeight);
g.setColor(c);
}
//一开始都放在同一水平线上,通过Star可计算x,y
public Planet(Star center, String imgpath, double longAxis,
double shortAxis, double speed) {
super(GameUtil.getImage(imgpath));
this.center = center;
this.x = (center.x + center.width/2) + longAxis - this.width/2;//计算出x
this.y = (center.y + center.height/2) - this.height/2;
this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.speed = speed;
}
//一开始都放在同一水平线上,通过Star可计算x,y
public Planet(Star center, String imgpath, double longAxis,
double shortAxis, double speed,boolean isSatellite) {
this(center, imgpath, longAxis, shortAxis, speed);
this.isSatellite = isSatellite;
}
public Planet(Image img, double x, double y) {
super(img, x, y);
}
public Planet(String imgpath, double x, double y) {
super(imgpath, x, y);
}
}
///
package com.test.solar;
import java.awt.Graphics;
import java.awt.Image;
import com.test.util.GameUtil;
public class Star {
Image img;
double x,y;
int width,height;
public void draw(Graphics g){
g.drawImage(img, (int)x, (int)y, null);
}
public Star(){
}
public Star(Image img){
this.img = img;
this.width = img.getWidth(null);
this.height = img.getHeight(null);
}
public Star(Image img,double x,double y){//传图片
this(img);//构造器调用,优化代码结构
this.x = x;
this.y = y;
}
public Star(String imgpath,double x,double y){//传路径
this(GameUtil.getImage(imgpath),x,y);//构造器调用,优化代码结构
}
}
/
package com.test.solar;
import java.awt.Graphics;
import java.awt.Image;
import com.test.util.Constant;
import com.test.util.GameUtil;
import com.test.util.MyFrame;
/**
* 太阳系的主窗口,以后再研究窗口闪烁问题
*/
public class SolarFrame extends MyFrame{
Image bg = GameUtil.getImage("images/bg.jpg");
Star sun = new Star("images/sun.jpg", Constant.GAME_WIDTH/2, Constant.GAME_HEIGHT/2);
Planet earth = new Planet(sun, "images/earth.jpg", 150, 100, 0.1);
Planet moon = new Planet(earth, "images/moon.jpg", 30, 20, 0.2,true);
Planet mars = new Planet(sun, "images/mars.jpg", 200, 130, 0.2);
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
sun.draw(g);
earth.draw(g);
moon.draw(g);
mars.draw(g);
}
public static void main(String[] args){
new SolarFrame().launchFrame();
}
}