'Java Mini Game Implementation': Tank Battle (Continued 3)
Blog post "Java Mini Game Implementation": In Tank Battle (continued 2), it has been realized that tanks can fire bullets and hit enemy tanks. This blog post continues to implement more functions on this basis.
Complete Feature: Add Explosion Effect
In the previous version, when we hit the enemy tank, the enemy tank just disappeared without producing an effect similar to the explosion we are familiar with. . Next we add this explosion effect.
<code class="hljs java"> public class Explode { //爆炸所在的位置 private int x; private int y; //爆炸图片的直径 private int[] diameter={6,20,40,60,20,7}; //标识爆炸对象是否存活的属性 private boolean live =true; public boolean isLive() { return live; } //标识爆炸显示到第几步了 private int step = 0; private TankClient tc; public Explode(int x ,int y , TankClient tc){ this.x = x; this.y = y; this.tc = tc; } /* * 让爆炸显示出来 * */ public void draw(Graphics g){ if(!live) return; //判断显示到第几步了,如果全部显示完了,则此对象已死,返回 if(step>=diameter.length){ live = false; step = 0; return; } Color c = g.getColor(); g.setColor(Color.YELLOW); g.fillOval(x, y, diameter[step], diameter[step]); g.setColor(c); step++; } }</code>
<code class="hljs java"> private Explode explode = new Explode(200,200,this); @Override public void paint(Graphics g) { //炸弹对象 explode.draw(g); } </code>
<code>与子弹类似,在TankClient类中加入集合 将集合中的爆炸逐一画出(如果死去就去除) </code>
<code class="hljs cs"> /* * 为每个被击中的坦克添加一个爆炸对象 * */ private List<explode> explodes = new ArrayList<explode>(); @Override public void paint(Graphics g) { //炸弹对象 for(int i=0;i<explodes.size();i++){ code="" e="explodes.get(i);" explode=""></explodes.size();i++){></explode></explode></code>
<code class="hljs cs"><code>在Missile类中hitTank方法中添加相关代码 </code></code>
<code class="hljs cs"><code class="hljs java"> public boolean hitTank(Tank t){ //先判断该坦克是否还是存活,如果已经死了,子弹就不打他了 if(!t.isLive()){ return false; } if(this.getRect().intersects(t.getRect())){//判断是否有碰撞 //碰撞之后,子弹和该坦克就应该都死了 this.live = false;//子弹死了 t.setLive(false);//坦克死了 Explode e = new Explode(x,y,tc); tc.getExplodes().add(e); return true; } else{ return false; } } </code></code>
<code class="hljs cs"><code class="hljs cs"> /* * 函数功能:产生敌方坦克 * */ public void produceTank(){ int totalNum =r.nextInt(4)+3 ; for(int i=0;i<totalnum;i++){ code="" enemy="new" int="" tank="" x="(r.nextInt(10)+1)*40;" y="(r.nextInt(10)+1)*30;"></totalnum;i++){></code></code>
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"> @Override public void paint(Graphics g) { //直接调用坦克类的draw方法 tk.draw(g); /* * 将敌方坦克也画出来,如果没有了敌方坦克,则产生一定数量的地方坦克 * */ if(enemyTanks.size()==0){ this.produceTank(); } for(int i=0;i<enemytanks.size();i++){ code="" e="explodes.get(i);" enemy="enemyTanks.get(i);" explode="" i="0;i<explodes.size();i++) {" int="" missile="" ms="missiles.get(i);" tank=""></enemytanks.size();i++){></code></code></code>
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"> public boolean hitTank(Tank t){ //先判断该坦克是否还是存活,如果已经死了,子弹就不打他了 if(!t.isLive()){ return false; } if(this.getRect().intersects(t.getRect())){//判断是否有碰撞 //碰撞之后,子弹和该坦克就应该都死了 this.live = false;//子弹死了 t.setLive(false);//坦克死了 Explode e = new Explode(x,y,tc); tc.getExplodes().add(e); return true; } else{ return false; } } /* * 一颗子弹打List中的坦克 * */ public boolean hitTanks(List<tank> tanks){ for(int i=0;i<tanks.size();i++){ code="" return=""></tanks.size();i++){></tank></code></code></code></code>
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"><code class="hljs java"> public Tank(int x, int y,boolean good,Direction dir, TankClient tc) { this(x,y,good); this.dir = dir; this.tc = tc; }</code></code></code></code></code>
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"><code class="hljs cs"> /* * 函数功能:产生敌方坦克 * */ public void produceTank(){ int totalNum =r.nextInt(4)+3 ; for(int i=0;i<totalnum;i++){ code="" dir="dirs[rn];" direction="" dirs="Direction.values();" enemy="new" int="" rn="r.nextInt(dirs.length);" tank="" x="(r.nextInt(10)+1)*40;" y="(r.nextInt(10)+1)*30;"></totalnum;i++){></code></code></code></code></code>
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"> <code class="hljs java"><code class="hljs cs"><code class="hljs cs"> //坦克没走step步,随机换一个方向 private Random r = new Random(); private int step = r.nextInt(7)+3; public void draw(Graphics g){ if(!live){ //判断坦克是否存活,如果死了,则不绘画出来,直接返回 return ; } if(!this.good){ if(step==0){ Direction[] dirs =Direction.values(); int rn = r.nextInt(dirs.length); this.dir = dirs[rn]; step = r.nextInt(7)+3; } } Color c = g.getColor(); if(good){ g.setColor(Color.RED); } else{ g.setColor(Color.BLUE); } g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); //画一个炮筒 drawGunBarrel(g); move();//根据键盘按键的结果改变坦克所在的位置 step--; }</code></code></code></code></code></code>
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"> <code class="hljs cs"><code class="hljs java"> private boolean good =true; public Missile(int x,int y,Direction dir,boolean good,TankClient tc){ this(x,y,dir); this.good = good; this.tc = tc; }</code></code></code></code></code></code>
Tank类中fire方法的具体代码如下:
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"><code class="hljs cs"> <code class="hljs cs"> public Missile fire(){ //计算子弹的位置,并利用炮筒的方向来new一个子弹对象 int x = this.x +(this.WIDTH)/2 - (Missile.WIDTH)/2; int y = this.y + (this.HEIGHT)/2 -(Missile.HEIGHT)/2; //根据坦克的类型(good)来new与之对应的子弹类型 Missile ms = new Missile(x,y,this.ptDir,this.good,this.tc); return ms; } </code></code></code></code></code></code>
3、更改以上两点,则在Tank类中draw方法中为坏坦克添加发射子弹这一功能。
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"> <code class="hljs cs"><code class="hljs cs"> public void draw(Graphics g){ if(!live){ //判断坦克是否存活,如果死了,则不绘画出来,直接返回 return ; } //为坏坦克添加随机的方向 if(!this.good){ if(step==0){ Direction[] dirs =Direction.values(); int rn = r.nextInt(dirs.length); this.dir = dirs[rn]; step = r.nextInt(7)+3; } } //根据坦克的好坏来设置不同的颜色 Color c = g.getColor(); if(good){ g.setColor(Color.RED); } else{ g.setColor(Color.BLUE); } g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); //画一个炮筒 drawGunBarrel(g); move();//根据键盘按键的结果改变坦克所在的位置 step--; //敌方子弹开火 if(!this.good&&r.nextInt(40)>38){ this.tc.getMissiles().add(fire()); } } </code></code></code></code></code></code>
4、此时,经过上面的三步之后,我方和敌方都能够发射子弹了,但是敌方能够打死敌方的坦克。因此,还需要对Missile的hitTank(Tank t)方法进行修正。
即在碰撞检测条件中添加这一条:this.good!=t.isGood(),即只有子弹和坦克不是同一类型的,才能打死对方。
具体完整代码如下:
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"> <code class="hljs cs"><code class="hljs java"> public boolean hitTank(Tank t){ //先判断该坦克是否还是存活,如果已经死了,子弹就不打他了 if(!t.isLive()){ return false; } if(this.live&&this.good!=t.isGood()&&this.getRect().intersects(t.getRect())){//判断是否有碰撞 //碰撞之后,子弹和该坦克就应该都死了 this.live = false;//子弹死了 t.setLive(false);//坦克死了 Explode e = new Explode(x,y,tc); tc.getExplodes().add(e); return true; } else{ return false; } } </code></code></code></code></code></code>
5、在Missile的draw方法中,根据子弹的好坏给出不同的颜色。这里采用好子弹采用红色,坏子弹采用蓝色进行区分。
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"> <code class="hljs cs"><code class="hljs cs"> public void draw(Graphics g){ //如果该子弹不是存活的,则不进行绘图 if(!live){ return ; } Color c = g.getColor(); //根据子弹的好坏来设置不同的颜色 if(this.good){ g.setColor(Color.RED); } else{ g.setColor(Color.BLUE); } g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); move(); } </code></code></code></code></code></code>
6、现在差不多就算完成了,但是,我们还需要在TankClient类中的draw方法中做一些改善,例如:1)将我方的坦克置于被敌方子弹攻击的范围内,2)我方坦克被打死之后,应该如何处理。
本项目中,处理的方法为,在我方坦克死亡之后,提示“Game Over,按键A可以复活!!!”字样,并按下键盘A产生一个新的我方坦克。
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"><code class="hljs cs"><code class="hljs cs"> @Override public void paint(Graphics g) { /* * 画出我们自己的坦克,首先判断自己的坦克是否是活的,如果是,则画出来 * 否则,则提示 Game Over ,并休眠100000毫秒 * */ if(tk.isLive()){ tk.draw(g); } else{ g.drawString("Game Over,按键A可以复活!!!",GAME_WIDTH/2 , GAME_HEIGHT/2); } /* * 将敌方坦克也画出来,如果没有了敌方坦克,则产生一定数量的地方坦克 * */ if(enemyTanks.size()==0){ this.produceTank(); } for(int i=0;i<enemytanks.size();i++){ code="" e="explodes.get(i);" enemy="enemyTanks.get(i);" explode="" i="0;i<explodes.size();i++){" int="" missile="" ms="missiles.get(i);" tank=""></enemytanks.size();i++){></code></code></code></code></code></code>
在Tank类中的keyReleased方法中添加键盘A的事件。具体代码如下:
<code class="hljs cs"><code class="hljs cs"><code class="hljs cs"><code class="hljs java"> <code class="hljs cs"><code class="hljs cs"><code class="hljs cs"> //键盘按键松下时,也要进行记录 public void keyReleased(KeyEvent e) { int key=e.getKeyCode(); switch(key){ case KeyEvent.VK_A: produceMainTank(); break; //.....一些其它的case } } private void produceMainTank() { Tank t=this.tc.getTk(); if(!t.isLive()){ int x = r.nextInt(100)+200; int y = r.nextInt(150)+300; Tank newTank =new Tank(x,y,true,Direction.STOP,this.tc); this.tc.setTk(newTank); } } </code></code></code></code></code></code></code>
以上就基本上实现了坦克大战的敌我双方的游戏了。但是,还有一些需要我们完善的功能,例如,加入一些墙等等。在后面,将会慢慢实现。也会在博文中进行记录。
以上就是《Java小游戏实现》:坦克大战(续三)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
