当前位置: 首页 > news >正文

vue使用于网站开发邢台高端网站建设价格

vue使用于网站开发,邢台高端网站建设价格,英雄联盟做的广告视频网站,个人网页制作总结今天给大家分享一个使用java编写的坦克大战小游戏#xff0c;整体还是挺好玩的#xff0c;通过对这款游戏的简单实现#xff0c;加深对java基础的深刻理解。 一、设计思路 1.坦克大战小游戏通过java实现#xff0c;其第一步需要先绘制每一关对应的地图#xff0c;地图包括…今天给大家分享一个使用java编写的坦克大战小游戏整体还是挺好玩的通过对这款游戏的简单实现加深对java基础的深刻理解。 一、设计思路 1.坦克大战小游戏通过java实现其第一步需要先绘制每一关对应的地图地图包括河流、草地、砖块、铁块等。 2.需要绘制玩家坦克、敌方坦克、以及坦克移动过程中使用到的碰撞算法子弹与坦克之间的碰撞 包括击中敌方坦克后的爆炸效果通过重绘实现坦克的移动以及各种道具的随机生成算法。 实际运行效果如下 二、代码实现 1.首先需要将游戏中涉及到的各种对象梳理清楚由于Java面向对象的特征可以将一些对象公共特性抽象出来比如将坦克抽象出一个超类代码如下 public abstract class Tank{int x0;int y0;int tempX0;int tempY0;int size32;int directConstant.UP;int speed1;int lives1;int frame0;//控制敌方坦克切换方向的时间boolean isAIfalse;//是否自动boolean hit;boolean isShootingfalse;boolean isDestroyedfalse;boolean isProtectedfalse;Map map;GameMain gameMain;Collision collision;Bullet bullet;int starNum0; //星星数public Tank(int size,int speed,Collision collision,Map map,GameMain gameMain) {this.sizesize;this.collisioncollision;this.mapmap;this.gameMaingameMain;this.speedspeed; }public void move() {//如果是AI坦克在一定时间或碰撞后切换方法if(this.isAI gameMain.enemyStopTime0) {return;}this.tempXthis.x;this.tempYthis.y;if(this.isAI) {this.frame;if(this.frame%1000 || this.hit) {this.direct(int)(Math.random()*4);this.hitfalse;this.frame0;}}if(this.directConstant.UP) {this.tempY-this.speed;}else if(this.directConstant.DOWN) {this.tempYthis.speed;}else if(this.directConstant.LEFT) {this.tempX-this.speed;}else if(this.directConstant.RIGHT) {this.tempXthis.speed;}isHit();if(!this.hit) {this.xthis.tempX;this.ythis.tempY;}}public void isHit() {if(this.directConstant.LEFT) {if(this.xmap.offsetX) {this.xmap.offsetX;this.hittrue;}}else if(this.directConstant.RIGHT) {if(this.xmap.offsetXmap.mapWidth-this.size) {this.xmap.offsetXmap.mapWidth-this.size;this.hittrue;}}else if(this.directConstant.UP) {if(this.ymap.offsetY) {this.ymap.offsetY;this.hittrue;}}else if(this.directConstant.DOWN) {if(this.ymap.offsetYmap.mapHeight-this.size) {this.ymap.offsetYmap.mapHeight-this.size;this.hittrue;}}if(!this.hit) {if(collision.tankMapCollision(this, map)) {this.hittrue;}}}public abstract void drawTank(Graphics2D ctx2);public void shoot(int type,Graphics2D ctx2) {if(this.isAI gameMain.enemyStopTime0) {return;}if(this.isShooting) {return;}int tempXthis.x;int tempYthis.y;this.bulletnew Bullet(tempX, tempY, this.direct, gameMain, type, map, this, collision);if(!this.isAI) {if(this.starNum0) {this.bullet.speed3;this.bullet.fireNum1;}if(this.starNum0 this.starNum3) {this.bullet.speedthis.starNum;this.bullet.fireNum(this.starNum-1);}if(this.starNum3) {this.bullet.speed3;this.bullet.fireNum3;}}if(this.directConstant.UP) {tempX this.x this.size/2 - this.bullet.size/2;tempYthis.y-this.bullet.size;}else if(this.directConstant.DOWN) {tempX this.x this.size/2 - this.bullet.size/2;tempYthis.ythis.bullet.size;}else if(this.directConstant.LEFT) {tempXthis.x-this.bullet.size;tempYthis.y this.size/2 - this.bullet.size/2;}else if(this.directConstant.RIGHT) {tempXthis.xthis.bullet.size;tempYthis.y this.size/2 - this.bullet.size/2;}this.bullet.xtempX;this.bullet.ytempY;if(!this.isAI) {//音乐Constant.executor.execute(new Runnable() {Overridepublic void run() {Constant.ATTACK_AUDIO.play();}});}this.bullet.drawBullet(ctx2);gameMain.bulletArr.add(this.bullet);this.isShootingtrue;}public void distroy() {this.isDestroyedtrue;gameMain.crackArr.add(new CrackAnimation(gameMain, Constant.CRACK_TYPE_TANK, this));if(this.isAI) {Constant.executor.execute(new Runnable() {Overridepublic void run() {Constant.TANK_DESTROY_AUDIO.play();}});gameMain.appearEnemy--;}else {Constant.executor.execute(new Runnable() {Overridepublic void run() {Constant.PLAYER_DESTROY_AUDIO.play();}});this.starNum0;}} } 2.坦克子类实现 public class PlayTank extends Tank{int protectedTime 0;//保护时间int offsetX0; //坦克2与坦克1的距离int type; //玩家类型public PlayTank(int type,Collision collision, Map map, GameMain gameMain) {super(32, 2, collision, map, gameMain);this.lives 3;//生命值this.isProtected true;//是否受保护this.protectedTime 500;//保护时间this.typetype;}Overridepublic void drawTank(Graphics2D ctx2) {this.hit false;if(this.type1) {ctx2.drawImage(Constant.RESOURCE_IMAGE, this.x, this.y,this.xthis.size,this.ythis.size,Constant.POS.get(player).xthis.offsetXthis.direct*this.size, Constant.POS.get(player).y,Constant.POS.get(player).xthis.offsetXthis.direct*this.sizethis.size, Constant.POS.get(player).ythis.size, null);}else {ctx2.drawImage(Constant.RESOURCE_IMAGE, this.x, this.y,this.xthis.size,this.ythis.size,Constant.POS.get(player).x128this.offsetXthis.direct*this.size, Constant.POS.get(player).y,Constant.POS.get(player).x128this.offsetXthis.direct*this.sizethis.size, Constant.POS.get(player).ythis.size, null);}if(this.isProtected) {int temp((500-protectedTime)/5)%2;ctx2.drawImage(Constant.RESOURCE_IMAGE, this.x, this.y,this.xthis.size,this.ythis.size,Constant.POS.get(protected).x, Constant.POS.get(protected).y32*temp,Constant.POS.get(protected).xthis.size, Constant.POS.get(protected).ythis.size, null);this.protectedTime--;if(this.protectedTime0) {this.isProtectedfalse;}}}/*** 玩家坦克复活* param player*/public void renascenc(int playerType) {this.lives--;this.directConstant.UP;this.isProtectedtrue;this.protectedTime500;this.isDestroyed false;int temp0;if(playerType1) {temp129;}else {temp256;}this.x temp map.offsetX;this.y 385 map.offsetY;}}3.通过子类继承父类实现玩家坦克和敌方坦克的创建创建完后接下来就可让坦克动起来如下 public void addEnemyTank(Graphics2D ctx2) {if(enemyArr null || enemyArr.size() maxAppearEnemy || maxEnemy 0){return;}appearEnemy;Tank objTanknull;int rand(int) (Math.random()*4);if(rand0) {objTanknew TankEnemy0(collision, map, this);}else if(rand1) {objTanknew TankEnemy1(collision, map, this);}else if(rand2) {objTanknew TankEnemy2(collision, map, this);}else if(rand3) {objTanknew TankEnemy3(collision, map, this);}enemyArr.add(objTank);this.maxEnemy--;map.clearEnemyNum(maxEnemy, appearEnemy,ctx2);} 4.在移动过程中涉及到坦克与子弹坦克与地图之间的碰撞问题实现如下 /** * 坦克与地图间的碰撞 **/ public boolean tankMapCollision(Tank tank,Map map) {//移动检测记录最后一次的移动方向根据方向判断-overlap;int tileNum0;int rowIndex0;int colIndex0;int overlap3; //允许重叠的大小//根据tank的x、y计算map的row和colif(tank.directConstant.UP) {rowIndex(tank.tempYoverlap-map.offsetY)/map.tileSize;colIndex(tank.tempXoverlap-map.offsetX)/map.tileSize;}else if(tank.directConstant.DOWN) {rowIndex(tank.tempY-overlap-map.offsetYtank.size)/map.tileSize;colIndex(tank.tempXoverlap-map.offsetX)/map.tileSize;}else if(tank.directConstant.LEFT) {rowIndex(tank.tempYoverlap-map.offsetY)/map.tileSize;colIndex(tank.tempXoverlap-map.offsetX)/map.tileSize;}else if(tank.directConstant.RIGHT){rowIndex(tank.tempYoverlap-map.offsetY)/map.tileSize;colIndex(tank.tempX-overlap-map.offsetXtank.size)/map.tileSize;}if(rowIndexmap.hTileCount || rowIndex0 || colIndexmap.wTileCount || colIndex0) {return true;}if(tank.directConstant.UP || tank.directConstant.DOWN) {int tempWidthtank.tempX-map.offsetX-colIndex*map.tileSizetank.size-overlap;if(tempWidth%map.tileSize0) {tileNumtempWidth/map.tileSize;}else {tileNumtempWidth/map.tileSize1;}for(int i0;itileNum colIndeximap.wTileCount;i) {int mapContentmap.mapLevel[rowIndex][colIndexi];if(mapContentConstant.WALL || mapContentConstant.GRID || mapContentConstant.WATER || mapContentConstant.HOME || mapContentConstant.ANOTHER_HOME) {if(tank.directConstant.UP) {tank.ymap.offsetYrowIndex*map.tileSizemap.tileSize-overlap;}else {tank.ymap.offsetYrowIndex*map.tileSize-tank.sizeoverlap;}return true;}}}else {int tempHeighttank.tempY-map.offsetY-rowIndex*map.tileSizetank.size-overlap;if(tempHeight%map.tileSize0) {tileNumtempHeight/map.tileSize;}else {tileNumtempHeight/map.tileSize1;}for(int i0;itileNum rowIndeximap.hTileCount;i) {int mapContentmap.mapLevel[rowIndexi][colIndex];if(mapContentConstant.WALL || mapContentConstant.GRID || mapContentConstant.WATER || mapContentConstant.HOME || mapContentConstant.ANOTHER_HOME) {if(tank.directConstant.LEFT) {tank.xmap.offsetXcolIndex*map.tileSizemap.tileSize-overlap;}else {tank.xmap.offsetXcolIndex*map.tileSize-tank.sizeoverlap;}return true;}}}return false;}5.绘制界面通过定时重绘实现具体代码如下 public class GameMain extends JPanel{//int enemyNum12;Map map;Num num;int level1;Level tankLevel;int gameStateConstant.GAME_STATE_MENU;private boolean isGameOverfalse;int maxEnemy 12;//敌方坦克总数int maxAppearEnemy 5;//屏幕上一起出现的最大数int appearEnemy 0; //已出现的敌方坦克int enemyStopTime0;ListBullet bulletArr;ListTank enemyArr;ListCrackAnimation crackArr;ListInteger keys;Tank player1 null;//玩家1Tank player2 null;//玩家2Collision collision;int mainframe 0;Image offScreenImage;Menu menu;Stage stage;Prop prop;Graphics2D ctx2;int overX 176;int overY 384;int propTime 300; //道具出现频次int homeProtectedTime -1;int winWaitTime80;public void initGame(GameMain gameMain) {this.numnew Num(gameMain);this.tankLevelnew Level();this.collisionnew Collision(gameMain);}public void initObject() {this.mapnew Map(this, num, tankLevel, level);player1new PlayTank(1,collision, map, this);player1.x 129 map.offsetX;player1.y 385 map.offsetY;player2 new PlayTank(2,collision, map, this);player2.x 256 map.offsetX;player2.y 385 map.offsetY;bulletArrnew ArrayList();enemyArrnew ArrayList();crackArrnew ArrayList();keysnew ArrayList();menunew Menu();stagenew Stage(level, this,this.num);this.isGameOverfalse;this.propTime400;this.homeProtectedTime -1;this.maxEnemy12;this.winWaitTime50;this.appearEnemy0;this.overY384;}public void goGameOver() {this.isGameOvertrue;}Overridepublic void paint(Graphics g) {//创建和容器一样大小的Image图片if(offScreenImagenull) {offScreenImagethis.createImage(Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);}//获得该图片的画布Graphics gImageoffScreenImage.getGraphics();//填充整个画布gImage.fillRect(0,0,Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);if(ctx2null) {ctx2(Graphics2D)gImage;}if(gameStateConstant.GAME_STATE_MENU) {menu.drawMenu(ctx2);}if(gameStateConstant.GAME_STATE_INIT) {stage.setNum(ctx2);stage.drawStage(ctx2);}if(gameStateConstant.GAME_STATE_START || gameStateConstant.GAME_STATE_OVER) {drawAll(ctx2);}if(gameStateConstant.GAME_STATE_OVER) {gameOver();}if(gameStateConstant.GAME_STATE_WIN) {this.winWaitTime--;drawAll(ctx2);if(this.winWaitTime0) {nextLevel();}}//将缓冲区绘制好的图形整个绘制到容器的画布中g.drawImage(offScreenImage,0,0,null);}public void initMap(Graphics2D ctx2) {map.setNum(ctx2);map.drawMap(ctx2);}public void drawAll(Graphics2D ctx2) {map.setNum(ctx2);map.drawMap(ctx2);if(player1.lives0) {player1.drawTank(ctx2);}if(player2.lives0) {player2.drawTank(ctx2);}if(appearEnemymaxEnemy){if(mainframe % 100 0){addEnemyTank(ctx2);mainframe 0;}mainframe;}drawLives(ctx2);drawEnemyTanks(ctx2);map.drawGrassMap(ctx2);drawBullet(ctx2);drawCrack(ctx2);keyEvent();if(propTime0){drawProp(ctx2);}else{propTime --;}if(homeProtectedTime 0){homeProtectedTime --;}else if(homeProtectedTime 0){homeProtectedTime -1;homeNoProtected(ctx2);}}public void addEnemyTank(Graphics2D ctx2) {if(enemyArr null || enemyArr.size() maxAppearEnemy || maxEnemy 0){return;}appearEnemy;Tank objTanknull;int rand(int) (Math.random()*4);if(rand0) {objTanknew TankEnemy0(collision, map, this);}else if(rand1) {objTanknew TankEnemy1(collision, map, this);}else if(rand2) {objTanknew TankEnemy2(collision, map, this);}else if(rand3) {objTanknew TankEnemy3(collision, map, this);}enemyArr.add(objTank);this.maxEnemy--;map.clearEnemyNum(maxEnemy, appearEnemy,ctx2);}public void drawEnemyTanks(Graphics2D ctx2) {if(enemyArr!null enemyArr.size()0) {IteratorTank itenemyArr.iterator();while(it.hasNext()) {Tank enemyTankit.next();if(enemyTank.isDestroyed) {it.remove();}else {enemyTank.drawTank(ctx2);}}}if(enemyStopTime 0){enemyStopTime --;}}/*** 绘制玩家生命数*/public void drawLives(Graphics2D ctx2) {map.drawLives(player1.lives, 1,ctx2);map.drawLives(player2.lives, 2,ctx2);}/*** 绘制子弹* param ctx2*/public void drawBullet(Graphics2D ctx2) {if(bulletArr ! null bulletArr.size() 0){IteratorBullet itbulletArr.iterator();while(it.hasNext()) {Bullet bulletit.next();if(bullet.isDestroyed) {it.remove();bullet.owner.isShootingfalse;}else {//绘制子弹bullet.drawBullet(ctx2);}}}}public void drawCrack(Graphics2D ctx2) {if(crackArr ! null crackArr.size() 0){IteratorCrackAnimation itcrackArr.iterator();while(it.hasNext()) {CrackAnimation crackAnimationit.next();if(crackAnimation.isOver) {it.remove();if(crackAnimation.crackObj instanceof Tank) {Tank tank(Tank)crackAnimation.crackObj;if(tankplayer1) {PlayTank playerTank1(PlayTank)player1;playerTank1.renascenc(1);}else if(tankplayer2) {PlayTank playerTank2(PlayTank)player2;playerTank2.renascenc(2);}}}else {//绘制爆炸效果crackAnimation.draw(ctx2);}}}}public void drawProp(Graphics2D ctx2) {double randMath.random();if(rand0.4 propnull) {propnew Prop(this, map, collision);prop.initProp();}if(prop!null) {prop.drawProp(ctx2);if(prop.isDestroyed) {propnull;propTime600;}}}public void nextLevel() {level;if(level17) {level1;}int oldPlayerNummenu.playNum;initObject();menu.playNum oldPlayerNum;//只有一个玩家if(menu.playNum 1){player2.lives 0;}map.first0;stage.init(level);gameState Constant.GAME_STATE_INIT;}public void preLevel() {level--;if(level 0){level 16;}//保存玩家数int oldPlayerNum menu.playNum;initObject();menu.playNum oldPlayerNum;//只有一个玩家if(menu.playNum 1){player2.lives 0;}stage.init(level);gameState Constant.GAME_STATE_INIT;}public void gameLoop() {switch (gameState) {case Constant.GAME_STATE_MENU:repaint();break;case Constant.GAME_STATE_INIT://stage.draw();if(stage.isReady true){gameState Constant.GAME_STATE_START;}repaint();break;case Constant.GAME_STATE_START://drawAll();if(isGameOver ||(player1.lives 0 player2.lives 0)){gameState Constant.GAME_STATE_OVER;map.homeHit(ctx2);Constant.executor.execute(new Runnable() {Overridepublic void run() {Constant.PLAYER_DESTROY_AUDIO.play();}});}if(appearEnemy maxEnemy enemyArr.size() 0){gameState Constant.GAME_STATE_WIN;}repaint();break;case Constant.GAME_STATE_WIN:repaint();break;case Constant.GAME_STATE_OVER:repaint();break;}}public void gameOver() {//.clearRect(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);ctx2.drawImage(Constant.RESOURCE_IMAGE, overXmap.offsetX, overYmap.offsetY,overXmap.offsetX64,overYmap.offsetY32,Constant.POS.get(over).x, Constant.POS.get(over).y,Constant.POS.get(over).x64, Constant.POS.get(over).y32, null);overY-2;if(overYmap.mapHeight/2) {initObject();//if(menu.playNum1) {// player2.lives0;//}gameState Constant.GAME_STATE_MENU;}}public void action() {KeyAdapter lnew KeyAdapter() {Overridepublic void keyPressed(KeyEvent e) {switch (gameState) {case Constant.GAME_STATE_MENU:if(e.getKeyCode()KeyEvent.VK_ENTER){gameState Constant.GAME_STATE_INIT;//只有一个玩家if(menu.playNum1) {player2.lives0;}}else {int n0;if(e.getKeyCode()KeyEvent.VK_DOWN) {n1;}else if(e.getKeyCode()KeyEvent.VK_UP) {n-1;}menu.next(n);}break;case Constant.GAME_STATE_START:if(!keys.contains(e.getKeyCode())){keys.add(e.getKeyCode());}//射击if(e.getKeyCode()KeyEvent.VK_SPACE player1.lives 0){player1.shoot(Constant.BULLET_TYPE_PLAYER, ctx2);}else if(e.getKeyCode()KeyEvent.VK_ENTER player2.lives 0) {player2.shoot(Constant.BULLET_TYPE_PLAYER, ctx2);}else if(e.getKeyCode()KeyEvent.VK_N) { //下一关nextLevel();}else if(e.getKeyCode() KeyEvent.VK_P) {preLevel();}break;}}Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub//super.keyReleased(e);if(keys ! null keys.size() 0){IteratorInteger itkeys.iterator();while(it.hasNext()) {Integer keyit.next();if(key.intValue()e.getKeyCode()) {it.remove();break;}}}}Overridepublic void keyTyped(KeyEvent e) {//super.keyTyped(e);}};this.addKeyListener(l);this.setFocusable(true);Timer timernew Timer();int interval20;timer.schedule(new TimerTask() {Overridepublic void run() {gameLoop(); }}, interval, interval);}public void keyEvent() {if(keys.contains(KeyEvent.VK_W)){player1.direct Constant.UP;player1.hit false;player1.move();}else if(keys.contains(KeyEvent.VK_S)){player1.direct Constant.DOWN;player1.hit false;player1.move();}else if(keys.contains(KeyEvent.VK_A)){player1.direct Constant.LEFT;player1.hit false;player1.move();}else if(keys.contains(KeyEvent.VK_D)){player1.direct Constant.RIGHT;player1.hit false;player1.move();}if(keys.contains(KeyEvent.VK_UP)){player2.direct Constant.UP;player2.hit false;player2.move();}else if(keys.contains(KeyEvent.VK_DOWN)){player2.direct Constant.DOWN;player2.hit false;player2.move();}else if(keys.contains(KeyEvent.VK_LEFT)){player2.direct Constant.LEFT;player2.hit false;player2.move();}else if(keys.contains(KeyEvent.VK_RIGHT)){player2.direct Constant.RIGHT;player2.hit false;player2.move();}}public void homeNoProtected(Graphics2D ctx2) {ListInteger[] mapChangeIndexListnew ArrayListInteger[]();mapChangeIndexList.add(new Integer[] {23,11});mapChangeIndexList.add(new Integer[] {23,12});mapChangeIndexList.add(new Integer[] {23,13});mapChangeIndexList.add(new Integer[] {23,14});mapChangeIndexList.add(new Integer[] {24,11});mapChangeIndexList.add(new Integer[] {24,14});mapChangeIndexList.add(new Integer[] {25,11});mapChangeIndexList.add(new Integer[] {25,14});map.updateMap(mapChangeIndexList,Constant.WALL,ctx2);}public static void main(String[] args) {JFrame jfnew JFrame();jf.setTitle(坦克大战);jf.setSize(Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);GameMain gameMainnew GameMain();jf.add(gameMain);jf.setLocationRelativeTo(null);jf.setResizable(false);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true);gameMain.initGame(gameMain);gameMain.initObject();gameMain.action();} } 接下来就可运行了。 其中切换到下一关按键盘N上一个按P空格键射击。 有兴趣的可以试一试。 下载地址 坦克大战小游戏完整源码
http://www.dnsts.com.cn/news/221902.html

