[Android]四子棋游戏开发源码和分析
来源:互联网 发布:淘宝客卖保健品 编辑:程序博客网 时间:2024/06/05 23:04
最近写了一个四子棋,说实话,我个人比较喜欢Java应用开发和JavaEE开发,但是对于Android游戏开发真的没有什么经验,所以在这个游戏开发的过程中遇见的问题贴一下分享一下:
四子棋的规则和五子棋判断胜负很类似,但是棋子的下落和俄罗斯方块很类似(点击某一列,该列就从上方以动画的形式下落)
废话不多说,先看运行效果:
游戏设计过程中遇见的问题:
1.加载bitmap图片的时候,原来图片的分辨率为130 * 130,被加载到了内存中后,尺寸为390 * 390,查了资料才知道Android会自动缩放图片,所以加载图片的时候需要一个options:
BitmapFactory.Options options = new BitmapFactory.Options();options.inScaled = false;Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),color.equals("red") ? R.drawable.circle_red : R.drawable.circle_green,options);2.bitmap缩放的方法:
Matrix matrix = new Matrix();matrix.setScale(scaleX, scaleY);Bitmap bitmap = createBitMapCopy();Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);3.View的平移动画写法:
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -points.get(position).y, 0);translateAnimation.setDuration(1024);gameView.setAnimation(translateAnimation);
4.源代码贴一下:
package site.iknown.connect4;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.graphics.Point;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.transition.Transition;import android.view.MotionEvent;import android.view.View;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;/** * Created by IamBestCoder on 17/5/16. */public class GameActivity extends AppCompatActivity implements View.OnClickListener{ private FrameLayout frameLayout = null; private int countClick = 0; private ImageView greenImage = null; private ImageView redImage = null; private int map[][] = new int[9][10]; private TextView greenText = null; private TextView redText = null; private TextView scoreView = null; //游戏者 private Player green = null; private Player red = null; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_game); //游戏者取值 green = (Player) getIntent().getSerializableExtra("green"); red = (Player) getIntent().getSerializableExtra("red"); //设置文本 greenText = (TextView) findViewById(R.id.game_text_green); redText = (TextView) findViewById(R.id.game_text_red); greenText.setText(green.getName()); redText.setText(red.getName()); scoreView = (TextView) findViewById(R.id.game_text_score); findViewById(R.id.game_play).setOnClickListener(this); findViewById(R.id.game_back).setOnClickListener(this); //ImageView greenImage = (ImageView) findViewById(R.id.game_image_green); redImage = (ImageView) findViewById(R.id.game_image_red); //framelayout frameLayout = (FrameLayout) findViewById(R.id.game_view); frameLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //计算缩放比 float scaleX = (float) (frameLayout.getWidth() / 700); float scaleY = (float) (frameLayout.getHeight() / 600); countClick++; String color = null; if (countClick % 2 == 0) { greenImage.setVisibility(View.VISIBLE); redImage.setVisibility(View.INVISIBLE); scaleX /= 13 / 7; scaleY /= 13 / 7; color = "red"; } else { greenImage.setVisibility(View.INVISIBLE); redImage.setVisibility(View.VISIBLE); color = "green"; scaleX /= 12 / 7; scaleY /= 12 / 7; } int width = frameLayout.getWidth() / 7; int height = frameLayout.getHeight() / 6; boolean isContinue = true; int i = 0; //纵轴坐标合集 ArrayList<Point> points = null; while (isContinue && i < 7) { i++; points = new ArrayList<Point>(); int circleX = width * i - (int) (width * 0.5); for (int j = 1; j <= 6; j++) { //中心点的位置应该是这个格子的中心 int circleY = height * j - (int) (height * 0.5); points.add(new Point(circleX, circleY)); } //找出所在行数 if (Math.abs(event.getX() * 1. - circleX * 1.) <= 64) { isContinue = false; } } //最后画点的位置 int position = findEmptyPosition(i); Point point = points.get(position); View gameView = new GameView(GameActivity.this, point.x, point.y, scaleX, scaleY, color); //动画 frameLayout.addView(gameView); TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -points.get(position).y, 0); translateAnimation.setDuration(1024); gameView.setAnimation(translateAnimation); //更新map map[position][i] = countClick % 2 == 0 ? 2 : 1; //判断是否有冠军产生 int winner = createWinner(); if (winner != 0) { //更新比分 if (winner == 1) { green.setSocre(green.getSocre() + 1); } else if (winner == 2) { red.setSocre(red.getSocre() + 1); } showScore(); //结果 View view = View.inflate(GameActivity.this, R.layout.view_winner, null); ImageView winnerImage = (ImageView) view.findViewById(R.id.over_image); TextView winnerText = (TextView) view.findViewById(R.id.over_text); //设置文本和图片 winnerImage.setImageResource( winner == 1 ? R.drawable.person_green : R.drawable.person_red); winnerText.setText(winner == 1 ? green.getName() : red.getName()); //对话框 AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this); builder.setView(view).setPositiveButton("Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { frameLayout.removeAllViews(); //清空集合 map = new int[9][10]; } }).create().show(); } break; } return false; } }); } private int createWinner() { //判断是否有冠军产生 int winner = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { int n = map[i][j]; if (n != 0) { if ((n == map[i + 1][j + 1] && n == map[i + 2][j + 2] && n == map[i + 3][j + 3]) || (n == map[i][j + 1] && n == map[i][j + 2] && n == map[i][j + 3]) || (n == map[i + 1][j] && n == map[i + 2][j] && n == map[i + 3][j]) || (i >= 3 && (n == map[i - 1][j + 1] && n == map[i - 2][j + 2] && n == map[i - 3][j + 3]))) { winner = n; } } } System.out.println(); } return winner; } private int findEmptyPosition(int i) { //找到空的位置 for (int j = 5; j >= 0; j--) { if (map[j][i] != 1 && map[j][i] != 2) { return j; } } return 0; } private void showScore() { scoreView.setText(green.getSocre() + " : " + red.getSocre()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.game_back: //返回 finish(); break; case R.id.game_play: //再来一局 Toast.makeText(GameActivity.this, "Game over!", Toast.LENGTH_LONG).show(); green.setSocre(0); red.setSocre(0); //清屏 frameLayout.removeAllViews(); //清空集合 map = new int[9][10]; showScore(); break; default: break; } }}
阅读全文
0 0
- [Android]四子棋游戏开发源码和分析
- Android App 源码分析(贪吃蛇游戏)
- Android开发之getMeasuredWidth和getWidth区别从源码分析
- Android开发之getMeasuredWidth和getWidth区别从源码分析
- Android开发之getMeasuredWidth和getWidth区别从源码分析
- Android开发之getMeasuredWidth和getWidth区别从源码分析
- Android开发,源码分析finish()和onBackPressed()的区别
- Android cocos2dx游戏开发——示例程序HelloCpp源码分析
- Android开发Handler源码分析
- android开发-Glide源码分析
- android游戏开发的技术实现分析
- Android游戏——贪吃蛇开发实录(改进后的源码和详解)
- Android游戏——贪吃蛇开发实录(改进后的源码和详解)
- Java游戏开发现状分析和走向
- Java游戏开发现状分析和走向
- DirectX终极游戏开发指南引擎源码分析
- Android的游戏开发和iOS游戏开发
- Android游戏源码的分析、疑问与交流
- 【Java学习笔记】20.Properties实现付费功能案例
- realm支持散列(MD5加密)实现认证
- Educational Codeforces Round 6 C. Pearls in a Row
- dubbo-非常详细的DUBBO配置方式详解
- Nginx proxy_set_header中$proxy_host,$host,$http_host的区别
- [Android]四子棋游戏开发源码和分析
- 视图
- linux系统设置命令别名
- (转)王国斌:价值投资的制度基础
- djang框架原理阐述
- 成绩排序(解题报告)
- Java实现在登录时验证验证码并在登录后记住用户名
- Bugly多渠道热更新解决方案
- Python蜜汁bug