Simple collision principle in Java games

Source: Internet
Author: User

First, the game: the player plane collision with enemy aircraft or bullets and enemy aircraft, resulting in explosive effect, enemy aircraft and bullets disappeared after the explosion.



Second, the realization principle: Detection principle of two objects whether collision, Java provides Rectangle this class, the inside of the intersects (Rectangle R) method can complete this demand. The current practice is to traverse the screen of bullets and airplanes, and in the process of traversal, use this method to detect collisions. Third, enemy aircraft and bullet collision code:

Package Com.jack;import Java.awt.graphics;import Java.awt.image;import java.awt.rectangle;import Java.awt.image.imageobserver;import javax.swing.jpanel;import com.jack.imp.iplan;/** * * @author laughing * @date November 20, 2014 PM 11:03:11 */public Class Enemy3 implements Iplan {private static final image[]enemy1_images= imagemanager.ge Tinstance (). Getimagesbytype ("E3");p rivate static final image[]bomb_images= imagemanager.getinstance (). Getimagesbytype ("Bomb");p rivate intx= 370;private inty= 0;private static final intspeed= 20;private booleanoutofbounds=  False;private booleaniscollision= false; @Overridepublic void Drawmyself (Graphics g, JPanel p) {if (iscollision) {for (int i = 0; i < bomb_images.length; i++) {g.drawimage (bomb_images[i], X, Y, (ImageObserver) p);}} else {g.drawimage (enemy1_images[0], X, Y, (ImageObserver) p);}} @Overridepublic void Move () {if (y >= gamepanel.height) {setoutofbounds (true);} else {y + = speed;}} @Overridepublic void Shut () {//TODO auto-generated method stub} @Overridepublic Boolean Collison (Rectangle r) {Rectangle enemy1 = new Rectangle (X,y,enemy1_images[0].getwidth (NULL), Enemy1_images[0].getheight (NULL)), if (Enemy1.intersects (R)) {Setcollision (True), return true; return false;} /** * @return The Outofbounds */public boolean isoutofbounds () {return outofbounds;} /** * @param outofbounds * The outofbounds to set */public void Setoutofbounds (Boolean outofbounds) {This.outof Bounds = Outofbounds;} /* * (NON-JAVADOC) * * @see com.jack.imp.iplan#isoutofbounds () */@Overridepublic Boolean isoutofbounds () {//TODO Auto-ge Nerated method Stubreturn outofbounds;} /** * @return The Iscollision */public boolean iscollision () {return iscollision;} /** * @param iscollision * The iscollision to set */public void Setcollision (Boolean iscollision) {This.iscolli sion = Iscollision;}}

Package Com.jack;import Java.awt.graphics;import Java.awt.image;import java.awt.rectangle;import  Java.awt.image.imageobserver;import Javax.swing.jpanel;import com.jack.imp.idraw;/** * Game bullets * * @author laughing * @date November 20, 2014 morning 10:27:12 */public class Bullet implements IDraw {public static final image[]bullet_images= Imagemanager.get Instance (). Getimagesbytype ("Bullet");p ublic static final image[]bomb_images= imagemanager.getinstance (). Getimagesbytype ("Bomb");p ublic static final intfly_speed= 30;private booleanoutofbounds= false;private booleaniscollision= false;public intx;public inty;public Bullet (int x, int y) {this.x = x + 38;this.y = y;} public Boolean outofbounds () {if (This.y = = 0) {this.outofbounds = True;return true;} return false;} Public Rectangle Castrectangle () {return new Rectangle (X,y,bullet_images[0].getwidth (NULL), Bullet_images[0]. GetHeight (null));} /* * (NON-JAVADOC) * * @see com.jack.imp.iprocess#drawmyself (java.awt.Graphics, * javax.swing.JPanel) * * @Overridepublicvoid Drawmyself (Graphics g, JPanel p) {if (Iscollision ()) {G.drawimage (Bomb_images[0],x,y-bomb_images[0].getheight ( NULL), (ImageObserver) p);} else {g.drawimage (bullet_images[0],x,y-bullet_images[0].getheight (null), (ImageObserver) p);}} /* * (NON-JAVADOC) * * @see com.jack.imp.iprocess#move () */@Overridepublic void Move () {if (!outofbounds ()) This.y-= Fly_ Speed;} /** * @return the y */public int GetY () {return y;} /** * @return The x */public int GetX () {return x;} /** * @return The Outofbounds */public boolean isoutofbounds () {return outofbounds;} /** * @param outofbounds * The outofbounds to set */public void Setoutofbounds (Boolean outofbounds) {This.outof Bounds = Outofbounds;} /** * @return The Iscollision */public boolean iscollision () {return iscollision;} /** * @param iscollision * The iscollision to set */public void Setcollision (Boolean iscollision) {This.iscolli sion = Iscollision;} /* * (NON-JAVADOC) * * @see java.lang.object#tostring () */@Overridepublic STRing toString () {return "Bullet [outofbounds=" + Outofbounds + ", iscollision=" + Iscollision + ", x=" + x + ", y=" + y + " ]";}}


Iv. Source code: Click to open the link

Simple collision principle in Java games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.