Cách Tạo Game Online Trên Điện Thoại

Share:
Mục tiêu của tài liệu hướng dẫn bạn làm thân quen với một vài kỹ thuật đơn giản dễ dàng trong lập trình sẵn Game 2 chiều Android. Bao gồm:
Sử dụng SuffaceViewVẽ bên trên CanvasChuyển động của các nhân trang bị game.Tương tác với động tác cử chỉ của bạn chơi
Trong tài liệu này tôi đã hướng dẫn chúng ta lập trình từng bước, chính vì vậy bạn phải đọc và thực hành từ bên trên xuống dưới. Chúng ta sẽ viết từng phiên bạn dạng của trò đùa từ 1 cho tới phiên bản cuối thuộc (Release).

Bạn đang đọc: Cách tạo game online trên điện thoại


*

*

*

*

*

Chú ý rằng nhiều người đang tạo một trò nghịch 2D bên trên Android, bởi vì vậy giao diện của trò chơi bắt buộc do bạn vẽ ra, chính vì vậy chúng ta không cần một file như thể activity_main.xml.
Copy các hình ảnh này vào thư mục drawable của project. Tạo new thư mục raw, và copy file explosion.wavbackground.mp3 vào thư mục này.
Với trò chơi bạn phải sét đặt hình ảnh nền và một điều quan tiền trọng bạn cần phải sét chế độ FullScreen (Đầy màn hình).

package org.o7planning.android2dgame; import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity
Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); // set fullscreen this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // phối No Title this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Tiếp theo sét để kiểu màn hình nằm ngang (Landscape). Bạn phải sét để trong AndroidManifest.xml.
Tiếp theo bạn phải viết code nhằm hiển thị nhân thứ trò nghịch trên màn hình và di chuyển nó trường đoản cú trái qua đề xuất theo một tốc độ nào đó.
Với một nhân thứ trong trò chơi chúng ta chỉ bao gồm một tệp tin ảnh, dẫu vậy file hình ảnh được chia nhỏ ra làm nhiều vùng diễn đạt các hành vi khác nhau của nhân vật.

Xem thêm: Những Vị Vua Cuối Cùng Của Việt Nam, Điều Chưa Kể Về Vua Bảo Đại


