Android Research game Development Webcam Update

Source: Internet
Author: User



Introduction to the principle of in-game camera

Updating the location of the camera in game development can determine the content of the screen display, especially the RPG game camera has a very important role, I give an example sometimes we play the RPG game when we enter a new scene triggered a script and found that the lens began to move upward Depending on the lens, the player can take a look at what is going on in this scene and what kind of plot to trigger. The way this is implemented is the game camera principle. Oberzhang learned the Android game development map editor that is needed to look under.

: First, the camera display area is also the area of the phone screen display if you need to change the location of the camera is actually changing the location of the background map using the program to drag the background map gives the player a false impression that the camera is moving rather than the background map.



The drawing principle of the map in the game describes the ID of each tile generated by the map editor to find each tile's map resource The XY coordinate of the original file to figure out where the picture is displayed. Use the program's cutting method to cut each tile out and display it in a mobile screen. The code for the cut picture looks like this:

<strong>/** * Draw part of picture in picture * * @param canvas * @param paint * @param bitmap * @param x * @param y * @param src_x * @p Aram Src_y * @param src_width * @param src_height */private void drawclipimage (canvas canvas, paint paint, Bitmap bitmap,i NT x, int y, int src_x, int src_y, int src_xp, int src_yp) {    canvas.save ();    Canvas.cliprect (x, y, x + src_xp, y + src_yp);    Canvas.drawbitmap (Bitmap, x-src_x, y-src_y, paint);    Canvas.restore ();} </strong>

Canvas.save ();
Save the canvas before cutting the picture and then after the cut is finished.
Canvas.restore ();
In the state of the canvas is reset back if you do not do so, the first picture will block all the pictures after cutting, so we must remember this.

Here's how each tile works, and here are some friends who might want to ask, if my map is infinitely large, wouldn't it be an infinite loop based on this method? In fact, the screen needs to draw the number of tiles only need to draw within the screen display area, outside the screen real-world we do not have to consider mapping only need to update the map coordinate data can be, for example, my simulator screen size is 320x480 then I actually draw the number of tiles is only ten X15 (block). In fact, there is a more important drawing technology in game development is the double buffering technology which can be used to solve the screen flicker problem, I will detail in the next chapter.

Yesterday, a friend and I proposed this way of using arrays to draw a map unscientific I agree with his point of view, why not science? Because now we have only one scene. We're going to use an array to draw the map. What if there are 100 scenes in our game? Do we have to write 100 arrays in the program? In fact, in the actual development we are the information of these maps into an XML file into the game's package, the player will be in the game when switching the scene of the map XML file is read in the current game scene. In fact, these XML files are also the two-bit array information to save the map, but the advantage is that data-driven programmers do not have to define n the number of sets of N to judge only need to base on the ID of the currently switched scene can get the information of the map is very convenient also can avoid the code due to clerical errors.

But no matter how the data is processed in any way, its drawing principle is the same.

How to update the in-game camera

: The program takes random numbers to update the game camera

At present, update the camera position every 10000 milliseconds (random number) We have the position of the camera after we can calculate the relative display position of the background image to move the position of the background image can give the player to create a camera in the mobile illusion.

The map block is the number of tiles I've been 20x20.


