Android Combat Aircraft Game Menu Page Design (1) _android

Source: Internet
Author: User

This article aims to achieve control of small aircraft moving around, dodge bullets, hit boss.

This section implements the Start menu interface

1. First of all, the resource files are handcuffed.

2. Divide the game status

 public static final int game_menu = 0;//Game Menu public
 static final int gameing = 1;//game public
 static final int G Ame_win = 2;//Game victory public
 static final int game_lost = 3;//game failure public
 static final int game_pause = -1;//Game Menu c6/>//Current game state (default initial in Game menu interface) public
 static int gamestate = Game_menu;

Define five states

After the method is defined in the drawing method, the entity key presses the method, raises the method, touches the screen to monitor, the logical method, the switch

In those ways add this

  switch (gamestate) {case
  game_menu: Break

   ;
  Case gameing: Break
   ;

  Case Game_win: Break

   ;
  Case Game_lost: Break

   ;
  Case Game_pause: Break
   ;
  Default: Break
   ;
  }

Here's another statement.

Declare a resources instance to facilitate loading pictures
 Private Resources res = this.getresources ();
 Declare the game needs to use the picture resources (picture statement)
 Private Bitmap bmpbackground;//game background
 private Bitmap bmpboom;//explosion effect
 private Bitmap Bmpboosboom;//boos explosion effect
 private Bitmap bmpbutton;//game start button
 Private Bitmap bmpbuttonpress;//Game Start button is clicked
 Private Bitmap bmpenemyduck;//Monster Ducks
 private Bitmap bmpenemyfly;//monster flies
 private Bitmap bmpenemyboos;//monster pig head boos
 Private Bitmap bmpgamewin;//game victory background
 private Bitmap bmpgamelost;//game background
 private Bitmap bmpplayer;// Game lead airplane
 private Bitmap bmpplayerhp;//lead airplane blood volume
 private Bitmap bmpmenu;//menu background public
 static Bitmap bmpbullet;//bullets public
 static Bitmap bmpenemybullet;//enemy bullets public
 static Bitmap Bmpbossbullet;//boss Bullets

Initializing games

