天天今天给分享android游戏代码的知识,其中也会对android小游戏源代码进行解释,希望能解决你的问题,请看下面的文章阅读吧!

1、package com.fiveChess;import android.app.Activity;import android.os.Bundle;import android.view.Display;import android.view.Menu;import android.view.MenuItem;import android.view.Window;import android.view.WindowMar;public class MainActivity extends Activity {GameView View = null;@Overridepublic void onCreate(Bundle sedInstanceState) {super.onCreate(sedInstanceState);this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);this.getWindow().setFlags(WindowMar.LayoutParams.FLAG_FULLSCREEN,WindowMar.LayoutParams.FLAG_FULLSCREEN);Display display = this.getWindowMar().getDefaultDisplay();View = new GameView(this,display.getWidth(),display.getHeight());setContentView(View);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add("重新开始").setIcon(android.R.drawable.ic_menu_myplaces);menu.add("退出");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem ) {if(.getTitle().equals("重新开始")){View.canPlay = true;View.chess = new int[View.row][View.col];View.invalidate();}else if(.getTitle().equals("退出")){finish();}return super.onOptionsItemSelected();}}package com.fiveChess;import android.app.AlertDialog;import android.content.Context;import android.content.DialogIntece;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.view.MotionEvent;import android.view.View;public class GameView extends View {Context context = null;int screenWidth,screenHeight;String message = "";//提示轮到哪个玩家int row,col; //划线的行数和列数int stepLength = 30;//棋盘每格间距int[][] chess = null;//0代表没有棋子,1代表是黑棋,2代表白旗boolean isBlack = true;boolean canPlay = true;public GameView(Context context,int screenWidth,int screenHeight) {super(context);this.context = context;this.screenWidth = screenWidth;this.screenHeight = screenHeight;this.message = "黑棋先行";row = (screenHeight-50)/stepLength+1;col = (screenWidth-10)/stepLength+1;chess = new int[row][col];}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();paint.setColor(Color.WHITE);canvas.drawRect(0, 0, screenWidth, screenHeight, paint);//画背景paint.setColor(Color.BLUE);paint.setTextSize(25);canvas.drawText(message, (screenWidth-100)/2, 30, paint);//画顶层的字paint.setColor(Color.BLACK);//画棋盘for(int i=0;icanvas.drawLine(10, 50+istepLength, 10+(col-1)stepLength, 50+istepLength, paint);}for(int i=0;icanvas.drawLine(10+istepLength,50,10+istepLength,50+(row-1)stepLength, paint);}for(int r=0;rfor(int c=0;cif(chess[r][c] == 1){paint.setColor(Color.BLACK);paint.setStyle(Style.FILL);canvas.drawCircle(10+cstepLength, 50+rstepLength, 10, paint);}else if(chess[r][c] == 2){//画白棋paint.setColor(Color.WHITE);paint.setStyle(Style.FILL);canvas.drawCircle(10+cstepLength, 50+rstepLength, 10, paint);paint.setColor(Color.BLACK);paint.setStyle(Style.STROKE);canvas.drawCircle(10+cstepLength, 50+rstepLength, 10, paint);}}}}@Overridepublic boolean onTouchEvent(MotionEvent nt) {if(!canPlay){return false;}float x = nt.getX();float y = nt.getY();int r = Math.round((y-50)/stepLength);int c = Math.round((x-10)/stepLength);if(rrow-1 || ccol-1){return false;}if(chess[r][c]!=0){return false;}//若有棋子则不再画棋子了if(isBlack){chess[r][c] = 1;isBlack = false;message = "轮到白棋";}else{chess[r][c] = 2;isBlack = true;message = "轮到黑棋";}invalidate();if(judge(r, c,0,1)) return false;if(judge(r, c,1,0)) return false ;if(judge(r, c,1,1)) return false;if(judge(r, c,1,-1)) return false;return super.onTouchEvent(nt);}private boolean judge(int r, int c,int x,int y) {//r,c表示行和列,x表示在y方向上的偏移,y表示在x方向上的偏移int count = 1;int a = r;int b = c;while(r>=0 && r=0 && c=0 && r+x=0 && c+ycount++;if(y>0){c++;}else if(yc--;}if(x>0){r++;}else if(xr--;}}while(a>=0 && a=0 && b=0 && a-x=0 && b-ycount++;if(y>0){b--;}else if(yb++;}if(x>0){a--;}else if(xa++;}}if(count>=5){String str = "";if(isBlack){str = "白棋胜利";}else{str = "黑棋胜利";}new AlertDialog.Builder(context).setTitle("游戏结束").setMessage(str).setPositiveButton("重新开始", new DialogIntece.OnClickListener() {@Overridepublic void onClick(DialogIntece dialog, int which) {chess = new int[row][col];invalidate();}}).setNegativeButton("观看棋局", new DialogIntece.OnClickListener() {@Overridepublic void onClick(DialogIntece dialog, int which) {canPlay = false;}}).show();return true;}return false;}}PS:五子棋,无需,直接在程序里画出来的。

2、注意我发的是两个文件,一个activity,一个类文件,别把它当成一个文件了。

本文到这结束,希望上面文章对大家有所帮助。