HTML5 + jquery, full version of smart bullets

Source: Internet
Author: User

The full version of the bullet tracking aircraft is provided here. The bullet will keep tracking until it hits the plane. After the call, a message is displayed, indicating a few seconds of persistence.

There is a small bug. After hitting it, It is prompted first and then red. You can change it to red first, and then prompt again.

In addition, it can be changed to a bullet that automatically disappears after 10 s of flight (re-start again)

You can also change the speed to + 1 per second;

 

<! Doctype HTML>
<HTML>
<Head>
<Title> full version of smart bullets </title>
<SCRIPT type = "text/JavaScript" src = "jquery-1.6.1.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
VaR x = 225;
Var y = 450;
VaR mousex, Mousey;
VaR canvas;
VaR CTX;
VaR C = 0;
VaR Zidan = new object ();
$ (Document). Ready (function (){
Canvas = $ (document). Get (0). getelementbyid ("mycanvas ");

If (canvas. getcontext ){
CTX = canvas. getcontext ("2D ");
Zidan. x = 245;
Zidan. Y = 10;
Zidan. jiaodu = math. Pi * 0.5;
Zidan. Sudu = 10;
Zidan. dadao = 0; // 0 indicates that no call is made, and 1 indicates that no call is made.
Animate ();
}
});
Zidan. Move = function (){
If (Y-Zidan. Y> 0)
{
Zidan. Y = Zidan. Y + math. Abs (Zidan. Sudu * Math. Cos (Zidan. jiaodu ));
}
Else
{
Zidan. Y = Zidan. Y-math. Abs (Zidan. Sudu * Math. Cos (Zidan. jiaodu ));
}
If (X-Zidan. x> 0)
{
Zidan. x = Zidan. x + math. Abs (Zidan. Sudu * Math. Sin (Zidan. jiaodu ));
}
Else
{
Zidan. x = Zidan. X-math. Abs (Zidan. Sudu * Math. Sin (Zidan. jiaodu ));
}


// Exceeds the bottom boundary
If (zidan. y> 500)
Zidan. Y = 10;
// Add a judgment that exceeds the upper left and right boundary
// If (Zidan. Y = 10)
Zidan. jiaodu = math. atan (X-Zidan. X)/(Y-Zidan. y ));
};
Function animate ()
{

CTX. clearrect (0, 0,500,500 );
// The square controlled by the keyboard
If (Zidan. dadao = 1)
CTX. fillstyle = "red ";
CTX. fillrect (X, Y, 50, 50 );
CTX. fillstyle = "black ";
CTX. beginpath ();
CTX. ARC (Zidan. X, Zidan. Y, 10, 0, math. Pi * 2, false );
CTX. closepath ();
CTX. Fill ();
If (Zidan. dadao = 0)
SetTimeout (animate, 100 );
// Animation logic. If the bullet is in a straight line and beyond the range, set it to 10 again.
Zidan. Move ();
// Collision detection. If hit, the square turns red and the game stops.
If (Zidan. y> Y) & (Zidan. Y <Y + 50) & (Zidan. x> X) & (Zidan. x <X + 50 ))
{
Zidan. dadao = 1;
Alert (C/10 );
}
C = C + 1;

};
</SCRIPT>
</Head>
<Body>
<Div>
<Canvas id = "mycanvas" width = "500" Height = "500"> </canvas>
</Div>

</Body>
<SCRIPT type = "text/JavaScript">

// Note that this is document, and canvas should be able to capture the keydown event. Try again later.
$ (Document). keydown (function (e ){
VaR K = E. keycode;
// If you do not know the key code, use alert to output it.
// Alert (k );
If (k = 37)
X = x-10;
If (k = 39)
X = x + 10;
If (k = 38)
Y = Y-10;
If (k = 40)
Y = Y + 10;

});
</SCRIPT>
</Html>

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.