Amazed | read the TXT file in the assets folder | drawing | acceleration sensor sensormanager |

Source: Internet
Author: User
Document directory
  • I. Overview
  • 2. Load the maze Map
  • Iii. Use of acceleration sensors
I. Overview

Game Rules: marble ball (marble) due to gravity acceleration movement, when hit the black area was determined as a loss, when hit the blue area was determined as a win, hit the gray area continue to move.

[]

2. Load the maze Map

(1) data describing the maze

As the level increases, the maze style also increases complexity. The style of the maze is displayed by means of the document description (txt document.

For example, a style is drawn from the following data:

 

(2) read TXT files in the assets folder

The Android system provides the/Assets Directory for each newly designed program. The files saved in this directory can be packaged in the program. The difference between/RES and/assets is that android does not generate an ID for the file in/assets. If you use files under/assets, you must specify the file path and file name.

The following code reads the maze description data from the files specified in the assets folder and saves it to an integer array.

Private Static int [] mmazedata;

/** <Br/> * load specified maze level. <br/> * @ Param activity <br/> * Activity controlled the maze, we use this load the level data <br/> * @ Param newlevel <br/> * maze level to be loaded. <br/> */<br/> void load (activity, int newlevel) {<br/> // maze data is stored in the assets folder as level1.txt, level2.txt <br/> // etc .... <br/> string mlevel = "level" + newlevel + ". TXT "; <br/> inputstream is = NULL; <br/> try {<br/> // construct our maze data array. <br/> mmazedata = new int [maze_rows * maze_cols]; <br/> // attempt to load maze data. <br/> is = activity. getassets (). open (mlevel); <br/> // We need to loop through the input stream and load each tile for <br/> // The current maze. <br/> for (INT I = 0; I <mmazedata. length; I ++) {<br/> // data is stored in Unicode so we need to convert it. <br/> mmazedata [I] = character. getnumericvalue (is. read (); <br/> // skip the "," and white space in our human readable file. <br/> is. read (); <br/> is. read (); <br/>}< br/>}catch (exception e) {<br/> log. I ("Maze", "load exception:" + E); <br/>} finally {<br/> closestream (is );

 

(3) drawing view canvas paint ondraw

The android plotting operation inherits the view

Implementation, in ondraw

Function. The invalidate and postinvalidate methods are used to refresh the interface.
After the two methods are called, The ondraw event is called to re-paint the interface.

The following code creates and updates the game interface. gametick () re-assigns a value to the drawing parameter based on the coordinate value obtained by the accelerometer.

@ Override <br/> Public void ondraw (canvas) {<br/> // update our canvas reference. <br/> mcanvas = canvas; <br/> // clear the screen. <br/> mpaint. setcolor (color. white); <br/> mcanvas. drawrect (0, 0, mcanvaswidth, mcanvasheight, mpaint); <br/> // simple state machine, draw screen depending on the current state. <br/> switch (mcurstate) {<br/> case game_running: <br/> // draw our maze first since everything else appears "on Top" of it. <br/> mmaze. draw (mcanvas, mpaint); <br/> // draw our marble and HUD. <br/> mmarble. draw (mcanvas, mpaint); <br/> // draw HUD <br/> drawhud (); <br/> break; <br/> case game_over: <br/> drawgameover (); <br/> break; <br/> case game_complete: <br/> drawgamecomplete (); <br/> break; <br/> case game_landscape: <br/> drawlandscapemode (); <br/> break; <br/>}< br/> gametick (); <br/>}


(4) display custom view of activity

Make sure that you only access the acceleration sensor service in the running status. InOnresume registration, in
Onpause anti-registration.

"To ensure that you request service updates only while in the running state, register for updates inonResume()
And unregister inonPause()
."

Public class amazedactivity extends activity {<br/> // custom view <br/> private amazedview mview; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // remove title bar. <br/> requestwindowfeature (window. feature_no_title); <br/> // setup our view, give it focus and display. <br/> mview = new amazedview (getapplicationcontext (), this); <br/> mview. setfocusable (true); <br/> setcontentview (mview); <br/>}< br/> @ override <br/> protected void onresume () {<br/> super. onresume (); <br/> mview. registerlistener (); <br/>}< br/> @ override <br/> Public void onsaveinstancestate (bundle icicle) {<br/> super. onsaveinstancestate (icicle); <br/> mview. unregisterlistener (); <br/>}< br/>}

Iii. Use of acceleration sensors

When the mobile phone screen is placed horizontally up (Z axis to day), the values of (x, y, z) are (0, 0, 10 );
When the mobile phone screen is placed horizontally down (Z axis toward the ground), the values of (x, y, z) are (0, 0,-10 );
When the mobile phone screen is placed on the left (X axis to the day), the values of (x, y, z) are (10, 0, 0 );
The values of (x, y, z) are (0, 10, 0) when the mobile phone is vertical (Y axis to day );
The other analogy is that the day is a positive number, and the day is a negative number.
Using the three values of X, Y, and Z to calculate the trigonometric function, you can precisely detect the motion of the mobile phone.

 

The function of the following code is to obtain a manager of the acceleration sensor service, create a listener, and implement its interface (onsensorchanged)
In this way, when an event is triggered, the Service Manager can call this callback function (onsensorchanged. The callback function stores the acceleration sensor coordinates.

// Http://code.google.com/android/reference/android/hardware/SensorManager.html#SENSOR_ACCELEROMETER <br/> // For an explanation on the values reported by sensor_accelerometer. <br/> private final sensorlistener msenslistener celerometer = new sensorlistener () {<br/> // method called whenever new sensor values are reported. <br/> Public void onsensorchanged (INT sensor, float [] values) {<br/> // grab the values required to respond to user movement. <br/> maccelx = values [0]; <br/> maccely = values [1]; <br/> maccelz = values [2]; <br/>}< br/> // reports when the accuracy of sensor has change <br/> // sensor_status_accuracy_high = 3 <br/> // sensor_status_accuracy_low = 1 <br/low/> // sensor_status_accuracy_medium = 2 <br/> // sensor_status_unreliable = 0 // Calibration required. <br/> Public void onaccuracychanged (INT sensor, int accuracy) {<br/> // currently not used <br/>}< br/>/** <br/> * Custom view constructor. <br/> * @ Param context <br/> * application context <br/> * @ Param activity <br/> * Activity controlling the view <br/> */<br/> Public amazedview (context, activity activity) {<br/> super (context); <br/>... <br/> // setup Accelerometer Sensor manager. <br/> msensormanager = (sensormanager) activity. getsystemservice (context. sensor_service); <br/> // register our accelerometer so we can receive values. <br/> // sensor_delay_game is the recommended rate for games <br/> msensormanager. registerlistener (msensoraccelerometer, sensormanager. sensor_accelerometer, <br/> sensormanager. sensor_delay_game); <br/>... <br/>}< br/>

4. Determine whether to win or lose

In the ondraw function, convert the coordinates of the ball to the row number and column number, calculate the index of the mmazedata Array Based on the row number and column number, and obtain the index value, 1 indicates losing, 2 indicates winning, and 0 indicates moving.

 

Reference

Http://www.pin5i.com/showtopic-android-sensormanager-demo.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.