/** * Surfaceview View creation, responding to this function */@Override public void surfacecreated (Surfaceholder h
 older) {screenw = This.getwidth ();
 Screenh = This.getheight ();
 Initgame ();
 Flag = true;
 instance thread th = new thread (this);
 Start thread Th.start (); }

 /** * Load game resource/private void Initgame () {//load game Resource Bmpbackground = Bitmapfactory.decoderesource (res, R.DRAWABLE.BAC
 Kground);
 Bmpboom = Bitmapfactory.decoderesource (res, r.drawable.boom);
 Bmpboosboom = Bitmapfactory.decoderesource (res, r.drawable.boos_boom);
 Bmpbutton = Bitmapfactory.decoderesource (res, R.drawable.button);
 bmpbuttonpress = Bitmapfactory.decoderesource (res, r.drawable.button_press);
 Bmpenemyduck = Bitmapfactory.decoderesource (res, r.drawable.enemy_duck);
 Bmpenemyfly = Bitmapfactory.decoderesource (res, r.drawable.enemy_fly);
 Bmpenemyboos = Bitmapfactory.decoderesource (res, r.drawable.enemy_pig);
 Bmpgamewin = Bitmapfactory.decoderesource (res, r.drawable.gamewin);
 Bmpgamelost = Bitmapfactory.decoderesource (res, r.drawable.gamelost);
 Bmpplayer = Bitmapfactory.decoderesource (res, r.drawable.player);
 BMPPLAYERHP = Bitmapfactory.decoderesource (res, R.DRAWABLE.HP);
 Bmpmenu = Bitmapfactory.decoderesource (res, r.drawable.menu); Bmpbullet = Bitmapfactory.decoderEsource (res, r.drawable.bullet);
 Bmpenemybullet = Bitmapfactory.decoderesource (res, r.drawable.bullet_enemy);

 Bmpbossbullet = Bitmapfactory.decoderesource (res, r.drawable.boosbullet);

 }

Menu Class Gamemenu

Menu class
Includes initializing drawing buttons and background images

Package COM.GSF;
Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;

Import android.view.MotionEvent;
 /** * * @author liuml * @time 2016-5-27 PM 5:43:34/public class Gamemenu {//menu background map private Bitmap bmpmenu;
 Button picture resources (pressed and not pressed below) private Bitmap Bmpbutton, bmpbuttonpress;
 The coordinates of the button are private int btnx, Btny;

 button to press the identity bit private Boolean ispress;
 The menu initializes public gamemenu (Bitmap bmpmenu, Bitmap Bmpbutton, Bitmap bmpbuttonpress) {this.bmpmenu = Bmpmenu;
 This.bmpbutton = Bmpbutton;
 this.bmpbuttonpress = bmpbuttonpress;
 X is centered, Y is immediately at the bottom of the screen btnx = MYSURFACEVIEW.SCREENW/2-Bmpbutton.getwidth ()/2;
 Btny = Mysurfaceview.screenh-bmpbutton.getheight ();
 Ispress = false;
 public void Draw (Canvas Canvas, Paint Paint) {//Draw menu background Image Canvas.drawbitmap (bmpmenu, 0, 0, Paint);
 if (ispress) {Canvas.drawbitmap (bmpbuttonpress, Btnx, Btny, paint);

 else {canvas.drawbitmap (Bmpbutton, Btnx, Btny, paint); }} public void OnToUchevent (Motionevent event) {//get current touch position int pointx = (int) event.getx ();

 int pointyy = (int) event.gety ();  If the user is pressed and moved if (event.getaction () = Motionevent.action_down | | event.getaction () = Motionevent.action_move) {// Determine if the user clicks the button if (Pointx > Btnx && pointx < btnx + bmpbutton.getwidth ()) {if (Pointyy > Btny &&amp ;
  Pointyy < Btny + bmpbutton.getheight ()) {ispress = true;
  else {ispress = false;
  } else {ispress = false; {//When used to lift action} else if (event.getaction () = = motionevent.action_up) {//Determine if the button is raised to prevent the user from moving elsewhere if (Pointx > B tnx && Pointx < btnx + bmpbutton.getwidth ()) {if pointyy > Btny && pointyy < Btny + Bmpbutton . GetHeight ()) {ispress = false;//after lifting reset button status is not pressed state//change current game status to start game Mysurfaceview.gamestate = Mysurfaceview.
  gameing;

 }
  }
 }

 }
}

Then use the Gamemenu in the Mysurfaceview to use the menu class

public class Mysurfaceview extends Surfaceview implements Callback, Runnable {private Surfaceholder sfh;
 Private Paint Paint;
 private Thread th;
 Private Boolean flag;

 Private Canvas Canvas; 1 Definition game state constant public static final int game_menu = 0;//Game menu public static final int gameing = 1;//game public static fi nal int game_win = 2;//game wins public static final int game_lost = 3;//game failure public static final int game_pause = -1;//Game
 Menu//Current game status (Default initial in Game menu interface) public static int gamestate = Game_menu;
 Declare a resources instance to facilitate loading pictures private resources res = this.getresources (); Declare the game needs to use the picture resources (picture statement) Private Bitmap bmpbackground;//game background private Bitmap bmpboom;//explosion effect Private Bitmap bmpboosboom;/  /Boos explosion effect Private Bitmap bmpbutton;//game start button Private Bitmap bmpbuttonpress;//game Start button is clicked Private Bitmap bmpenemyduck;// Monster Duck private Bitmap bmpenemyfly;//monster fly private Bitmap bmpenemyboos;//monster pig head boos private Bitmap bmpgamewin;//game victory background P Rivate Bitmap bmpgamelost;//Game failure background Private BITMAP bmpplayer;//game lead airplane private Bitmap bmpplayerhp;//lead airplane blood volume private Bitmap bmpmenu;//menu background public static Bitmap Bmpbull et;//bullets public static Bitmap bmpenemybullet;//enemy bullets public static Bitmap bmpbossbullet;//boss Bullets public static int SCR
 Eenw;

 public static int screenh;

 Private Gamemenu Gamemenu;
 /** * Surfaceview initialization function/public Mysurfaceview {super (context);
 SFH = This.getholder ();
 Sfh.addcallback (this);
 Paint = new paint ();
 Paint.setcolor (Color.White);
 Paint.setantialias (TRUE);
 Setfocusable (TRUE); /** * Surfaceview View creation, response to this function/@Override public void surfacecreated (Surfaceholder holder) {screenw = This.getwid
 Th ();
 Screenh = This.getheight ();
 Initgame ();
 Flag = true;
 instance thread th = new thread (this);
 Start thread Th.start (); /** * Load game resource/private void Initgame () {//load game resource Bmpbackground = bitmapfactory. Decoderesource (Res, r.drawab
 Le.background);
 Bmpboom = Bitmapfactory.decoderesource (res, r.drawable.boom); BmpboosboOM = Bitmapfactory.decoderesource (res, r.drawable.boos_boom);
 Bmpbutton = Bitmapfactory.decoderesource (res, R.drawable.button);
 bmpbuttonpress = Bitmapfactory.decoderesource (res, r.drawable.button_press);
 Bmpenemyduck = Bitmapfactory.decoderesource (res, r.drawable.enemy_duck);
 Bmpenemyfly = Bitmapfactory.decoderesource (res, r.drawable.enemy_fly);
 Bmpenemyboos = Bitmapfactory.decoderesource (res, r.drawable.enemy_pig);
 Bmpgamewin = Bitmapfactory.decoderesource (res, r.drawable.gamewin);
 Bmpgamelost = Bitmapfactory.decoderesource (res, r.drawable.gamelost);
 Bmpplayer = Bitmapfactory.decoderesource (res, r.drawable.player);
 BMPPLAYERHP = Bitmapfactory.decoderesource (res, R.DRAWABLE.HP);
 Bmpmenu = Bitmapfactory.decoderesource (res, r.drawable.menu);
 Bmpbullet = Bitmapfactory.decoderesource (res, r.drawable.bullet);
 Bmpenemybullet = Bitmapfactory.decoderesource (res, r.drawable.bullet_enemy);

 Bmpbossbullet = bitmapfactory. Decoderesource (res, r.drawable.boosbullet);
 Menu class instantiationGamemenu = new Gamemenu (Bmpmenu, Bmpbutton, bmpbuttonpress);
  /** * Game drawing/public void Mydraw () {try {canvas = Sfh.lockcanvas ();
  if (canvas!= null) {Canvas.drawcolor (color.white);
   Drawing functions vary according to game state to draw switch (gamestate) {case GAME_MENU:gameMenu.draw (canvas, paint);
  Break

  Case Gameing:break;
  Case Game_win:break;
  Case Game_lost:break;
  Case Game_pause:break;
  Default:break; (Exception e) {//Todo:handle Exception} finally {if (canvas!= null) sfh.unlockcanvasandpost (c
 Anvas); /** * Touchscreen Event Monitor/@Override public boolean ontouchevent (Motionevent event) {switch (gamestate) {case Game_men
  U:gamemenu.ontouchevent (event);
 Break

 Case Gameing:break;
 Case Game_win:break;
 Case Game_lost:break;
 Case Game_pause:break;
 return true; /** * Key Event Monitor/@Override public boolean onKeyDown (int keycode, keyevent event) {switch (gamestate) {case GAME

  _menu:Break

 Case Gameing:break;
 Case Game_win:break;
 Case Game_lost:break;
 Case Game_pause:break;
 Return Super.onkeydown (KeyCode, event);
 @Override public boolean onKeyUp (int keycode, keyevent event) {switch (gamestate) {case game_menu:break;

 Case Gameing:break;
 Case Game_win:break;
 Case Game_lost:break;
 Case Game_pause:break;
 Return Super.onkeyup (KeyCode, event);
 /** * Game logic/private void logic () {switch (gamestate) {case game_menu:break;

 Case Gameing:break;
 Case Game_win:break;
 Case Game_lost:break;
 Case Game_pause:break;
  @Override public void Run () {a while (flag) {Long start = System.currenttimemillis ();
  Mydraw ();
  Logic ();
  Long end = System.currenttimemillis ();
  try {if (End-start <) {Thread.Sleep (End-start));
  } catch (Interruptedexception e) {e.printstacktrace (); /** * Surfaceview view state changed in response to this function */@Override public void SurfaCechanged (surfaceholder holder, int format, int width, int height) {}/** * Surfaceview View dies, respond to this function */@Override
 public void surfacedestroyed (Surfaceholder holder) {flag = false;

 }

}

Effect Chart:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.