[Android] A simple "cloud game" -- the first level of the push box

Source: Internet
Author: User
This article briefly introduces how to use the concept of "cloud" to implement a simple push box game and use socket to communicate with servers. Each time the client (Android) sends the operation command to the server, the server performs judgment and other processing, generates an image and returns it to the client. That is, the client only displays the images sent back by the server after sending commands, and the logic judgment is handed over to the server. After the client is displayed on the interface, you can use the upper, lower, and lower buttons to perform operations. The victory screen is simple. Press the return key twice to exit.

The server side is implemented by Java application. It uses the serversocket class and waits for the client to send a request in the thread for processing and returns an image. The two-dimensional array is used for storage, the program is not well modified, and the write is dead. The 8-by-8 matrix is used, and different numbers in the annotation indicate what the meaning is. Each operation is used to determine the meaning, the main code is as follows:
Class serverthread extends thread {private Socket socket; bufferedimage image; int imagewidth = 400; // The Image Width int imageheight = 400; // The Image Height private int x = imagewidth/2; private int y = imageheight/2; bufferedimage man = NULL; bufferedimage road = NULL; bufferedimage box = NULL; bufferedimage block = NULL; bufferedimage aim = NULL; bufferedimage win = NULL; // start position of the villain int Mani = 4; int manj = 4; // The obstacle is 0, the road is 1, and the minor is 2. The box is 3, the target is 4, and the null value is-1 Private Static int [] [] matrix = {-1,-1, 0, 0, 0,-1, -1,-1}, {-1,-1, 0, 4, 0,-1,-1,-1}, {-1,-1, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 3, 1, 3, 4, 0}, {0, 4, 1, 3, 2, 0, 0, 0}, {0, 0, 0, 0, 3, 0,-1,-1,-1}, {-1,-1,-1, 0, 4, 0,-1,-1}, {-1,-1,-1, 0, 0,-1,-1 }}; public serverthread (socket s) {system. out. println ("constructor serverthread... "); this. socket = s; try {MAN = ImageIO. read (n EW file ("images/man.png"); Road = ImageIO. read (new file ("images/road.jpg"); box = ImageIO. read (new file ("images/box.jpg"); block = ImageIO. read (new file ("images/block.jpg"); Aim = ImageIO. read (new file ("images/aim.jpg"); win = ImageIO. read (new file ("images/win.png");} catch (exception e) {e. printstacktrace () ;}} public static Boolean judgewin () {If (Matrix [1] [3] = 3 & matrix [3] [6] = 3 & matrix [4] [1] = = 3 & matrix [6] [4] = 3) {return true;} return false;} public static Boolean CANGO (int x, int y) {If (Matrix [x] [Y] = 1 | matrix [x] [Y] = 4) {return true;} return false;} public void run () {try {// create a serversocket object and have it listen to inputstream in = socket on the port. getinputstream (); outputstream out = socket. getoutputstream (); While (true) {// call the serversocket accept () method to receive the request string MSG = readstring (in); system. out. printl N ("MSG:" + MSG); If (MSG. equals ("right") {If (manj <7) // prevents cross-border {If (CANGO (Mani, manj + 1 )) {matrix [Mani] [manj] = 1; matrix [Mani] [++ manj] = 2 ;} else if (manj <6 & matrix [Mani] [manj + 1] = 3 & CANGO (Mani, manj + 2) {// differentiate between aim and road, if you leave aim on AIM, the aim should not be 1, // but the first level does not exist. In this case, matrix [Mani] [manj ++] = 1; matrix [Mani] [manj] = 2; matrix [Mani] [manj + 1] = 3 ;}} else if (MSG. equals ("Left") {If (manj> 0) // prevents cross-border {If (CANGO (Mani, manj-1) {matrix [Ma NI] [manj] = 1; matrix [Mani] [-- manj] = 2 ;} else if (manj> 1 & matrix [Mani] [manj-1] = 3 & CANGO (Mani, manj-2) {// differentiate between aim and road, if you leave aim on AIM, the aim should not be 1, // but the first level does not exist. In this case, matrix [Mani] [manj --] = 1; matrix [Mani] [manj] = 2; matrix [Mani] [manj-1] = 3 ;}} else if (MSG. equals ("up") {If (Mani> 0) // prevents cross-border {If (CANGO (mani-1, manj) {matrix [Mani] [manj] = 1; matrix [-- Mani] [manj] = 2;} else if (Mani> 1 & matrix [mani-1] [manj] = 3 & Cang O (mani-2, manj) {// It should be distinguished between aim and road, if it leaves on aim should still aim should not be 1, // but the first level does not exist. In this case, matrix [mani --] [manj] = 1; matrix [Mani] [manj] = 2; matrix [mani-1] [manj] = 3 ;}} else if (MSG. equals ("down") {If (Mani <7) // prevents cross-border {If (CANGO (Mani + 1, manj )) {matrix [Mani] [manj] = 1; matrix [++ Mani] [manj] = 2 ;} else if (Mani <6 & matrix [Mani + 1] [manj] = 3 & CANGO (Mani + 2, manj) {// differentiate between aim and road, if you leave aim on AIM, the aim should not be 1, // but the first level does not exist. In this case, matrix [Mani ++] [manj] = 1; Matrix [Mani] [manj] = 2; matrix [Mani + 1] [manj] = 3 ;}} drawimage (); fileinputstream fin = new fileinputstream ("test.jpg"); int AVL = fin. available (); writeint (Out, aVL); // system. out. println ("New Byte"); byte [] DATA = new byte [1024]; // system. out. println ("New byte over"); int readnum, writenum = 0; while (writenum <AVL) {out. flush (); readnum = fin. read (data); // write the image data to the output stream. write (data, 0, readnum); writenum + = Readnum; // system. out. println ("readnum =" + readnum);} // system. out. println ("Write over"); out. flush (); Fin. close () ;}} catch (exception e) {e. printstacktrace ();} finally {try {socket. close ();} catch (ioexception e) {e. printstacktrace () ;}} void createimage (string filelocation) {try {fileoutputstream Fos = new fileoutputstream (filelocation); bufferedoutputstream Bos = new bufferedoutputstream (FOS); JPEG Imageencoder encoder = required codec. createjpegencoder (BOS); encoder. encode (image); Bos. close ();} catch (exception e) {e. printstacktrace () ;}} public void drawimage () {image = new bufferedimage (imagewidth, imageheight, bufferedimage. type_int_rgb); graphics G = image. getgraphics (); G. setcolor (color. white); G. fillrect (0, 0, imagewidth, imageheight); For (INT I = 0; I <8; I ++) {for (Int J = 0; j <8; j ++) {Switch (Matrix [I] [J]) {Case 0: If (block! = NULL) g. drawimage (Block, J * 50, I * 50, 50, 50, null); break; Case 1: If (road! = NULL) g. drawimage (road, J * 50, I * 50, 50, 50, null); break; Case 2: If (man! = NULL) g. drawimage (road, J * 50, I * 50, 50, 50, null); G. drawimage (man, J * 50, I * 50, 50, 50, null); break; Case 3: If (box! = NULL) g. drawimage (box, J * 50, I * 50, 50, 50, null); break; Case 4: If (aim! = NULL) g. drawimage (aim, J * 50, I * 50, 50, 50, null); break ;}} if (win! = NULL & judgewin () {system. Out. println ("Win win !!! "); G. drawimage (Win, 130,130,200,200, null);} G. dispose (); createimage ("test.jpg");} public static int readint (inputstream in) throws exception {byte [] B = new byte [4]; In. read (B); int I = 0; For (Int J = 3; j> = 0; -- j) {I = I <8; I + = B [J] & 0xff;} return I;} public static string readstring (inputstream in) throws exception {int T = readint (in ); byte [] B = new byte [T]; // system. out. println ("t =" + T); In. read (B); return new string (B);} public static void writestring (outputstream out, string MSG) throws exception {byte [] B = MSG. getbytes (); writeint (out, B. length); out. write (B);} public static void writeint (outputstream out, int it) throws exception {byte [] B = new byte [4]; for (INT I = 0; I <4; ++ I) {B [I] = (byte) it; it >>= 8;} Out. write (B );}}

At the beginning of the Client client, an initial image is displayed. When you click the up and down arrow keys, a command is sent to the server. Wait for the image to be sent back, set imgeview to a new image, and change the image in the thread using the POST method, the main code is as follows:

Public class clientsocket implements runnable {private string IP; private int port; private Socket socket = NULL; outputstream out = NULL; inputstream in = NULL; imageview image = NULL; public clientsocket (string IP, int port, imageview IMG) {This. IP = IP; this. port = port; this. image = IMG;}/*** create socket connection ** @ throws exception */Public void createconnection () {try {log. E ("out", IP + "" + Po RT); socket = new socket (IP, Port); log. E ("out", "connection to the server successful! "); In = socket. getinputstream (); out = socket. getoutputstream (); New thread (this ). start ();} catch (exception e) {log. E ("out", "failed to connect to the server! "); E. printstacktrace () ;}} public void sendmessage (string MSG) throws exception {writestring (Out, MSG); log. E ("out", MSG);} public static void writestring (outputstream out, string MSG) throws exception {byte [] B = MSG. getbytes (); log. E ("ABC", "Len =" + B. length); writeint (out, B. length); out. write (B);} public static void writeint (outputstream out, int it) throws exception {byte [] B = new byte [4]; for (INT I = 0; I <4; ++ I) {B [I] = (byte) it; it >>= 8;} Out. write (B);} public void shutdownconnection () {try {If (OUT! = NULL) Out. Close (); If (in! = NULL) in. Close (); If (socket! = NULL) socket. close ();} catch (exception e) {e. printstacktrace () ;}} public static int readint (inputstream in) throws exception {byte [] B = new byte [4]; int Len = in. read (B); log. E ("ABC", "Read Len =" + Len); int I = 0; For (Int J = 3; j> = 0; -- J) {I = I <8; I + = B [J] & 0xff;} return I;} public static string readstring (inputstream in) throws exception {int T = readint (in); byte [] B = new byte [T]; In. read (B); return new string (B) ;}@ overridepublic void run () {// todo auto-generated method stubtry {// continuously monitors the data status log of the input stream. E ("ABC", "came in Run"); While (true) {// when there is data in the stream, read the data and process the log. E ("ABC", "read"); // create a data array and read the data in the stream to the array. // log. E ("ABC", "mes" + in. readutf (); int T = readint (in); log. E ("ABC", "datasize =" + T); byte [] DATA = new byte [T]; // note that no log is processed when the image size exceeds the int range. E ("ABC", "New over"); int readnum = 0; while (readnum <t) {int size = in. read (data, readnum, T-readnum); readnum + = size;} log. E ("ABC", "size =" + readnum); // create the bitmap object Bitmap Based on the file data, because bitmap will be used in the following internal classes, so it is defined as final Bitmap bitmap = bitmapfactory. decodebytearray (data, 0, Data. length); // display the image to the imageview. // This method is provided by Android because the components in the view are thread insecure. (For details, see "Appendix 2 ") image. post (New runnable () {public void run () {// display bitmap to the image. setimagebitmap (Bitmap) ;}}); // thread rest 1 S // thread. sleep (1000) ;}} catch (exception e) {log. E ("ABC", "exit"); E. printstacktrace ();}}}

Engineering address http://download.csdn.net/detail/felicitia/5575559

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.