飞机小游戏几乎是每一位java初学者都会写到的小游戏项目,他很好的将Java的基础知识进行了训练,包括继承,封装,属性方法,静态的方法,所以很有纪念价值,今天我们就来看一看这个小游戏。
产生父类。
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
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() {
super();
// TODO Auto-generated constructor stub
}
public Rectangle getRect(){
return new Rectangle((int)x,(int)y,width,height);
}
}
产生飞机继承类;
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(right){
x +=speed;
}if(up){
y-=speed;
}if(down){
y+=speed;
}else{
}
}
}
//是否存活控制
public Plane(boolean live) {
super();
this.live = live;
}
public Plane() {
super();
}
public Plane(Image img,double x,double y){
this.img=img;
this.x=x;
this.y=y;
this.speed=10;
this.width=img.getWidth(null);
this.height=img.getHeight(null);
}
//按键控制
public void addDirection(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
left=true;break;
case KeyEvent.VK_RIGHT:
right=true;break;
case KeyEvent.VK_UP:
up=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_RIGHT:
right=false;break;
case KeyEvent.VK_UP:
up=false;break;
case KeyEvent.VK_DOWN:
down=false;
}
}
}
产生子弹的类
import java.awt.Color;
import java.awt.Graphics;
public class Shell extends GameObject{
double degree;
public Shell(){
x=200;
y=200;
width=10;
height=10;
speed=3;
degree=Math.random()Math.PI2;
}
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;
}
if(y<30||y>Constant.GAME_HEIGHT-height){
degree=-degree;
}
g.setColor©;
}
}
创建加载图片的IO流的工具包
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(){
}
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;
}
}
产生我们飞机和子弹的碰撞的爆炸效果,
就是对爆炸图片的连续夹杂。
import java.awt.Graphics;
import java.awt.Image;
public class Explode {
double x,y;
static Image[]imgs=new Image[16];
static {
for(int i=1;i<=16;i++){
imgs[i-1]=GameUtil.getImage(“images/explode/e”+i+".gif");
imgs[i-1].getWidth(null);
}
}
int count;
int num;
public void draw(Graphics g){
if(count<=15){
g.drawImage(imgs[count], (int)x, (int)y, null);
num++;
if(num==2){
num=0;
count++;
}
}
}
public Explode (double x,double y) {
this.x=x;
this.y=y;
}
}
画对话框我们需要一个大小固定不变的对话框。
public class Constant {
public static final int GAME_WIDTH=800;
public static final int GAME_HEIGHT=800;
}
加载主程序,程序的入口即以及产生游戏物体。
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;
public class MainProject extends Frame{
Image planeImg=GameUtil.getImage(“images/plane.png”);
Image bg=GameUtil.getImage(“images/bg.jpg”);
Plane plane=new Plane(planeImg,250,250);
Shell[] shells=new Shell[100];
Explode bao;
Date startTime=new Date();
Date endTime;
int period;
private Image offScreenImage=null;
public void paint(Graphics g){
// if(offScreenImage==null){
// offScreenImage=this.createImage(500,500);
// }
Color c=g.getColor();
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);
}
bao.draw(g);
if(!plane.live){
g.setColor(color.red );
Font f=new Font(“仿宋体”, Font.BOLD, 50);
g.setFont(f);
g.drawString(“时间”+period+“秒”, (int)plane.x, (int)plane.y);
}
}
g.setColor©;
}
}
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
plane.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
private void launchFrame() {
// TODO Auto-generated method stub
this.setTitle(“JOJO程序员作品”);
this.setVisible(true);
this.setSize(Constant.GAME_HEIGHT, Constant.GAME_HEIGHT);
this.setLocation(300,300);
this.addWindowListener(new WindowAdapter(){
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) {
MainProject f=new MainProject();
f.launchFrame();
}
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);
}
}
这样一个简单的小游戏完成了
他可以考察初学者是否掌握了java的简单基础,
祝每位java的学习者能有所收获。
(飞机的图片和爆炸的图片没有上传,
)