Game Development: launching a bullet and moving back

Source: Internet
Author: User

 

<Br/> Import Java. applet. *; <br/> Import Java. util. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> public class T6 extends applet implements runnable <br/>{< br/> thread tt; <br/> int appletwidth, appletheight; <br/> image offscreen, bullet, player, player2; <br/> graphics drawoffscreen; <br/> mediatracker MT; <br/> imagesprite2 GS1, fs1; <br/> bulletsprite2 zd1; <br/> Public void Init () {<br/> Mt = New mediatracker (this); <br/> resize (800,600); <br/> appletwidth = getsize (). width; <br/> appletheight = getsize (). height; <br/> bullet = getimage (getdocumentbase (), "ZD. GIF "); <br/> player = getimage (getdocumentbase ()," a2.gif "); <br/> player2 = getimage (getdocumentbase ()," a1.gif "); <br/> Mt. addimage (player, 0); <br/> Mt. addimage (player2, 0); <br/> Mt. addimage (bullet, 0); <br/> try {<br/> Mt. waitforall (); <br/>} Catch (exception e) {}< br/> GS1 = new imagesprite2 (player2, this, 0); <br/> fs1 = new imagesprite2 (player, this, 1 ); <br/> zd1 = new bulletsprite2 (bullet, this, appletwidth, appletheight); <br/> gs1.setsize (player2.getwidth (this), player2.getheight (this )); <br/> fs1.setsize (player. getwidth (this), player. getheight (this); <br/> zd1.setsize (bullet. getwidth (this), Bullet. getwidth (this); <br/> gs1.setlocation (10, 10 ); <Br/> fs1.setlocation (); <br/> offscreen = createimage (appletwidth, appletheight); <br/> drawoffscreen = offscreen. getgraphics (); <br/>}< br/> Public void start () {<br/> TT = new thread (this); <br/> TT. start (); <br/>}< br/> Public void stop () {TT = NULL ;}< br/> Public void run () <br/>{< br/> int I2 = 0; <br/> while (TT! = NULL) <br/>{< br/> repaint (); <br/> try {<br/> thread. sleep (33); <br/>}catch (exception e) {}< br/> gs1.updatestate (); <br/> fs1.updatestate (); <br/> zd1.updatestate (); <br/> If (I2 ++> 10) <br/>{< br/> start1 (); <br/> I2 = 0; <br/>}< br/> If (zd1.hit2 (fs1.getx (), fs1.gety (), fs1.getwidth (), fs1.getheight ())) <br/>{< br/> zd1.setbullet (false); <br/>}< br/> If (fs1.hit2 (zd1.getx (), zd1.gety (), zd1.getwidth (), zd1.getheight () <br/>{< br/> fs1.setlocation (fs1.getx () + 2, fs1.gety ()); <br/>}< br/> Public void paint (Graphics g) <br/>{< br/> drawoffscreen. clearrect (0,0, appletwidth, appletheight); <br/> gs1.paintsprite (drawoffscreen); <br/> fs1.paintsprite (drawoffscreen); <br/> zd1.paintsprite (drawoffscreen ); <br/> G. drawimage (OFFSCREEN, 0, 0, this); <br/>}< br/> Public void Update (Graphics g) {paint (g );} <br/> Public void start1 () <br/> {<br/> If (zd1.canvisible () = false & <br/> zd1.canmove () = false) <br/>{< br/> zd1.setlocation (gs1.getx () + gs1.getwidth (), gs1.gety () + (gs1.getheight ()/2 )); <br/> zd1.setvisible (true); <br/> zd1.setmove (true); <br/>}< br/> 

 

Import Java. applet. *; <br/> Import Java. util. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <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/>} 

 

Import Java. applet. *; <br/> Import Java. util. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; </P> <p> class imagesprite2 extends supersprite <br/>{< br/> int VX, Vy; <br/> image spriteimage; <br/> applet game; <br/> Boolean showpig; <br/> Public imagesprite2 (image spriteimage, Applet game, int VX) <br/>{< br/> This. spriteimage = spriteimage; <br/> This. game = game; <br/> This. VX = VX; <br/> showpig = false; <br/> setlocation (0, 0); <br/> setvisible (true); <br/> setmove (true ); <br/>}< br/> Public void updatestate () <br/>{< br/> If (active) <br/>{< br/> X + = VX; <br/> // y + = Vy; <br/>}< br/> // system. out. println (x); <br/> // system. out. println (y); <br/> // If (x> 640 | Y> 480) | (x <0 | Y <0 )) <br/> If (x> 800 | Y> 600) | (x <0 | Y <0 )) <br/>{< br/> // showpig = false; <br/> vx =-VX; <br/> // Vy =-Vy; <br/>}else {<br/> // showpig = true; <br/> showpig = true; <br/>}< br/> Public void paintsprite (Graphics g) <br/>{< br/> If (visible) <br/> {<br/> G. drawimage (spriteimage, X, Y, game); <br/>}< br/> Public Boolean hit (int x, // coordinate of the current X of the other Party <br/> int y, // coordinate of the current y of the other Party <br/> int p_width, // length of your own image <br/> int p_height, // height of your own image <br/> int h_width, // length of the Target Image <br/> int h_height // height of the Target Image <br/>) <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/>{< br/> // showpig = false; <br/> showpig = false; <br/> // setvisible (false); <br/> // setmove (false); <br/> return true; <br/>}else <br/> return false; <br/>}< br/> Public Boolean hit2 (INT Dix, <br/> int DIY, <br/> int di_width, <br/> int di_height) <br/>{< br/> If (<br/> (this. X + this. width> = Dix) & <br/> (this. Y + this. height> = DIY) & <br/> (Dix + di_width> = This. x) & <br/> (DIY + di_height> = This. y) & <br/> showpig <br/>) {<br/>/* system. out. println (Dix); <br/> system. out. println (DIY); <br/> system. out. println (di_width); <br/> system. out. println (di_height); <br/> system. out. println (showpig); <br/> system. out. println (); <br/> system. out. println (this. x); <br/> system. out. println (this. y); <br/> system. out. println (this. width); <br/> system. out. println (this. height); */<br/> showpig = false; <br/> return true; <br/>} else <br/> return false; <br/>}< br/> 

 

<Br/> Import Java. applet. *; <br/> Import Java. util. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; </P> <p> class bulletsprite2 extends imagesprite2 <br/>{< br/> int appletwidth, appletheight; <br/> Boolean bullet = true; <br/> Public bulletsprite2 (image bullet, Applet game, int appletwidth, int appletheight) <br/>{< br/> super (bullet, game, 0 ); <br/> This. appletwidth = appletwidth; <br/> This. appletheight = appletheight; <br/> setvisible (false); <br/> setmove (false); <br/> // system. out. println (showpig); <br/> showpig = true; <br/>}< br/> Public void updatestate () <br/>{< br/> If (active) <br/>{< br/> If (x> appletwidth) <br/>{< br/> setvisible (false); <br/> setmove (false ); <br/> showpig = false; <br/>} else if (bullet = false) {<br/> setvisible (false); <br/> setmove (false ); <br/> // system. out. println ("Bullet =" + bullet); <br/> bullet = true; <br/> // system. out. println ("Bullet =" + bullet); <br/> showpig = false; <br/>}else {<br/> showpig = true; <br/> X = x + 10; <br/>}< br/> Public void paintsprite (Graphics g) <br/>{< br/> If (visible) <br/>{< br/> G. drawimage (spriteimage, X, Y, game); <br/>}< br/> Public Boolean getbullet () <br/>{< br/> return bullet; <br/>}< br/> Public void setbullet (Boolean v) <br/>{< br/> This. bullet = V; <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.