Android game development 2-circle collision

Source: Internet
Author: User
Collision is a very important part. Let me briefly introduce it. Circleactivity Code ; Package yy. circle; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. window; import android. view. windowmanager; public class circleactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. requestwindowfeature (window. feature_no_title ); This. getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); setcontentview (new circle (this) ;}} circle code; Package yy. circle; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paintflagsdrawfilter; import android. view. keyevent; import android. view. m Otionevent; import android. view. surfaceholder; import android. view. surfaceview; import android. view. surfaceholder. callback; public class circle extends surfaceview implements callback, runnable {private surfaceholder SFH; private paint; private thread; private Boolean flag; private canvas; private int screenw, screenh; // Private bitmap BMP; private int X1 = 20, Y1 = 20, R1 = 20; private int X2 = 50, y2 = 50, R2 = 20; private Boolean iscollsion; private paintflagsdrawfilter PFD = new paintflagsdrawfilter (0, paint. anti_alias_flag | paint. filter_bitmap_flag); Public circle (context) {super (context); SFH = This. getholder (); SFH. addcallback (this); paint = new paint (); paint. setcolor (color. white); paint. setantialias (true); // BMP = bitmapfactory. decoderesource (this. getresources (), R. drawable. icon); s Etfocusable (true);} public void surfacecreated (surfaceholder holder) {screenw = This. getwidth (); screenh = This. getheight (); flag = true; thread = new thread (this); thread. start ();} public void mydraw () {try {canvas = SFH. lockcanvas (); If (canvas! = NULL) {canvas. setdrawfilter (PFD); canvas. drawcolor (color. black); If (iscollsion) {paint. setcolor (color. red); paint. settextsize (20); canvas. drawtext ("collision! ", Paint);} else {paint. setcolor (color. white);} canvas. drawcircle (x1, Y1, R1, paint); canvas. drawcircle (X2, Y2, R2, paint); // canvas. drawbitmap (BMP, 100,100, paint) ;}} catch (exception e) {} finally {If (canvas! = NULL) {SFH. unlockcanvasandpost (canvas) ;}} public Boolean ontouchevent (motionevent event) {x1 = (INT) event. getx ()-R1; Y1 = (INT) event. gety ()-R1; If (iscollsionwithcircle (x1, Y1, R1, X2, Y2, R2) {iscollsion = true;} else {iscollsion = false;} return true ;} private Boolean iscollsionwithcircle (INT X1, int Y1, int R1, int X2, int Y2, int R2) {/* If (x1> = X2 & x1> = x2 + r2 + R1) {return false ;} else if (X1 <= X2 & X1 + R1 + R2 <= x2) {return false ;} else if (Y1> = Y2 & Y1> = y2 + r2 + R1) {return false ;} else if (Y1 <= Y2 & Y1 + R1 + R2 <= Y2) {return false;} */If (R1 + R2) * (R1 + R2) <= (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) {return false;} return true;} public Boolean onkeydown (INT keycode, keyevent event) {return Super. onkeydown (keycode, event);} public void logic () {} public void run () {While (FLAG) {long start = system. currenttimemillis (); mydraw (); logic (); long end = system. currenttimemillis (); try {If (end-start <50) {thread. sleep (50-(end-Start);} catch (interruptedexception e) {e. printstacktrace () ;}} public void surfacechanged (surfaceholder holder, int format, int width, int height) {} public void surfacedestroyed (surfaceholder holder) {flag = false ;}}

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.