Java Applet game collision code

Source: Internet
Author: User

 

 

 

Import Java. applet. *; <br/> Import Java. util. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> public class T1 extends applet implements runnable, mouselistener, mousemotionlistener, actionlistener <br/>{< br/> thread tt; <br/> image offscreen, tupian1, tupian2, tu2, bj1; <br/> graphics drawoffscreen; <br/> int appletwidth, appletheight, tuwidth, tuheight, tu2width, tu2height; <br/> pigsprite ps1, pS2; <br /> Sstest1 beijing1; <br/> button b1 = new button ("Start again"); <br/> mediatracker MT; // This is important without it, incorrect image length and width <br/> Public void Init () <br/>{< br/> addmouselistener (this); <br/> addmousemotionlistener (this ); <br/> resize (640,480); <br/> appletwidth = getsize (). width; <br/> appletheight = getsize (). height; <br/> Mt = new mediatracker (this); <br/> tupian1 = getimage (getdocumentbase (), "a1.gif"); <br/> tupian2 = getimag E (getdocumentbase (), "a2.gif"); <br/> bj1 = getimage (getdocumentbase (), "bj1.gif"); <br/> Mt. addimage (tupian1, 0); <br/> Mt. addimage (tupian2, 0); <br/> try {<br/> Mt. waitforall (); <br/>}catch (exception e) {}< br/> ps1 = new pigsprite (tupian1, this, 5 ); <br/> PS2 = new pigsprite (tupian2, this, 2); <br/> beijing1 = new sstest1 (bj1, this ); <br/> tuwidth = tupian1.getwidth (this); <br/> system. out. println (tuwidth); <br/> T Uheight = tupian1.getheight (this); <br/> system. out. println (tuheight); <br/> tu2width = tupian2.getwidth (this); <br/> system. out. println (tu2width); <br/> tu2height = tupian2.getheight (this); <br/> system. out. println (tu2height); <br/> ps1.setlocation (100,100); <br/> ps2.setlocation (); <br/> offscreen = createimage (appletwidth, appletheight ); <br/> drawoffscreen = offscreen. getgraphics (); <br/>}< br/> pub LIC void start () {<br/> TT = new thread (this); <br/> TT. start (); <br/>}< br/> Public void stop () {TT = NULL ;}< br/> Public void run () <br/>{< br/> while (TT! = NULL) <br/>{< br/> repaint (); <br/> try <br/>{< br/> thread. sleep (33); <br/> // thread. sleep (1000); <br/>}catch (exception e) {}< br/> ps1.updatestate (); <br/> ps2.updatestate (); <br/> // int <br/> If (ps1.hit (ps2.getx (), ps2.gety (), tuwidth, tuheight, tu2width, tu2height )) <br/>{< br/> // ps2.setvisible (false); <br/>}< br/> If (ps2.hit (ps1.getx (), ps1.gety (), tuwidth, tuheight, tu2width, tu2height) <br/>{< br/> // ps1.setvisible (false ); <br/>}< br/> Public void paint (Graphics g) <br/>{< br/> drawoffscreen. clearrect (, appletwidth, appletheight); <br/> beijing1.paintsprite (drawoffscreen); <br/> ps1.paintsprite (drawoffscreen); <br/> ps2.paintsprite (drawoffscreen ); <br/> G. drawimage (OFFSCREEN, 0, 0, this); <br/>}< br/> Public void Update (Graphics g) {paint (g );} <br/> Public void mouseexited (mouseevent e) {}< br/> Public void mouseclicked (mouseevent e) {}< br/> Public void mouseentered (mouseevent E) {}< br/> Public void mousepressed (mouseevent e) {}< br/> Public void mousereleased (mouseevent e) {}< br/> Public void mousemoved (mouseevent E) {}< br/> Public void mousedragged (mouseevent e) {}< br/> Public void actionreceivmed (actionevent E) {}< br/>}< br/> abstract class supersprite <br/>{< br/> int X, Y, width, height; <br/> Boolean visible, active; <br/> Abstract Public void paintsprite (Graphics g); <br/> Abstract Public void updatestate (); <br/> Public int getx () {return X ;} <br/> Public int Gety () {return y ;}< br/> Public void setlocation (int x, int y) <br/>{< br/> This. X = x; <br/> This. y = y; <br/>}< br/> Public int getwidth () <br/>{< br/> return width; <br/>}< br/> Public int getheight () <br/>{< br/> return height; <br/>}< br/> Public void setsize (INT width, int height) <br/>{< br/> This. width = width; <br/> This. height = height; <br/>}< br/> Public Boolean canvisible () <br/>{< br/> return visible; <br/>}< br/> Public void setvisible (Boolean v) <br/>{< br/> visible = V; <br/>}< br/> Public Boolean canmove () <br/>{< br/> return active; <br/>}< br/> Public void setmove (Boolean m) <br/>{< br/> active = m; <br/>}< br/> class pigsprite extends supersprite <br/>{< br/> int VX, Vy; <br/> image img1; <br/> applet game; <br/> random R; <br/> Boolean showpig; <br/> Public pigsprite (image img1, Applet game, int speed) <br/>{< br/> This. img1 = img1; <br/> This. game = game; <br/> This. VX = speed; <br/> This. vy = speed; <br/> showpig = false; <br/> setvisible (true); <br/> setmove (true ); <br/>}< br/> Public void updatestate () <br/>{< br/> If (active) <br/>{< br/> X + = VX; <br/> Y + = Vy; <br/>}< br/> If (x> 640 | Y> 480) | (x <0 | Y <0 )) <br/>{< br/> showpig = false; <br/> vx =-VX; Vy =-Vy; <br/>} else {<br/> showpig = true; <br/>}< br/> Public void paintsprite (Graphics g) <br/>{< br/> If (visible) <br/>{< br/> If (showpig) <br/> G. drawimage (img1, X, Y, game); <br/>}< br/> Public Boolean hit (int x, int y, int p_width, int p_height, int h_width, int h_height) <br/>{< br/> If (<br/> (this. X + p_width> = x) & <br/> // (this. X + 40> = x) & <br/> (this. Y + p_height> = y) & <br/> // (this. Y + 40> = y) & <br/> (x + h_width> = This. x) & <br/> // (x + 40> = This. x) & <br/> (Y + h_height> = This. y) & <br/> // (Y + 40> = This. y) & <br/> showpig) <br/>{< br/> showpig = false; <br/> // setvisible (false ); <br/> // setmove (false); <br/> return true; <br/>}else <br/> return false; <br/>}< br/>} 

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.