相关文章:

  • 临沂百度网站推广公司建一个网站吗
  • 北京网站建设价位看装修案例的网站
  • 也买酒技术网站建设访问数据库的网站开发语言
  • 东莞网站推广大全建筑木工模板包工价格
  • 怎么打开google网站广西建设厅官网证件查询
  • jsp项目个人网站开发企业网站优化设计的含义
  • 吉林市教做网站it服务外包公司有哪些
  • 如何进行网页设计和网站制作哪里可以下企业网站模板
  • 网站开发怎么样?湖南省住房与城乡建设厅网站官网
  • 门户网站 特点网站开发与应用案例教程
  • 企业网站的优缺点外部链接链轮的建设对于网站提
  • 优质的做pc端网站网站 排版模板
  • 如何防止网站被注册html5网站开发案例视频
  • 电子商城网站制作什么叫高端网站定制
  • 宁波优质网站制作哪家好公众号小程序注册
  • 电商网站成本微网站 案例
  • 免费个人二级域名网站手机软件应用市场
  • 类似聚划算的网站怎么建设哪里查询网站备案
  • 企业商务网站优化织梦网站如何播放mp4
  • 在线超级外链工具公众号seo排名优化
  • 把网站做成手机版小米手机的网站架构
  • 文昌网站建设全包wordpress查询量过大
  • 网站设计器大型网站常见问题
  • 有哪些学校的网站做的好网站标题和描述优化
  • 自己做网站接入微信和支付宝3d虚拟人物制作软件
  • 聚美优品网站建设的特点wordpress的简单介绍
  • 个人主页的英文湘潭有实力seo优化
  • 站长推广工具我想做卖鱼苗网站怎样做
  • 响应式网站开发步骤怎么开发属于自己的app软件
  • 深圳福田特价网站建设免费行情网站大全