编写飞行物类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
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<x && x< this .x+width && this .y<y && y< this .y+height; if (shoot){ bullet.setbomb( true ); } return shoot; }
} |
编写飞机类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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; }
} |
编写奖励接口
1 2 3 4 5 6 7 8 9 10 |
package com.tarena.shoot; /** * 奖励 */ public interface award { int double_fire = 0 ; //双倍火力 int life = 1 ; //1条命 /** 获得奖励类型(上面的0或1) */ int gettype(); } |
编写蜜蜂类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 ; } } } |
编写大飞机类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
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 ; } } |
编写子弹类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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; }
} |
编写飞机爆炸效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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; } } |
编写敌人类
1 2 3 4 5 6 7 8 9 |
package com.tarena.shoot;
/** * 敌人,可以有分数 */ public interface enemy { //敌人的分数 int getscore(); } |
编写敌机子弹类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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; }
} |
编写英雄机类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
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类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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(); } } } |
编写主类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
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<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 ); } }
/** 画分数 */ 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); // 设置字体 g.drawstring( "score:" + score, x, y); // 画分数 y+= 20 ; g.drawstring( "life:" + hero.getlife(), x, y); }
/** 画游戏状态 */ 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); // 默认关闭操作 frame.seticonimage( new imageicon( "images/icon.jpg" ).getimage()); // 设置窗体的图标 frame.setlocationrelativeto( null ); // 设置窗体初始位置 frame.setvisible( true ); // 尽快调用paint
game.action(); // 启动执行 }
public void action() { // 启动执行代码 // 鼠标监听事件 mouseadapter l = new mouseadapter() { @override public void mousemoved(mouseevent e) { // 鼠标移动 if (state == running) { // 运行时移动英雄机 int x = e.getx(); int y = e.gety(); hero.moveto(x, y); } }
@override public void mouseentered(mouseevent e) { // 鼠标进入 if (state == pause) { // 暂停时运行 state = running; } }
@override public void mouseexited(mouseevent e) { // 鼠标退出 if (state != game_over) { state = pause; // 游戏未结束,则设置其为暂停 } }
@override public void mouseclicked(mouseevent e) { // 鼠标点击 switch (state) { case start: state = running; break ; case game_over: // 游戏结束,清理现场 flyings = new flyingobject[ 0 ]; bullets = new bullet[ 0 ]; hero = new hero(); score = 0 ; state = start; break ; } } }; this .addmouselistener(l); // 处理鼠标点击操作 this .addmousemotionlistener(l); // 处理鼠标滑动操作
timer = new timer(); // 主流程控制 timer.schedule( new timertask() { @override public void run() { if (state == running) { enteraction(); // 飞行物入场 stepaction(); // 走一步 shootaction(); // 射击 bangaction(); // 子弹打飞行物 outofboundsaction(); // 删除越界飞行物及子弹 checkgameoveraction(); // 检查游戏结束 emberaction(); } repaint(); // 重绘,调用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 ; // 飞行物入场计数
/** 飞行物入场 */ 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 ; // 射击计数
/** 射击 */ public void shootaction() { shootindex++; if (shootindex % 30 == 0 ) { // 100毫秒发一颗 bullet[] bs = hero.shoot(); // 英雄打出子弹 bullets = arrays.copyof(bullets, bullets.length + bs.length); // 扩容 system.arraycopy(bs, 0 , bullets, bullets.length - bs.length,bs.length); // 追加数组 } if (shootindex % 100 == 0 ) { // 大boss发射子弹 for ( int i = 0 ; i < flyings.length; i++) { if (flyings[i] instanceof bigplane) { // 大boss发射子弹 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毫秒发一颗 // // enemybullet[] bs2 =big.shoot(); // bullets = arrays.copyof(bullets, bullets.length + bs2.length); // 扩容 // system.arraycopy(bs2, 0, bullets, bullets.length - bs2.length, // bs2.length); // 追加数组 // } //} /** 子弹与飞行物碰撞检测 */ 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); // 将不越界的子弹留着 }
/** 检查游戏结束 */ public void checkgameoveraction() { if (isgameover()) { try { dao.save(score); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } state = game_over; // 改变状态 } }
/** 检查游戏是否结束 */ 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; // 强制类型转换 score += e.getscore(); // 加分 }
if (one instanceof award) { // 若为奖励,设置奖励 award a = (award) one; int type = a.gettype(); // 获取奖励类型 switch (type) { case award.double_fire: hero.adddoublefire(); // 设置双倍火力 break ; case award.life: hero.addlife(); // 设置加命 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(); } } } |
下面实现数据库的连接,首先编写将分数写入数据库的方法,在src重新建一个包,包里写写入分数的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<code class = "language-java" > 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); } }
}</code> |
然后连接数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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; } } |
原文链接:https://blog.csdn.net/qq_41530545/article/details/81024279
查看更多关于Java实现飞机大战-连接数据库并把得分写入数据库的详细内容...