Java實現(xiàn)飛機大戰(zhàn)-連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫
更新時間:2018年07月16日 08:21:11 作者:南喬代碼Cool
這篇文章給大家分享了Java實現(xiàn)飛機大戰(zhàn)中連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫的相關(guān)知識點和代碼,有興趣的可以學(xué)習(xí)參考下。
編寫飛行物類
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();
/**
* 檢查當(dāng)前飛行物體是否被子彈(x,y)擊(shoot)中,
* true表示擊中,飛行物可以被擊中
* @param Bullet 子彈對象
* @return true表示被擊中了
*/
public boolean shootBy(Bullet bullet){
if(bullet.isBomb()){
return false;
}
int x = bullet.x; //子彈橫坐標(biāo)
int y = bullet.y; //子彈縱坐標(biāo)
boolean shoot = this.x<x && x<this.x+width && this.y<y && y<this.y+height;
if(shoot){
bullet.setBomb(true);
}
return shoot;
}
}
編寫飛機類
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() { //獲取分?jǐn)?shù)
return 5;
}
@Override
public boolean outOfBounds() { //越界處理
return y>ShootGame.HEIGHT;
}
@Override
public void step() { //移動
y += speed;
}
}
編寫?yīng)剟罱涌?/p>
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() { // 發(fā)射子彈
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;
/**
* 敵人,可以有分?jǐn)?shù)
*/
public interface Enemy {
//敵人的分?jǐn)?shù)
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;
}
/**
* 當(dāng)前物體移動了一下,相對距離, x,y鼠標(biāo)位置
*/
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() { // 發(fā)射子彈
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(); //循環(huán)播放
} 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; // 面板高
/** 游戲的當(dāng)前狀態(tài): 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 = {}; // 敵機數(shù)組
private Bullet[] bullets = {}; // 子彈數(shù)組
private EnemyBullet[] bullets2 = {};//敵機子彈數(shù)組
private Hero hero = new Hero(); // 英雄機
private Ember[] embers = {}; // 灰燼
private BigPlane bigplane=new BigPlane();
static {// 靜態(tài)代碼塊
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); // 畫分?jǐn)?shù)
paintState(g); // 畫游戲狀態(tài)
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<bullets2.length;i++) {
EnemyBullet e=bullets2[i];
if(! e.isBomb()) {
g.drawImage(e.getImage(),e.getX() - e.getWidth() / 2, e.getY(),null);
}
}
}
/** 畫飛行物 */
public void paintFlyingObjects(Graphics g) {
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
g.drawImage(f.getImage(), f.getX(), f.getY(), null);
}
}
/** 畫分?jǐn)?shù) */
public void paintScore(Graphics g) {
int x = 10;
int y = 25;
Font font = new Font(Font.SANS_SERIF,Font.BOLD, 14);
g.setColor(new Color(0xFFFFFF));
g.setFont(font); // 設(shè)置字體
g.drawString("SCORE:" + score, x, y); // 畫分?jǐn)?shù)
y+=20;
g.drawString("LIFE:" + hero.getLife(), x, y);
}
/** 畫游戲狀態(tài) */
public void paintState(Graphics g) {
switch (state) {
case START:
g.drawImage(start, 0, 0, null);
break;
case PAUSE:
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER:
g.drawImage(gameover, 0, 0, null);
break;
}
}
public static void main(String[] args) {
new Sound();
JFrame frame = new JFrame("Shoot Game");
ShootGame game = new ShootGame(); // 面板對象
frame.add(game); // 將面板添加到JFrame中
frame.setSize(WIDTH, HEIGHT); // 大小
frame.setAlwaysOnTop(true); // 其總在最上
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默認(rèn)關(guān)閉操作
frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); // 設(shè)置窗體的圖標(biāo)
frame.setLocationRelativeTo(null); // 設(shè)置窗體初始位置
frame.setVisible(true); // 盡快調(diào)用paint
game.action(); // 啟動執(zhí)行
}
public void action() { // 啟動執(zhí)行代碼
// 鼠標(biāo)監(jiān)聽事件
MouseAdapter l = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) { // 鼠標(biāo)移動
if (state == RUNNING) { // 運行時移動英雄機
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
@Override
public void mouseEntered(MouseEvent e) { // 鼠標(biāo)進入
if (state == PAUSE) { // 暫停時運行
state = RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) { // 鼠標(biāo)退出
if (state != GAME_OVER) {
state = PAUSE; // 游戲未結(jié)束,則設(shè)置其為暫停
}
}
@Override
public void mouseClicked(MouseEvent e) { // 鼠標(biāo)點擊
switch (state) {
case START:
state = RUNNING;
break;
case GAME_OVER: // 游戲結(jié)束,清理現(xiàn)場
flyings = new FlyingObject[0];
bullets = new Bullet[0];
hero = new Hero();
score = 0;
state = START;
break;
}
}
};
this.addMouseListener(l); // 處理鼠標(biāo)點擊操作
this.addMouseMotionListener(l); // 處理鼠標(biāo)滑動操作
timer = new Timer(); // 主流程控制
timer.schedule(new TimerTask() {
@Override
public void run() {
if (state == RUNNING) {
enterAction(); // 飛行物入場
stepAction(); // 走一步
shootAction(); // 射擊
bangAction(); // 子彈打飛行物
outOfBoundsAction(); // 刪除越界飛行物及子彈
checkGameOverAction(); // 檢查游戲結(jié)束
emberAction();
}
repaint(); // 重繪,調(diào)用paint()方法
}
}, intervel, intervel);
}
private void emberAction() {
Ember[] live = new Ember[embers.length];
int index = 0;
for (int i = 0; i < embers.length; i++) {
Ember ember = embers[i];
if(! ember.burnDown()){
live[index++]=ember;
}
}
embers = Arrays.copyOf(live, index);
}
int flyEnteredIndex = 0; // 飛行物入場計數(shù)
/** 飛行物入場 */
public void enterAction() {
flyEnteredIndex++;
if (flyEnteredIndex % 40 == 0) { // 300毫秒--10*30
FlyingObject obj = nextOne(); // 隨機生成一個飛行物
flyings = Arrays.copyOf(flyings, flyings.length + 1);
flyings[flyings.length - 1] = obj;
}
}
public void stepAction() {
/** 飛行物走一步 */
for (int i = 0; i < flyings.length; i++) { // 飛行物走一步
FlyingObject f = flyings[i];
f.step();
}
/** 子彈走一步 */
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
b.step();
}
hero.step();
for (int i = 0; i < bullets2.length; i++) {
EnemyBullet c = bullets2[i];
c.step();
}
}
int shootIndex = 0; // 射擊計數(shù)
/** 射擊 */
public void shootAction() {
shootIndex++;
if (shootIndex % 30 == 0) { // 100毫秒發(fā)一顆
Bullet[] bs = hero.shoot(); // 英雄打出子彈
bullets = Arrays.copyOf(bullets, bullets.length + bs.length); // 擴容
System.arraycopy(bs, 0, bullets, bullets.length - bs.length,bs.length); // 追加數(shù)組
}
if (shootIndex % 100 == 0) {// 大Boss發(fā)射子彈
for (int i = 0; i < flyings.length; i++) {
if (flyings[i] instanceof BigPlane) {// 大Boss發(fā)射子彈
BigPlane boss = (BigPlane) flyings[i];
EnemyBullet[] b = boss.shoot();
bullets2 = Arrays.copyOf(bullets2, bullets2.length + b.length);
System.arraycopy(b, 0, bullets2, bullets2.length - b.length, b.length);
}
}
}
}
// if (shootIndex % 30 == 0) { // 100毫秒發(fā)一顆
//
// EnemyBullet[] bs2 =big.shoot();
// bullets = Arrays.copyOf(bullets, bullets.length + bs2.length); // 擴容
// System.arraycopy(bs2, 0, bullets, bullets.length - bs2.length,
// bs2.length); // 追加數(shù)組
// }
//}
/** 子彈與飛行物碰撞檢測 */
public void bangAction() {
for (int i = 0; i < bullets.length; i++) { // 遍歷所有子彈
Bullet b = bullets[i];
bang(b);
}
}
/** 刪除越界飛行物及子彈 */
public void outOfBoundsAction() {
int index = 0;
FlyingObject[] flyingLives = new FlyingObject[flyings.length]; // 活著的飛行物
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (!f.outOfBounds()) {
flyingLives[index++] = f; // 不越界的留著
}
}
flyings = Arrays.copyOf(flyingLives, index); // 將不越界的飛行物都留著
index = 0; // 重置為0
Bullet[] bulletLives = new Bullet[bullets.length];
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
if (!b.outOfBounds()) {
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives, index); // 將不越界的子彈留著
}
/** 檢查游戲結(jié)束 */
public void checkGameOverAction() {
if (isGameOver()) {
try {
DAO.save(score);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
state = GAME_OVER; // 改變狀態(tài)
}
}
/** 檢查游戲是否結(jié)束 */
public boolean isGameOver() {
int index = -1;
for (int i = 0; i < flyings.length; i++) {
FlyingObject obj = flyings[i];
if (hero.hit(obj)) { // 檢查英雄機與飛行物是否碰撞
hero.subtractLife();
hero.setDoubleFire(0);
index = i;
Ember ember = new Ember(hero);
embers = Arrays.copyOf(embers, embers.length+1);
embers[embers.length-1]=ember;
}
}
if(index!=-1){
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length-1];
flyings[flyings.length-1] = t;
flyings = Arrays.copyOf(flyings, flyings.length-1);
Ember ember = new Ember(t);
embers = Arrays.copyOf(embers, embers.length+1);
embers[embers.length-1]=ember;
}
for (int i = 0; i < bullets2.length; i++) {
EnemyBullet bb = bullets2[i]; // 獲取每一個敵人對象
if (hero.hit2(bb)) { // 返回true,英雄機與敵人相撞了
hero.subtractLife(); // 英雄機減命
hero.setDoubleFire(0); // 英雄機火力值為0,單倍火力
// 碰撞后敵人的后果是消失
EnemyBullet bb1 = bullets2[i];
bullets2[i] = bullets2[bullets2.length - 1];
bullets2[bullets2.length - 1] = bb1;
bullets2 = Arrays.copyOf(bullets2, bullets2.length - 1);
}
}
return hero.getLife() <= 0;
}
/** 子彈和飛行物之間的碰撞檢查 */
public void bang(Bullet bullet) {
int index = -1; // 擊中的飛行物索引
for (int i = 0; i < flyings.length; i++) {
FlyingObject obj = flyings[i];
if (obj.shootBy(bullet)) { // 判斷是否擊中
index = i; // 記錄被擊中的飛行物的索引
break;
}
}
if (index != -1) { // 有擊中的飛行物
FlyingObject one = flyings[index]; // 記錄被擊中的飛行物
FlyingObject temp = flyings[index]; // 被擊中的飛行物與最后一個飛行物交換
flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = temp;
flyings = Arrays.copyOf(flyings, flyings.length - 1); // 刪除最后一個飛行物(即被擊中的)
// 檢查one的類型 如果是敵人, 就算分
if (one instanceof Enemy) { // 檢查類型,是敵人,則加分
Enemy e = (Enemy) one; // 強制類型轉(zhuǎn)換
score += e.getScore(); // 加分
}
if (one instanceof Award) { // 若為獎勵,設(shè)置獎勵
Award a = (Award) one;
int type = a.getType(); // 獲取獎勵類型
switch (type) {
case Award.DOUBLE_FIRE:
hero.addDoubleFire(); // 設(shè)置雙倍火力
break;
case Award.LIFE:
hero.addLife(); // 設(shè)置加命
break;
}
}
//飛行物變成灰燼
Ember ember = new Ember(one);
embers = Arrays.copyOf(embers, embers.length+1);
embers[embers.length-1]=ember;
}
}
/**
* 隨機生成飛行物
*
* @return 飛行物對象
*/
public static FlyingObject nextOne() {
Random random = new Random();
int type = random.nextInt(20); // [0,4)
if (type==0) {
return new Bee();
}else if(type<=2){
return new BigPlane();
}else{
return new Airplane();
}
}
}
下面實現(xiàn)數(shù)據(jù)庫的連接,首先編寫將分?jǐn)?shù)寫入數(shù)據(jù)庫的方法,在src重新建一個包,包里寫寫入分?jǐn)?shù)的方法。
<code class="language-java">package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import util.DBUtil;
public class DAO {
/*
* 增加分?jǐn)?shù)的方法
*/
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);
}
}
}</code>
然后連接數(shù)據(jù)庫
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.注冊驅(qū)動(加載驅(qū)動)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=數(shù)據(jù)庫名字","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("注冊驅(qū)動");
// }catch(SQLException e){
// e.printStackTrace();
// }
return conn;
}
}
相關(guān)文章
spring框架cacheAnnotation緩存注釋聲明解析
這篇文章主要介紹了spring框架中cacheAnnotation注釋聲明緩存解析示例有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
Spring Boot實現(xiàn)功能的統(tǒng)一詳解
這篇文章主要介紹了Spring Boot統(tǒng)一功能的處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
springboot 整合fluent mybatis的過程,看這篇夠了
這篇文章主要介紹了springboot 整合fluent mybatis的過程,配置數(shù)據(jù)庫連接創(chuàng)建數(shù)據(jù)庫的詳細(xì)代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

