A small game demo tank war written by cocos2d-xna

Source: Internet
Author: User

Recently saw a lot of information about cocos2d online, looked at cocos2d also supports WP7, download a Cocos2d-XNA installation package, write a small example to play, familiar with cocos2d

The program is very simple. It is an entry-level mini-game. After writing it, you can run it on your mobile phone.

Development Environment: vs2010 and Windows Phone SDK 7.1

The implementation method is as follows:

1. Create a cclayer subclass.

2. Rewrite the onenter method to add some basic buttons and some initialization code.

3. Use the schedule method to control the ccsprite object of tank bullets

4. Click the cell phone screen to determine the direction of the tank. Modify the X and Y axes of the tank to move the tank.

5. Use the ccrectintersetsrect function of ccrect to perform collision detection, so that the bullet can hit the tank.

6. After the code is complete and Zune is installed, the game can be deployed on the mobile phone. before deployment, the mobile phone must be bound to the developer account or student account.

Specific implementation code

1. Add enumeration types to the Project

/// <Summary>

/// Enumeration type indicating the direction

/// </Summary>

Public Enum direction {L, U, D, R, stop}

2. Add constants and attributes of the bullet class.

/// <Summary>
/// Speed of the X axis of the bullet, in PX
/// </Summary>
Public static int xspeed = 10;

/// <Summary>
/// Speed of the bullet Y axis, in PX
/// </Summary>
Public static int yspeed = 10;

/// <Summary>
/// Bullet width
/// </Summary>
Public static int width = 15;

/// <Summary>
/// Bullet height
/// </Summary>
Public static int Height = 15;

/// <Summary>
/// Bullet coordinates
/// </Summary>
Int X, Y;

/// <Summary>
/// Bullet direction
/// </Summary>
Direction dir;

/// <Summary>
/// The survival status of the bullet
/// </Summary>
Private bool live = true;

/// <Summary>
/// Tankclient form instance
/// </Summary>
Private tankclient;

/// <Summary>
/// Mark of both sides of the enemy and me
/// </Summary>
Private bool good;

Ccsprite m_missile;

3. Add the draw method to draw the bullet

Public void draw ()
{
If (! Live)
{
Tankclient. removechild (m_missile, true );
Tankclient. Missiles. Remove (this );
Return;
}
M_missile.position = new ccpoint (x, y );
Move ();
}

4. How to add a bullet to combat tanks
Public bool hittank (tank T)
{
// Intersectswith is used to detect the collision between two rectangles.
// If (getrectangle (). intersectswith (T. getrectangle () & T. Live & this. Live & this. Good! = T. Good)
If (ccrect. ccrectintersetsrect (getrectangle (), T. getrectangle () & T. Live & this. Live & this. Good! = T. Good)
{
T. Live = false;
This. Live = false;
Return true;
}
Return false;
}

5. Add the attributes and constants of the tank class.
/// <Summary>
/// Tank X axis speed
/// </Summary>
Public static int xspeed = 5;

/// <Summary>
/// Tank Y axis speed
/// </Summary>
Public static int yspeed = 5;

/// <Summary>
/// Tank width
/// </Summary>
Public static int width = 58;

/// <Summary>
/// Tank height
/// </Summary>
Public static int Height = 58;

/// <Summary>
/// Tank coordinates
/// </Summary>
Private int X, Y;

/// <Summary>
/// Mark whether the upper and lower right keys are pressed
/// </Summary>
Private bool L = false, u = false, r = false, D = false;

/// <Summary>
/// Tank direction
/// </Summary>
Private direction dir = direction. Stop;

/// <Summary>
/// Tank barrel direction
/// </Summary>
Private direction ptdir = direction. D;

/// <Summary>
/// Tankclient form instance
/// </Summary>
Tankclient;

/// <Summary>
/// Mark both sides of the enemy and me
/// </Summary>
Private bool good;

/// <Summary>
/// Used to control irregular operations of enemy tanks
/// </Summary>
Private int step = 0;

/// <Summary>
/// Mark the survival status of the tank
/// </Summary>
Private bool live = true;

Public ccsprite m_tank;

6. Implement the tank painting method in the tank class
Public void draw ()
{
If (! Live)
{
If (! Good)
{
Tankclient. removechild (m_tank, true );
Tankclient. Tanks. Remove (this );
}
Tankclient. removechild (m_tank, true );
Return;
}
If (good)
{
M_tank.position = new ccpoint (x, y );
}
Else
{
// G. fillellipse (brushes. Blue, X, Y, width, height );
M_tank.position = new ccpoint (x, y );
}
// Draw the barrel of the tank based on the barrel Tank
Switch (ptdir)
{
Case direction. D:
M_tank.rotation = 0; // The rotation genie controls the barrel direction
Break;
Case direction. U:
M_tank.rotation = 180;
Break;
Case direction. L:
M_tank.rotation = 270;
Break;
Case direction. R:
M_tank.rotation = 90;
Break;
}
Move ();
}

7. Tank method of issuing bullets

Public missile fire ()
{
If (! Live) return NULL;

Int x = This. X;
Int y = This. Y;
Missile = new missile (X, Y, good, ptdir, tankclient );
Tankclient. Missiles. Add (missile );
Return missile;
}

 

8. Add cclayer to the tank

Mytank = new tank (60,420, true, this );
For (INT I = 0; I <10; I ++)
{
// Add 10 tanks with a spacing of 70
Tanks. Add (new tank (50 + 70 * (I + 1), 20, false, this ));
}

9. How to call a bullet to hit a tank in cclayer

For (INT I = 0; I <missiles. Count; I ++)
{
Missile M = Missiles [I];
M. hittank (mytank );
M. hittanks (tanks );
M. Draw ();
}

10. Code for controlling tank mobile shooting
Public override void cctouchended (cctouch touch, ccevent Event _)
{
Mytank. keyreleased (Microsoft. xNa. Framework. Input. Keys. Down );
}

Public void hitcallback (ccobject psender)
{
Mytank. keyreleased (Microsoft. xNa. Framework. Input. Keys. Enter );
}
 

11. Running effects in virtual machines

 

 

 

Code download http://download.csdn.net/detail/xiaoxiao108/4545620

The code in the program that controls the direction of the tank is not very well handled and is not implemented through the Virtual joystick.

If you find anything unreasonable, you need to improve the place, mail contact 328452421@qq.com (qq perennial not online, mail contact ). Exchange with each other. Thank you.

Note:

1. It is equivalent to the reset function, not the control direction. The direction is controlled by clicking the screen.

2. Bullet launch button

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.