package cn.m.xys;
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.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* name : 宣雨松
* email: xuanyusong@qq.com
* @author Administrator
*
*/
public class GameView extends SurfaceView implements Const,
SurfaceHolder.Callback, Runnable {
static GameView sInstance = null;
public static void init(Activity mActivity, int screenWidth,
int screenHeight) {
sInstance = new GameView(mActivity, screenWidth, screenHeight);
}
public static GameView getInstance() {
return sInstance;
}
// 控制循环
boolean mbLoop = false;
// 定义SurfaceHolder对象
SurfaceHolder mSurfaceHolder = null;
public static Paint sPaint = null;
public static Canvas sCanvas = null;
public static Resources sResources = null;
private int mGameState = 0;
private int mScreenWidth = 0;
private int mScreenHeight = 0;
public int[][] mGameMap = null;
private int mMapHeightLengh = 0;
private int mMapWidthLengh = 0;
private int mMapIndexX = 0;
private int mMapIndexY = 0;
public int mCampTurn = 0;
public int mCampWinner = 0;
private float mTitleSpace = 0;
private int mTitleHeight = 0;
private float mTitleIndex_x = 0;
private float mTitleIndex_y = 0;
Bitmap bitmapBg = null;
Bitmap mBlack = null;
Bitmap mWhite = null;
Context mContext = null;
public GameView(Activity activity, int screenWidth, int screenHeight) {
super(activity);
sPaint = new Paint();
sPaint.setAntiAlias(true);
sResources = getResources();
mContext = activity;
mScreenWidth = screenWidth;
mScreenHeight = screenHeight;
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
setFocusable(true);
mbLoop = true;
bitmapBg = CreatMatrixBitmap(R.drawable.status, mScreenWidth,
mScreenHeight);
mBlack = BitmapFactory.decodeResource(GameView.sResources,
R.drawable.ai);
mWhite = BitmapFactory.decodeResource(GameView.sResources,
R.drawable.human);
mTitleSpace = (float) mScreenWidth / CHESS_WIDTH;
mTitleHeight = mScreenHeight / 3;
mTitleIndex_x = (float) (mTitleSpace / 2);
mTitleIndex_y = (float) (mTitleSpace / 2);
setGameState(GS_GAME);
}
public void setGameState(int newState) {
mGameState = newState;
switch (mGameState) {
case GS_GAME:
mGameMap = new int[CHESS_HEIGHT][CHESS_WIDTH];
mMapHeightLengh = mGameMap.length;
mMapWidthLengh = mGameMap[0].length;
mCampTurn = CAMP_HERO;
break;
}
}
protected void Draw() {
sCanvas = mSurfaceHolder.lockCanvas();
if (mSurfaceHolder == null || sCanvas == null) {
return;
}
RenderGame();
mSurfaceHolder.unlockCanvasAndPost(sCanvas);
}
private void RenderGame() {
switch (mGameState) {
case GS_GAME:
DrawRect(Color.WHITE, 0, 0, mScreenWidth, mScreenHeight);
RenderMap();
break;
case GS_END:
DrawRect(Color.RED, 0, 0, mScreenWidth, mScreenHeight);
DrawString(Color.WHITE, sResources.getString(mCampWinner)
+ "胜利 点击继续游戏", 50, 50);
break;
}
}
private void RenderMap() {
int i, j;
DrawImage(bitmapBg, 0, 0, 0);
for (i = 0; i < mMapHeightLengh; i++) {
for (j = 0; j < mMapWidthLengh; j++) {
int CampID = mGameMap[i][j];
float x = (j * mTitleSpace) + mTitleIndex_x;
float y = (i * mTitleSpace) + mTitleHeight + mTitleIndex_y;
if (CampID == CAMP_HERO) {
DrawImage(mBlack, x, y, ALIGN_VCENTER | ALIGN_HCENTER);
} else if (CampID == CAMP_ENEMY) {
DrawImage(mWhite, x, y, ALIGN_VCENTER | ALIGN_HCENTER);
}
}
}
}
private void DrawRect(int color, int x, int y, int width, int height) {
sPaint.setColor(color);
sCanvas.clipRect(x, y, width, height);
sCanvas.drawRect(x, y, width, height, sPaint);
}
private void DrawString(int color, String str, int x, int y) {
sPaint.setColor(color);
sCanvas.drawText(str, x, y, sPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
UpdateTouchEvent(x, y);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(event);
}
public boolean CheckPiecesMeet(int Camp) {
int MeetCount = 0;
// 横向
for (int i = 0; i < CALU_ALL_COUNT; i++) {
int index = mMapIndexX - CALU_SINGLE_COUNT + i;
if (index < 0 || index >= mMapWidthLengh) {
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
MeetCount = 0;
continue;
}
if (mGameMap[mMapIndexY][index] == Camp) {
MeetCount++;
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
} else {
MeetCount = 0;
}
}
// 纵向
MeetCount = 0;
for (int i = 0; i < CALU_ALL_COUNT; i++) {
int index = mMapIndexY - CALU_SINGLE_COUNT + i;
if (index < 0 || index >= mMapHeightLengh) {
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
MeetCount = 0;
continue;
}
if (mGameMap[index][mMapIndexX] == Camp) {
MeetCount++;
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
} else {
MeetCount = 0;
}
}
// 右斜
MeetCount = 0;
for (int i = 0; i < CALU_ALL_COUNT; i++) {
int indexX = mMapIndexX - CALU_SINGLE_COUNT + i;
int indexY = mMapIndexY - CALU_SINGLE_COUNT + i;
if ((indexX < 0 || indexX >= mMapWidthLengh)
|| (indexY < 0 || indexY >= mMapHeightLengh)) {
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
MeetCount = 0;
continue;
}
if (mGameMap[indexY][indexX] == Camp) {
MeetCount++;
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
} else {
MeetCount = 0;
}
}
// 左斜
MeetCount = 0;
for (int i = 0; i < CALU_ALL_COUNT; i++) {
int indexX = mMapIndexX - CALU_SINGLE_COUNT + i;
int indexY = mMapIndexY + CALU_SINGLE_COUNT - i;
if ((indexX < 0 || indexX >= mMapWidthLengh)
|| (indexY < 0 || indexY >= mMapHeightLengh)) {
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
MeetCount = 0;
continue;
}
if (mGameMap[indexY][indexX] == Camp) {
MeetCount++;
if (MeetCount == CALU_SINGLE_COUNT) {
return true;
}
} else {
MeetCount = 0;
}
}
return false;
}
private void UpdateTouchEvent(int x, int y) {
switch (mGameState) {
case GS_GAME:
if (x > 0 && y > mTitleHeight) {
mMapIndexX = (int) (x / mTitleSpace);
mMapIndexY = (int) ((y - mTitleHeight) / mTitleSpace);
if (mMapIndexX > mMapWidthLengh) {
mMapIndexX = mMapWidthLengh;
}
if (mMapIndexX < 0) {
mMapIndexX = 0;
}
if (mMapIndexY > mMapHeightLengh) {
mMapIndexY = mMapHeightLengh;
}
if (mMapIndexY < 0) {
mMapIndexY = 0;
}
if (mGameMap[mMapIndexY][mMapIndexX] == CAMP_DEFAULT) {
if (mCampTurn == CAMP_HERO) {
mGameMap[mMapIndexY][mMapIndexX] = CAMP_HERO;
if (CheckPiecesMeet(CAMP_HERO)) {
mCampWinner = R.string.Role_black;
setGameState(GS_END);
} else {
mCampTurn = CAMP_ENEMY;
}
} else {
mGameMap[mMapIndexY][mMapIndexX] = CAMP_ENEMY;
if (CheckPiecesMeet(CAMP_ENEMY)) {
mCampWinner = R.string.Role_white;
setGameState(GS_END);
} else {
mCampTurn = CAMP_HERO;
}
}
}
}
break;
case GS_END:
没有合适的资源?快使用搜索试试~ 我知道了~
wuZiQi.rar_android_backgammon_backgammon game
共56个文件
class:16个
png:12个
java:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 121 浏览量
2022-09-23
12:23:14
上传
评论
收藏 1.25MB RAR 举报
温馨提示
android五子棋游戏小代码,有需要的新手朋友可以看一下
资源推荐
资源详情
资源评论
收起资源包目录
wuZiQi.rar (56个子文件)
codefans.net
FiveChess
bin
classes.dex 11KB
cn
m15
fivegame
fivechess
m
xys
GameView.class 9KB
R$layout.class 364B
Const.class 941B
R$drawable.class 455B
mainActivity.class 1KB
R$attr.class 310B
R.class 429B
R$string.class 465B
resources.ap_ 206KB
FiveChess.apk 214KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
human.png 1KB
status.png 192KB
icon.png 4KB
ai.png 1KB
values
strings.xml 273B
drawable-mdpi
icon.png 3KB
layout
main.xml 382B
assets
default.properties 362B
gen
cn
m15
fivegame
fivechess
m
xys
R.java 893B
proguard.cfg 1KB
src
cn
m
xys
mainActivity.java 920B
GameView.java 11KB
Const.java 1KB
.project 845B
.classpath 280B
AndroidManifest.xml 804B
wuZiQi
FiveChess
bin
classes.dex 11KB
cn
m15
fivegame
fivechess
m
xys
GameView.class 9KB
R$layout.class 364B
Const.class 941B
R$drawable.class 455B
mainActivity.class 1KB
R$attr.class 310B
R.class 429B
R$string.class 465B
resources.ap_ 206KB
FiveChess.apk 214KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
human.png 1KB
status.png 192KB
icon.png 4KB
ai.png 1KB
values
strings.xml 273B
drawable-mdpi
icon.png 3KB
layout
main.xml 382B
assets
default.properties 362B
gen
cn
m15
fivegame
fivechess
m
xys
R.java 893B
proguard.cfg 1KB
src
cn
m
xys
mainActivity.java 920B
GameView.java 11KB
Const.java 1KB
.project 845B
.classpath 280B
AndroidManifest.xml 804B
共 56 条
- 1
资源评论
朱moyimi
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功