Sử dụng code chúng ta cũng có thể vẽ một vùng hình ảnh lên đối tượng người tiêu dùng Canvas của trò chơi, tại chế tạo độ x, y. áp dụng vòng lặp for để thường xuyên vẽ lại trên Canvas bạn có thể tạo ra sự hoạt động của nhân vật.
Trong lúc lập trình Game bạn cũng cần được tính mang đến hướng dịch chuyển của nhân vật trong trò chơi, gia tốc của nhân vật.
package org.o7planning.android2dgame;import android.graphics.Bitmap;public abstract class GameObject protected Bitmap image; protected final int rowCount; protected final int colCount; protected final int WIDTH; protected final int HEIGHT; protected final int width; protected final int height; protected int x; protected int y; public GameObject(Bitmap image, int rowCount, int colCount, int x, int y) this.image = image; this.rowCount= rowCount; this.colCount= colCount; this.x= x; this.y= y; this.WIDTH = image.getWidth(); this.HEIGHT = image.getHeight(); this.width = this.WIDTH/ colCount; this.height= this.HEIGHT/ rowCount; protected Bitmap createSubImageAt(int row, int col) // createBitmap(bitmap, x, y, width, height). Bitmap subImage = Bitmap.createBitmap(image, col* width, row* height ,width,height); return subImage; public int getX() return this.x; public int getY() return this.y; public int getHeight() return height; public int getWidth() return width;
package org.o7planning.android2dgame;import android.graphics.Bitmap;import android.graphics.Canvas;public class ChibiCharacter extends GameObject { private static final int ROW_TOP_TO_BOTTOM = 0; private static final int ROW_RIGHT_TO_LEFT = 1; private static final int ROW_LEFT_TO_RIGHT = 2; private static final int ROW_BOTTOM_TO_TOP = 3; // Row index of Image are being used. Private int rowUsing = ROW_LEFT_TO_RIGHT; private int colUsing; private Bitmap<> leftToRights; private Bitmap<> rightToLefts; private Bitmap<> topToBottoms; private Bitmap<> bottomToTops; // Velocity of trò chơi character (pixel/millisecond) public static final float VELOCITY = 0.1f; private int movingVectorX = 10; private int movingVectorY = 5; private long lastDrawNanoTime =-1; private GameSurface gameSurface; public ChibiCharacter(GameSurface gameSurface, Bitmap image, int x, int y) { super(image, 4, 3, x, y); this.gameSurface= gameSurface; this.topToBottoms = new Bitmap; // 3 this.rightToLefts = new Bitmap; // 3 this.leftToRights = new Bitmap; // 3 this.bottomToTops = new Bitmap; // 3 for(int col = 0; col= this.colCount) this.colUsing =0; // Current time in nanoseconds long now = System.nanoTime(); // Never once did draw. If(lastDrawNanoTime==-1) lastDrawNanoTime= now; // Change nanoseconds to milliseconds (1 nanosecond = 1000000 milliseconds). Int deltaTime = (int) ((now - lastDrawNanoTime)/ 1000000 ); // Distance moves float distance = VELOCITY * deltaTime; double movingVectorLength = Math.sqrt(movingVectorX* movingVectorX + movingVectorY*movingVectorY); // Calculate the new position of the game character. This.x = x + (int)(distance* movingVectorX / movingVectorLength); this.y = y + (int)(distance* movingVectorY / movingVectorLength); // When the game"s character touches the edge of the screen, then change direction if(this.x this.gameSurface.getWidth() -width) this.x= this.gameSurface.getWidth()-width; this.movingVectorX = - this.movingVectorX; if(this.y this.gameSurface.getHeight()- height) this.y= this.gameSurface.getHeight()- height; this.movingVectorY = - this.movingVectorY ; // rowUsing if( movingVectorX > 0 ) { if(movingVectorY > 0 && Math.abs(movingVectorX) 0 && Math.abs(movingVectorX)
package org.o7planning.android2dgame;import android.graphics.Canvas;import android.view.SurfaceHolder;public class GameThread extends Thread { private boolean running; private GameSurface gameSurface; private SurfaceHolder surfaceHolder; public GameThread(GameSurface gameSurface, SurfaceHolder surfaceHolder) this.gameSurface= gameSurface; this.surfaceHolder= surfaceHolder;
Override public void run() { long startTime = System.nanoTime(); while(running) { Canvas canvas= null; try // Get Canvas from Holder và lock it. Canvas = this.surfaceHolder.lockCanvas(); // Synchronized synchronized (canvas) this.gameSurface.update(); this.gameSurface.draw(canvas); catch(Exception e) // do nothing. finally if(canvas!= null) // Unlock Canvas. This.surfaceHolder.unlockCanvasAndPost(canvas); long now = System.nanoTime() ; // Interval khổng lồ redraw trò chơi // (Change nanoseconds to milliseconds) long waitTime = (now - startTime)/1000000; if(waitTime
Lớp GameSurface mô phỏng toàn bộ bề mặt của trò chơi, class này mở rộng từ SurfaceView, SurfaceView cất một đối tượng người dùng Canvas, các đối tượng người tiêu dùng trong trò chơi sẽ tiến hành vẽ lên Canvas.
package org.o7planning.android2dgame;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.view.SurfaceHolder;import android.view.SurfaceView;public class GameSurface extends SurfaceView implements SurfaceHolder.Callback private GameThread gameThread; private ChibiCharacter chibi1; public GameSurface(Context context) super(context); // Make game Surface focusable so it can handle events. . This.setFocusable(true); // Sét callback. This.getHolder().addCallback(this); public void update() this.chibi1.update();
Override public void draw(Canvas canvas) super.draw(canvas); this.chibi1.draw(canvas); // Implements method of SurfaceHolder.Callback
Override public void surfaceCreated(SurfaceHolder holder) Bitmap chibiBitmap1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi1); this.chibi1 = new ChibiCharacter(this,chibiBitmap1,100,50); this.gameThread = new GameThread(this,holder); this.gameThread.setRunning(true); this.gameThread.start(); // Implements method of SurfaceHolder.Callback
Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) // Implements method of SurfaceHolder.Callback
Override public void surfaceDestroyed(SurfaceHolder holder) boolean retry= true; while(retry) try this.gameThread.setRunning(false); // Parent thread must wait until the end of GameThread. This.gameThread.join(); catch(InterruptedException e) e.printStackTrace(); retry= true;
package org.o7planning.android2dgame;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity
Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); // phối fullscreen this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // phối No Title this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.setContentView(new GameSurface(this));
Tiếp theo chúng ta có xử lý sự khiếu nại khi người dùng chạm vào màn hình, nhân vật trò đùa sẽ đuổi theo hướng các bạn đã chạm. Bạn cần xử lý sự khiếu nại này bên trên lớp GameSurface.
Overridepublic boolean onTouchEvent(MotionEvent event) if (event.getAction() == MotionEvent.ACTION_DOWN) int x= (int)event.getX(); int y = (int)event.getY(); int movingVectorX =x- this.chibi1.getX() ; int movingVectorY =y- this.chibi1.getY() ; this.chibi1.setMovingVector(movingVectorX,movingVectorY); return true; return false;
package org.o7planning.android2dgame;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;public class GameSurface extends SurfaceView implements SurfaceHolder.Callback private GameThread gameThread; private ChibiCharacter chibi1; public GameSurface(Context context) super(context); // Make trò chơi Surface focusable so it can handle events. This.setFocusable(true); // set callback. This.getHolder().addCallback(this); public void update() this.chibi1.update();
Override public boolean onTouchEvent(MotionEvent event) if (event.getAction() == MotionEvent.ACTION_DOWN) int x= (int)event.getX(); int y = (int)event.getY(); int movingVectorX =x- this.chibi1.getX() ; int movingVectorY =y- this.chibi1.getY() ; this.chibi1.setMovingVector(movingVectorX,movingVectorY); return true; return false;
Override public void draw(Canvas canvas) super.draw(canvas); this.chibi1.draw(canvas); // Implements method of SurfaceHolder.Callback
Override public void surfaceCreated(SurfaceHolder holder) Bitmap chibiBitmap1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi1); this.chibi1 = new ChibiCharacter(this,chibiBitmap1,100,50); this.gameThread = new GameThread(this,holder); this.gameThread.setRunning(true); this.gameThread.start(); // Implements method of SurfaceHolder.Callback
Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) // Implements method of SurfaceHolder.Callback
Override public void surfaceDestroyed(SurfaceHolder holder) boolean retry= true; while(retry) try this.gameThread.setRunning(false); // Parent thread must wait until the end of GameThread. This.gameThread.join(); catch(InterruptedException e) e.printStackTrace(); retry= true;
Bạn hoàn toàn có thể tạo thêm những nhân đồ vật khác vào trò chơi, ở chỗ này tôi thêm một nhân vật vật dụng 2. Sửa lại code của lớp GameSurface:
package org.o7planning.android2dgame;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import java.util.ArrayList;import java.util.List;public class GameSurface extends SurfaceView implements SurfaceHolder.Callback private GameThread gameThread; private final danh mục chibiList = new ArrayList(); public GameSurface(Context context) super(context); // Make game Surface focusable so it can handle events. This.setFocusable(true); // mix callback. This.getHolder().addCallback(this); public void update() for(ChibiCharacter chibi: chibiList) chibi.update();
Override public boolean onTouchEvent(MotionEvent event) if (event.getAction() == MotionEvent.ACTION_DOWN) int x= (int)event.getX(); int y = (int)event.getY(); for(ChibiCharacter chibi: chibiList) int movingVectorX =x- chibi.getX() ; int movingVectorY =y- chibi.getY() ; chibi.setMovingVector(movingVectorX, movingVectorY); return true; return false;
Override public void draw(Canvas canvas) super.draw(canvas); for(ChibiCharacter chibi: chibiList) chibi.draw(canvas); // Implements method of SurfaceHolder.Callback
Override public void surfaceCreated(SurfaceHolder holder) Bitmap chibiBitmap1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi1); ChibiCharacter chibi1 = new ChibiCharacter(this,chibiBitmap1,100,50); Bitmap chibiBitmap2 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi2); ChibiCharacter chibi2 = new ChibiCharacter(this,chibiBitmap2,300,150); this.chibiList.add(chibi1); this.chibiList.add(chibi2); this.gameThread = new GameThread(this,holder); this.gameThread.setRunning(true); this.gameThread.start(); // Implements method of SurfaceHolder.Callback
Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) // Implements method of SurfaceHolder.Callback
Override public void surfaceDestroyed(SurfaceHolder holder) boolean retry= true; while(retry) try this.gameThread.setRunning(false); // Parent thread must wait until the kết thúc of GameThread. This.gameThread.join(); catch(InterruptedException e) e.printStackTrace(); retry= true;
Đôi khi bạn cần phải xử lý một vài hiệu ứng mang đến trò chơi, chẳng hạn bạn đã điều khiển một chiếc máy bay, khi nó rơi xuống khu đất máy cất cánh phát nổ, vậy nổ là 1 hiệu ứng. Vào phần này tôi đang mô phỏng khi chúng ta chạm (click) vào nhân vật Chibi, nó vẫn phát nổ.
Lớp Explosion mô phỏng một vụ nổ, khi bạn click vào nhân đồ vật Chibi, nó bị loại bỏ ra ngoài trò chơi và một đối tượng người sử dụng Explosion được cung cấp trò nghịch tại địa chỉ của nhân thiết bị Chibi vừa bị loại bỏ.
package org.o7planning.android2dgame; import android.graphics.Bitmap;import android.graphics.Canvas;public class Explosion extends GameObject private int rowIndex = 0 ; private int colIndex = -1 ; private boolean finish= false; private GameSurface gameSurface; public Explosion(GameSurface GameSurface, Bitmap image, int x, int y) super(image, 5, 5, x, y); this.gameSurface= GameSurface; public void update() this.colIndex++; if(this.colIndex >= this.colCount) this.colIndex =0; this.rowIndex++; if(this.rowIndex>= this.rowCount) this.finish= true; public void draw(Canvas canvas) if(!finish) Bitmap bitmap= this.createSubImageAt(rowIndex,colIndex); canvas.drawBitmap(bitmap, this.x, this.y,null); public boolean isFinish() return finish;
package org.o7planning.android2dgame;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class GameSurface extends SurfaceView implements SurfaceHolder.Callback { private GameThread gameThread; private final menu chibiList = new ArrayList(); private final danh sách explosionList = new ArrayList(); public GameSurface(Context context) super(context); // Make trò chơi Surface focusable so it can handle events. This.setFocusable(true); // Sét callback. This.getHolder().addCallback(this);
Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) int x= (int)event.getX(); int y = (int)event.getY(); Iterator iterator= this.chibiList.iterator(); while(iterator.hasNext()) ChibiCharacter chibi = iterator.next(); if( chibi.getX() iterator= this.explosionList.iterator(); while(iterator.hasNext()) Explosion explosion = iterator.next(); if(explosion.isFinish()) // If explosion finish, Remove the current element from the iterator & list. Iterator.remove(); continue;
Override public void draw(Canvas canvas) super.draw(canvas); for(ChibiCharacter chibi: chibiList) chibi.draw(canvas); for(Explosion explosion: this.explosionList) explosion.draw(canvas); // Implements method of SurfaceHolder.Callback
Override public void surfaceCreated(SurfaceHolder holder) Bitmap chibiBitmap1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi1); ChibiCharacter chibi1 = new ChibiCharacter(this,chibiBitmap1,100,50); Bitmap chibiBitmap2 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi2); ChibiCharacter chibi2 = new ChibiCharacter(this,chibiBitmap2,300,150); this.chibiList.add(chibi1); this.chibiList.add(chibi2); this.gameThread = new GameThread(this,holder); this.gameThread.setRunning(true); this.gameThread.start(); // Implements method of SurfaceHolder.Callback
Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) // Implements method of SurfaceHolder.Callback
Override public void surfaceDestroyed(SurfaceHolder holder) boolean retry= true; while(retry) try this.gameThread.setRunning(false); // Parent thread must wait until the over of GameThread. This.gameThread.join(); catch(InterruptedException e) e.printStackTrace(); retry= true;
Tiếp theo bạn cần thêm hiệu ứng music vào trò chơi, ví dụ điển hình âm thanh nền của trò chơi, âm thanh tiếng nổ khi nhân vật dụng Chibi bị phá hủy.
package org.o7planning.android2dgame;import android.graphics.Bitmap;import android.graphics.Canvas;public class Explosion extends GameObject private int rowIndex = 0 ; private int colIndex = -1 ; private boolean finish= false; private GameSurface gameSurface; public Explosion(GameSurface GameSurface, Bitmap image, int x, int y) super(image, 5, 5, x, y); this.gameSurface= GameSurface; public void update() this.colIndex++; // Play sound explosion.wav. If(this.colIndex==0 && this.rowIndex==0) this.gameSurface.playSoundExplosion(); if(this.colIndex >= this.colCount) this.colIndex =0; this.rowIndex++; if(this.rowIndex>= this.rowCount) this.finish= true; public void draw(Canvas canvas) if(!finish) Bitmap bitmap= this.createSubImageAt(rowIndex,colIndex); canvas.drawBitmap(bitmap, this.x, this.y,null); public boolean isFinish() return finish;
package org.o7planning.android2dgame;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.media.AudioAttributes;import android.media.AudioManager;import android.media.SoundPool;import android.os.Build;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class GameSurface extends SurfaceView implements SurfaceHolder.Callback { private GameThread gameThread; private final các mục chibiList = new ArrayList(); private final list explosionList = new ArrayList(); private static final int MAX_STREAMS=100; private int soundIdExplosion; private int soundIdBackground; private boolean soundPoolLoaded; private SoundPool soundPool; public GameSurface(Context context) super(context); // Make game Surface focusable so it can handle events. This.setFocusable(true); // Sét callback. This.getHolder().addCallback(this); this.initSoundPool(); private void initSoundPool() // With app android API >= 21. If (Build.VERSION.SDK_INT >= 21 ) AudioAttributes audioAttrib = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_GAME) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build(); SoundPool.Builder builder= new SoundPool.Builder(); builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS); this.soundPool = builder.build(); // With app android API iterator= this.chibiList.iterator(); while(iterator.hasNext()) ChibiCharacter chibi = iterator.next(); if( chibi.getX() iterator= this.explosionList.iterator(); while(iterator.hasNext()) Explosion explosion = iterator.next(); if(explosion.isFinish()) // If explosion finish, Remove the current element from the iterator & list. Iterator.remove(); continue;
Override public void draw(Canvas canvas) super.draw(canvas); for(ChibiCharacter chibi: chibiList) chibi.draw(canvas); for(Explosion explosion: this.explosionList) explosion.draw(canvas); // Implements method of SurfaceHolder.Callback
Override public void surfaceCreated(SurfaceHolder holder) Bitmap chibiBitmap1 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi1); ChibiCharacter chibi1 = new ChibiCharacter(this,chibiBitmap1,100,50); Bitmap chibiBitmap2 = BitmapFactory.decodeResource(this.getResources(),R.drawable.chibi2); ChibiCharacter chibi2 = new ChibiCharacter(this,chibiBitmap2,300,150); this.chibiList.add(chibi1); this.chibiList.add(chibi2); this.gameThread = new GameThread(this,holder); this.gameThread.setRunning(true); this.gameThread.start(); // Implements method of SurfaceHolder.Callback
Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) // Implements method of SurfaceHolder.Callback
Override public void surfaceDestroyed(SurfaceHolder holder) boolean retry= true; while(retry) try this.gameThread.setRunning(false); // Parent thread must wait until the end of GameThread. This.gameThread.join(); catch(InterruptedException e) e.printStackTrace(); retry= true;

Bài viết liên quan