Use CTRL + C to copy and paste with Ctrl + V. Package Cn.m15.xys;import Java.io.InputStream;
Import Java.util.random;import android.app.Activity;
Import Android.content.Context;
Import android.content.res.Resources;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.os.Bundle;
Import Android.view.Display;
Import Android.view.View;
Import Android.view.Window;
Import Android.view.windowmanager;public class Cameraacitvity extends Activity {Mapview mmapview = null;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Full Screen display window
Requestwindowfeature (Window.feature_no_title);
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Get Screen width height
Display display = Getwindowmanager (). Getdefaultdisplay ();
Display a custom game view
Mmapview = new Mapview (This,display.getwidth (), Display.getheight ());
Setcontentview (Mmapview);
} public class Mapview extends View {//Tile block Width height
Public final static int tile_width = 32;
Public final static int tile_height = 32; The number of tile blocks with a wide height
Public final static int tile_width_count = 20;
Public final static int tile_height_count = 20; The width and height of the map
Public final static int map_width = 640;
Public final static int map_height = 640; Lens Movement Range
Public final static int camera_move = 10; The width and height of the screen
public int mscreenwidth = 0;
public int mscreenheight = 0; The array element is 0 and nothing is drawn.
Public final static int tile_null = 0;
First layer game view map array
Public int[][] Mmapview = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1,
1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 1, 1, 1,
1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1,
1, 1, 1},
{1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137, 1, 1,
1, 1, 1, 1, 1},
{137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 137, 1, 1, 1},
{137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 137, 1, 1, 1},
{137, 137, 137, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 1, 1, 1, 1},
{1, 137, 137, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 1, 137, 1},
{1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 1},
{1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 1, 1, 1, 137, 137,
137, 137, 137, 137},
{1, 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 1, 1, 1, 137,
137, 137, 137, 1},
{1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 1, 1, 1,
1, 137, 137, 137},
{1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 137, 137},
{1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
137, 137, 137},
{1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 1, 1, 1, 1,
1, 137, 137, 137},
{1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 1, 1, 1, 1,
1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 1, 1},
{1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 1, 1},
{1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1}}; Second level game entity actor array
Public int[][] Mmapacotor = {
{143, 144, 0, 102, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185, 186, 187, 188},
{151, 152, 0, 110, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193, 194, 195, 196},
{159, 160, 0, 110, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201, 202, 203, 204},
{0, 0, 0, 126, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209, 210, 211, 212},
{0, 0, 0, 134, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 218, 219, 220},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 227, 228},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{102, 103, 103, 103, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{110, 111, 111, 111, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{110, 111, 111, 111, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{126, 127, 127, 127, 128, 0, 0, 0, 0, 0, 0, 0, 165, 166, 0, 0,
0, 0, 0, 0},
{123, 124, 124, 124, 125, 0, 0, 0, 0, 0, 0, 0, 173, 174, 175, 176,
0, 0, 0, 0},
{229, 230, 231, 232, 0, 0, 0, 0, 0, 0, 0, 0, 181, 182, 183, 184,
0, 0, 0, 0},
{237, 238, 239, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{245, 246, 247, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0},
{0, 254, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 233, 234, 235,
236, 0, 0, 0},
{0, 262, 263, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 241, 242, 243,
244, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 250, 251,
0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 258, 259,
0, 0, 143, 144}
}; Third layer game collision Physical Layer Array
The next chapter introduces
.....//game map resources, and so on.//
Bitmap mbitmap = null; Resource file
Resources mresources = null; Game brushes
Paint mpaint = null; Number of horizontal vertical tile blocks
int mwidthtilecount = 0;
int mheighttilecount = 0; Number of horizontal vertical tile blocks
int mbitmapwidth = 0;
int mbitmapheight = 0; The focal point of the camera is 0.0 points in the center.
int mcameraposx = 0;
int mcameraposy = 0; Anchor Point coordinates of the top left corner of the map
int mmapposx = 0;
int mmapposy = 0; Record last time private long statrtime = 0; /**
* Construction Method
*
* @param context
*/
Public Mapview (Context context,int screenwidth, int screenheight) {
Super (context);
Mscreenheight = ScreenHeight;
Mscreenwidth = ScreenWidth;
Mpaint = new Paint ();
Mbitmap = Readbitmap (context, r.drawable.map);
Mbitmapwidth = Mbitmap.getwidth ();
Mbitmapheight = Mbitmap.getheight ();
Mwidthtilecount = Mbitmapwidth/tile_width;
Mheighttilecount = Mbitmapheight/tile_height;
Statrtime = System.currenttimemillis ();
} @Override
protected void OnDraw (canvas canvas) {
Updatecamera ();
Drawmap (canvas, Mbitmap);
Drawrecttext (canvas);
Super.ondraw (canvas);
Invalidate ();
} private void Drawmap (canvas canvas, Bitmap Bitmap) {
int I, J;
for (i = 0; I &lt; Tile_height_count; i++) {
for (j = 0; J &lt; Tile_width_count; J + +) {
int ViewID = Mmapview[i][j];
int actorid = Mmapacotor[i][j];
int x = (j* tile_width) + mmapposx;
int y = (i* tile_height) + mmapposy;
Draw the first layer of the map
if (ViewID &gt; Tile_null) {
Drawmaptile (ViewID, canvas, Mpaint, Bitmap, x, y);
}//Draw the map on the second floor
if (Actorid &gt; Tile_null) {
Drawmaptile (Actorid, canvas, Mpaint, Bitmap, x, y);
}
}
}
}
private void Drawrecttext (canvas canvas) {
Canvas.cliprect (0, 0,mscreenwidth, 30);
Mpaint.setcolor (Color.White);
Canvas.drawrect (0, 0,mscreenwidth, mpaint);
Mpaint.setcolor (color.red);
Canvas.drawtext ("Current camera X coordinate:" + mcameraposx + "Current camera y-coordinate:" + mcameraposy, 0, Mpaint);
}
private void Updatecamera () {
Long nowtime = System.currenttimemillis ();
Update the camera location every 100 milliseconds
if (nowtime-statrtime &gt; 1000) {
Randomly get the coordinates of the camera
Mcameraposx = Utilrandom (0,map_width-mscreenwidth);
Mcameraposy = Utilrandom (0,map_height-mscreenheight);
Update map coordinates based on camera coordinates
Mmapposx =-MCAMERAPOSX;
Mmapposy =-mcameraposy;
Statrtime = Nowtime;
}
} /**
* Returns a random number
* @param Botton
* @param top
* @return
*/
private int Utilrandom (int botton, int top) {
Return ((Math.Abs () (New Random (). Nextint ())% (Top-botton)) + botton);
}
/**
* Draw a tile block based on ID
*
* @param ID
* @param canvas
* @param paint
* @param bitmap
*/
private void drawmaptile (int id, canvas canvas, paint paint,
Bitmap Bitmap, int x, int y) {
Calculates the XY coordinates in the map resource based on the IDs in the array
Because the editor defaults to 0 so the ID of the first tile is not 0 but 1 so here-1
id--;
int count = Id/mwidthtilecount;
int BITMAPX = (ID-(count * mwidthtilecount)) * tile_width;
int bitmapy = count * tile_height;
Drawclipimage (canvas, Paint, Bitmap, x, Y, bitmapx, Bitmapy,
Tile_width, Tile_height);
} /**
* Read pictures of local resources
*
* @param context
* @param resId
* @return
*/
Public Bitmap Readbitmap (context context, int resId) {
Bitmapfactory.options opt = new bitmapfactory.options ();
Opt.inpreferredconfig = Bitmap.Config.RGB_565;
Opt.inpurgeable = true;
Opt.ininputshareable = true;
Get a picture of a resource
InputStream is = Context.getresources (). Openrawresource (ResId);
Return Bitmapfactory.decodestream (IS, null, opt);
} /**
* Draw part of picture in picture
*
* @param canvas
* @param paint
* @param bitmap
* @param x
* @param y
* @param src_x
* @param src_y
* @param src_width
* @param src_height
*/
private void Drawclipimage (canvas canvas, paint paint, Bitmap Bitmap,
int x, int y, int src_x, int src_y, int src_xp, int src_yp) {
Canvas.save ();
Canvas.cliprect (x, y, x + src_xp, y + src_yp);
Canvas.drawbitmap (Bitmap, x-src_x, y-src_y, paint);
Canvas.restore ();
}    }
}

Finally, if you still think I don't have enough detail to read, it doesn't matter. I put the source code to welcome you to discuss the study together.

SOURCE Download: Cameraview

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.