编写飞行物类
package com.tarena.shoot; import java.awt.image.BufferedImage; /** * 飞行物(敌机,蜜蜂,子弹,英雄机) */ public abstract class FlyingObject { protected int x; protected int y; protected int width; protected int height; protected BufferedImage image; protected BufferedImage[] ember; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public BufferedImage getImage() { return image; } public void setImage(BufferedImage image) { this.image = image; } /** * 检查是否出界 * @param width 边界宽 * @param height 边界高 * @return true 出界与否 */ public abstract boolean outOfBounds(); /** * 飞行物移动一步 */ public abstract void step(); /** * 检查当前飞行物体是否被子弹(x,y)击(shoot)中, * true表示击中,飞行物可以被击中 * @param Bullet 子弹对象 * @return true表示被击中了 */ public boolean shootBy(Bullet bullet){ if(bullet.isBomb()){ return false; } int x = bullet.x; //子弹横坐标 int y = bullet.y; //子弹纵坐标 boolean shoot = this.x
编写飞机类
package com.tarena.shoot; /** * 敌飞机: 是飞行物,也是敌人 */ public class Airplane extends FlyingObject implements Enemy { int speed = 2; //移动步骤 public Airplane(){ this.image = ShootGame.airplane; this.ember = ShootGame.airplaneEmber; width = image.getWidth(); height = image.getHeight(); y = -height; x = (int)(Math.random()*(ShootGame.WIDTH - width)); } @Override public int getScore() { //获取分数 return 5; } @Override public boolean outOfBounds() { //越界处理 return y>ShootGame.HEIGHT; } @Override public void step() { //移动 y += speed; } }
编写奖励接口
package com.tarena.shoot; /** * 奖励 */ public interface Award { int DOUBLE_FIRE = 0; //双倍火力 int LIFE = 1; //1条命 /** 获得奖励类型(上面的0或1) */ int getType(); }
编写蜜蜂类
package com.tarena.shoot; import java.util.Random; public class Bee extends FlyingObject implements Award{ private int xSpeed = 1; private int ySpeed = 2; private int awardType; public Bee(){ this.image = ShootGame.bee; this.ember = ShootGame.beeEmber; width = image.getWidth(); height = image.getHeight(); y = -height; Random rand = new Random(); x = rand.nextInt(ShootGame.WIDTH - width); awardType = rand.nextInt(2); //初始化时给奖励 } public int getType(){ return awardType; } @Override public boolean outOfBounds() { return y>ShootGame.HEIGHT; } @Override public void step() { //可斜飞 x += xSpeed; y += ySpeed; if(x > ShootGame.WIDTH-width){ xSpeed = -1; } if(x < 0){ xSpeed = 1; } } }
编写大飞机类
package com.tarena.shoot; import java.util.Random; public class BigPlane extends FlyingObject implements Enemy, Award{ private int xSpeed=1; private int ySpeed=1; private int life; private int awardType; public BigPlane() { life = 4; this.image = ShootGame.bigPlane; this.ember = ShootGame.bigPlaneEmber; width = image.getWidth(); height = image.getHeight(); y = -height; Random rand = new Random(); x = rand.nextInt(ShootGame.WIDTH - width); awardType = rand.nextInt(2); } public int getType() { return awardType; } @Override public int getScore() { return 50; } @Override public boolean outOfBounds() { return false; } // @Override // public BufferedImage getImage() { // Graphics g = image.getGraphics(); // g.setColor(Color.white); // g.fillRect(0, 0, 40, 30); // g.setColor(Color.black); // g.drawString("L:"+life, 10, 20); // return image; // } public EnemyBullet[] shoot() { // 发射子弹 int xStep = width / 4; // 子弹步分为飞机宽度4半 int yStep = 20; EnemyBullet[] bullets2 = new EnemyBullet[1]; bullets2[0] = new EnemyBullet(x + 2 * xStep, y - yStep); return bullets2; } @Override public void step() { //移动 x += xSpeed; y += ySpeed; if(x > ShootGame.WIDTH-width){ xSpeed = -1; } if(x < 0){ xSpeed = 1; } } public boolean shootBy(Bullet bullet) { if(super.shootBy(bullet)){ life--; } return life==0; } }
编写子弹类
package com.tarena.shoot; /** * 子弹类 */ public class Bullet extends FlyingObject { private int speed = 3; //移动的速度 private boolean bomb; public Bullet(int x,int y){ this.x = x; this.y = y; this.image = ShootGame.bullet; } public void setBomb(boolean bomb) { this.bomb = bomb; } public boolean isBomb() { return bomb; } @Override public void step(){ //移动方法 y-=speed; } @Override public boolean outOfBounds() { return y<-height; } }
编写飞机爆炸效果
package com.tarena.shoot; import java.awt.image.BufferedImage; /** * 灰烬 飞机被打掉以后的残影 * @author Robin */ public class Ember { private BufferedImage[] images={}; private int index; private int i; private BufferedImage image; private int intevel = 10; private int x,y; public Ember(FlyingObject object) { images = object.ember; image = object.image; x = object.x; y = object.y; index = 0; i = 0; } public boolean burnDown(){ i++; if(i%intevel==0){ if(index == images.length){ return true; } image = images[index++]; } return false; } public int getX() { return x; } public int getY() { return y; } public BufferedImage getImage() { return image; } }
编写敌人类
package com.tarena.shoot; /** * 敌人,可以有分数 */ public interface Enemy { //敌人的分数 int getScore(); }
编写敌机子弹类
package com.tarena.shoot; public class EnemyBullet extends FlyingObject { private int speed = 2; //移动的速度 private boolean bomb; public EnemyBullet(int x,int y){ this.x = x; this.y = y; this.image = ShootGame.EnemyBullet; } public void setBomb(boolean bomb) { this.bomb = bomb; } public boolean isBomb() { return bomb; } @Override public void step(){ //移动方法 y+=speed; } @Override public boolean outOfBounds() { return y<-height; } }
编写英雄机类
package com.tarena.shoot; import java.awt.image.BufferedImage; /** * 英雄机 * * @author Administrator * */ public class Hero extends FlyingObject { protected BufferedImage[] images = {}; protected int index = 0; //private boolean doubleFire; private int doubleFire; private int life; public Hero() { life = 3; doubleFire = 0; this.image = ShootGame.hero0; this.ember = ShootGame.heroEmber; images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; width = image.getWidth(); height = image.getHeight(); x = 150; y = 400; } public int isDoubleFire() { return doubleFire; } public void addDoubleFire(){ doubleFire = 40; } public void setDoubleFire(int doubleFire) { this.doubleFire = doubleFire; } public void addLife() { // 增命 life++; } public void subtractLife() { // 减命 life--; } public int getLife() { return life; } /** * 当前物体移动了一下,相对距离, x,y鼠标位置 */ public void moveTo(int x, int y) { this.x = x - width / 2; this.y = y - height / 2; } @Override public boolean outOfBounds() { return x < 0 || x > ShootGame.WIDTH - width || y < 0 || y > ShootGame.HEIGHT - height; } public Bullet[] shoot() { // 发射子弹 int xStep = width / 4; // 子弹步分为飞机宽度4半 int yStep = 20; if (doubleFire>0) { Bullet[] bullets = new Bullet[2]; bullets[0] = new Bullet(x + xStep, y - yStep); bullets[1] = new Bullet(x + 3 * xStep, y - yStep); doubleFire -= 2; return bullets; } else { // 单倍 Bullet[] bullets = new Bullet[1]; bullets[0] = new Bullet(x + 2 * xStep, y - yStep); // y-yStep(子弹距飞机的位置) return bullets; } } @Override public void step() { if(images.length>0){ image = images[index++/10%images.length]; } } public boolean hit(FlyingObject other) { // 碰撞算法 int x1 = other.x - this.width / 2; int x2 = other.x + other.width + this.width / 2; int y1 = other.y - this.height / 2; int y2 = other.y + other.height + this.height / 2; return this.x + this.width / 2 > x1 && this.x + this.width / 2 < x2 && this.y + this.height / 2 > y1 && this.y + this.width / 2 < y2; } public boolean hit2(EnemyBullet bb1) { int x1 = bb1.x - this.width / 2; int x2 = bb1.x + bb1.width + this.width / 2; int y1 = bb1.y - this.height / 2; int y2 = bb1.y + bb1.height + this.height / 2; int hx = this.x + this.width / 2; int hy = this.y + this.height / 2; return hx > x1 && hx < x2 && hy > y1 && hy < y2; } }
编写bgm类
package com.tarena.shoot; import java.applet.Applet; import java.applet.AudioClip; import java.io.File; import java.net.URI; import java.net.URL; import javax.swing.JFrame; public class Sound extends JFrame{ File f; URI uri; URL url; Sound(){ try { f = new File("C:\\Users\\姜涛\\Desktop\\dddd\\StudioEIM - MapleStory.wav"); uri = f.toURI(); url = uri.toURL(); //解析地址 AudioClip aau; aau = Applet.newAudioClip(url); aau.loop(); //循环播放 } catch (Exception e) { e.printStackTrace(); } } }
编写主类
package com.tarena.shoot; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import DAO.DAO; public class ShootGame extends JPanel { public static final int WIDTH = 400; // 面板宽 public static final int HEIGHT = 654; // 面板高 /** 游戏的当前状态: START RUNNING PAUSE GAME_OVER */ private int state; public static final int START = 0; public static final int RUNNING = 1; public static final int PAUSE = 2; public static final int GAME_OVER = 3; private int score = 0; // 得分 private Timer timer; // 定时器 private int intervel = 1000/100; // 时间间隔(毫秒) public static BufferedImage background; public static BufferedImage start; public static BufferedImage pause; public static BufferedImage gameover; public static BufferedImage bullet; public static BufferedImage airplane; public static BufferedImage EnemyBullet; public static BufferedImage[] airplaneEmber=new BufferedImage[4]; public static BufferedImage bee; public static BufferedImage[] beeEmber=new BufferedImage[4];; public static BufferedImage hero0; public static BufferedImage hero1; public static BufferedImage[] heroEmber=new BufferedImage[4];; public static BufferedImage bigPlane; public static BufferedImage[] bigPlaneEmber=new BufferedImage[4];; private FlyingObject[] flyings = {}; // 敌机数组 private Bullet[] bullets = {}; // 子弹数组 private EnemyBullet[] bullets2 = {};//敌机子弹数组 private Hero hero = new Hero(); // 英雄机 private Ember[] embers = {}; // 灰烬 private BigPlane bigplane=new BigPlane(); static {// 静态代码块 try { background = ImageIO.read(ShootGame.class .getResource("background.png")); bigPlane = ImageIO .read(ShootGame.class.getResource("bigplane.png")); airplane = ImageIO .read(ShootGame.class.getResource("airplane.png")); bee = ImageIO.read(ShootGame.class.getResource("bee.png")); bullet = ImageIO.read(ShootGame.class.getResource("bullet.png")); hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png")); hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png")); pause = ImageIO.read(ShootGame.class.getResource("pause.png")); EnemyBullet= ImageIO.read(ShootGame.class.getResource("EnemyBullet.png")); gameover = ImageIO .read(ShootGame.class.getResource("gameover.png")); start = ImageIO .read(ShootGame.class.getResource("start.png")); for(int i=0; i<4; i++){ beeEmber[i] = ImageIO.read( ShootGame.class.getResource("bee_ember"+i+".png")); airplaneEmber[i] = ImageIO.read( ShootGame.class.getResource("airplane_ember"+i+".png")); bigPlaneEmber[i] = ImageIO.read( ShootGame.class.getResource("bigplane_ember"+i+".png")); heroEmber[i] = ImageIO.read( ShootGame.class.getResource("hero_ember"+i+".png")); } } catch (Exception e) { e.printStackTrace(); } } @Override public void paint(Graphics g) { g.drawImage(background, 0, 0, null); // 画背景图 paintEmber(g); paintHero(g); // 画英雄机 paintBullets(g); // 画子弹 paintFlyingObjects(g); // 画飞行物 paintScore(g); // 画分数 paintState(g); // 画游戏状态 paintEnemyBullets(g);//画敌机子弹 } /** 画英雄机 */ public void paintHero(Graphics g) { g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null); } public void paintEmber(Graphics g) { for (int i = 0; i < embers.length; i++) { Ember e = embers[i]; g.drawImage(e.getImage(), e.getX(), e.getY(), null); } } /** 画子弹 */ public void paintBullets(Graphics g) { for (int i = 0; i < bullets.length; i++) { Bullet b = bullets[i]; if(! b.isBomb()) g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(), null); } } public void paintEnemyBullets(Graphics g) { for(int i=0;i
下面实现数据库的连接,首先编写将分数写入数据库的方法,在src重新建一个包,包里写写入分数的方法。
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import util.DBUtil;
public class DAO {
/*
* 增加分数的方法
*/
public static void save(int score) throws Exception{
PreparedStatement ps=null;
Connection conn = null;
try{
conn = DBUtil.getConnection();
String sql ="INSERT INTO mydb2(score) values(?)";
ps = conn.prepareStatement(sql);
ps.setInt(1,score);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
}
然后连接数据库
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { public static void main(String[]args) throws Exception{ Connection conn = DBUtil.getConnection(); System.out.println(conn); DBUtil.close(conn); } public static void close(Connection conn){ try{ if(conn != null){ conn.close(); } }catch(SQLException e){ e.printStackTrace(); } } public static Connection getConnection() throws Exception{ //System.out.println("112"); Connection conn = null; //1.注册驱动(加载驱动) Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=数据库名字","sa", "********"); //System.out.println("123"); // try{ // Class.forName("com.microsoft.sqlserver.jdbc.Driver"); // System.out.println("111"); // //2.建立连接 // conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443/mydb2","sa","the1012secret"); // System.out.println("111"); // }catch(ClassNotFoundException e){ // //System.out.println("注册驱动"); // }catch(SQLException e){ // e.printStackTrace(); // } return conn; } }