How to build your own game framework and make a game (2) (with source code)

Source: Internet
Author: User
Tags gety

Now we are teaching the second article. With the framework, we can freely draw what we want on the screen. The background is used.Background ComponentsThe characters, bullets, and genie are all used.Sprite genie Components

 

The gameactivity class is the main activity class of the game. It inherits the base class here. You only need to replace the interface with gameview.

Package COM. mocn. airbottle; import android. r; import android. OS. bundle; import COM. mocn. framework. baseactivity; import COM. mocn. framework. baseview;/*** game activity class ** @ author administrator **/public class planegameactivity extends baseactivity {public baseview; // reference baseview public gameview; // reference gameview @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); baseview = new gameview (this); // obtain the baseview object setcontentview (baseview); // set the display interface to baseview }}

The gameview class is used to draw the main interface of the game. Here we need to draw planes and enemies to detect collision events between bullets and enemies.

Package COM. mocn. airbottle; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import COM. mocn. framework. backgroundlayer; import COM. mocn. framework. baseview; import COM. mocn. framework. utils; public class gameview extends baseview implements ontouchlistener {backgroundlayer backlayer; // background component plane; // aircraft class public Boolean pressplane = false; Public gameview (context) {super (context); setontouchlistener (this); // draw background backlayer = new backgroundlayer (utils. getbitmap ("game/bg.png"), 800,480); backlayer. setposition (0, 0); // draw aircraft plane = new plane (utils. getbitmap ("game/plane.png"), 150,179); plane. setposition (40,300) ;}@ override public void drawsurfaceview (canvas, paint) {super. drawsurfaceview (canvas, paint); gamedata. bulletsandenemy (); // judge the collision between the bullet and the enemy's gamedata. createenemy (); // create enemy}/*** Method for executing the touch event */@ override public Boolean ontouch (view V, motionevent event) {If (event. getaction () = motionevent. action_down) {// press the executed event // The method of plane execution in the pressed point if (utils. inrect (plane. x-plane. w/2, plane. y-plane. h/2, plane. w, plane. h, event. getx (), event. gety () {pressplane = true; // set the aircraft to true} else if (event. getaction () = motionevent. action_move) {// drag the execution event if (pressplane) {plane. setposition (event. getx (), event. gety (); // set the coordinates of the plane to the coordinates when dragging.} else if (event. getaction () = motionevent. action_up) {// The method to be executed when the button is released: pressplane = false; // set the plane to move to false} return true ;}}

plane: aircraft. We can attach bullets to the aircraft.

Package COM. mocn. airbottle; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. paint; import COM. mocn. framework. layermanager; import COM. mocn. framework. sprite; import COM. mocn. framework. utils;/*** aircraft ** @ author administrator **/public class plane extends sprite {public int timespan = 1000; // The interval between bullets in an airplane is public long updatetime = 0; // The update time of an airplane is public plane (Bitmap bitmap, int W, int h) {super (bitmap, W, h, true) ;}@ override public void drawself (canvas, paint) {super. drawself (canvas, paint); // attaches a bullet to the plane if (system. currenttimemillis ()> updatetime) {// create a bullet B = new bullet (utils. getbitmap ("game/buttle.png"), 50, 50); B. setposition (x + W/2, Y); // set the position of the bullet B. setaction ("BB"); // sets the bullet action layermanager. insert (B, this); // Insert the bullet layer gamedata in front of the plane layer. bullets. add (B); // Add a bullet updatetime = system. currenttimemillis () + timespan ;}}}

Buttle class, bullet class. In this class, we need to set the action and route of the bullet.

 

Package COM. mocn. airbottle; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. paint; import COM. mocn. framework. layermanager; import COM. mocn. framework. sprite;/*** bullet class, inheriting the genie class ** @ author administrator **/public class bullet extends sprite {public int speed = 10; // speed of the bullet public bullet (Bitmap bitmap, int W, int h) {super (bitmap, W, H, false); addaction ("BB ", new int [] {0, 1, 2, 3, 4, 5}, new int [] {50, 50, 50, 50, 50 });} @ override public void drawself (canvas, paint) {super. drawself (canvas, paint); x + = speed; // move the bullet. // when the bullet exceeds the screen, remove the bullet if (x> = 800) {layermanager. deletelayer (this); gamedata. bullets. remove (this );}}}

 

Enemy class, enemy class. In this class, we need to set the action and path for it.Line

 
Package COM. mocn. airbottle; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. paint; import COM. mocn. framework. sprite;/*** enemy painting class ** @ author administrator **/public class enemy extends sprite {Public Enemy (Bitmap bitmap, int W, int H) {super (bitmap, W, H, true); addaction ("Left", new int [] {0, 1, 2, 3, 4 }, new int [] {50, 50, 50, 50}); // sets the action set} @ override public void drawself (canvas, paint) {super. drawself (canvas, paint); X-= 2; // move the enemy to the left }}

The last one is the gamedata and game data class. Here we have a method to create an enemy and a collision between the bullet and the enemy.

Package COM. mocn. airbottle; import Java. util. vector; import COM. mocn. framework. layermanager; import COM. mocn. framework. utils;/*** game data, used to control the appearance of the enemy and the collision between the bullets and the enemy ** @ author administrator **/public class gamedata {public static vector <bullet> bullets = new vector <bullet> (); public static vector <enemy> enemys = new vector <enemy> (); Private Static long updatetime; // set the Update Time/*** method for creating the enemy */public static void createenemy () {If (system. currenttimemillis () <updatetime) return; updatetime = system. currenttimemillis () + 3000; // set the update time, 3 seconds out of one // create enemy E = new enemy (utils. getbitmap ("game/enemy.png"), 72, 69); E. setposition (800, (float) math. random () * 480); // set the position of the enemy, fixed by X, and random by Y. setaction ("Left"); // sets the action enemys. add (E); // Add an enemy to the vector}/*** how to handle the collision between the bullet and the enemy */public static void bulletsandenemy () {// traverse bullets and enemy forces for (INT I = bullets. size ()-1; I> = 0; I --) {bullet B = bullets. elementat (I); For (Int J = enemys. size ()-1; j> = 0; j --) {enemy E = enemys. elementat (j); // call the collision detection method Boolean T = utils in the tool class. collisewidth (B. x-B. w/2, B. y-B. h/2, B. w, B. h, E. x-e. w/2, E. y-e. h/2, E. w, E. h); If (t) {bullets. remove (B); // remove the bullet enemys in the vector. remove (E); // remove the enemy layermanager from the vector. deletelayer (B); // remove the bullet layermanager from the component. deletelayer (E); // remove the enemy from the component }}}}}

So far, the entire game teaching has been completed.
This is a simple game,CodeThere are a large number of frameworks, especially the framework. However, this framework can be said to be very basic, but it covers many aspects of the game. If you have the chance to go to a game company in the future, the framework will be more complex, I believe that if a person can overcome the difficulties and understand them, he will be almost far away from the real game masters, there is a real opportunity for someone to prepare!
Download source code:

Http://files.cnblogs.com/feifei1010/AirBottle2.zip

Android Developers are welcome to join the group to exchange ideas ~ Beijing group 111059554, Xi'an group 252746034, Wuhan group 121592153

 

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.